Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Make Sleep check for cancelled context every second #798

Merged
merged 3 commits into from
Aug 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/context.go
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ func DefaultSessionBuilder(c *mysql.Conn, addr string) sql.Session {

// SessionManager is in charge of creating new sessions for the given
// connections and keep track of which sessions are in each connection, so
// they can be cancelled is the connection is closed.
// they can be cancelled if the connection is closed.
type SessionManager struct {
addr string
tracer opentracing.Tracer
15 changes: 11 additions & 4 deletions sql/expression/function/sleep.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package function

import (
"context"
"fmt"
"time"

"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
"time"
)

// Sleep is a function that just waits for the specified number of seconds
@@ -37,8 +37,15 @@ func (s *Sleep) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, err
}

time.Sleep(time.Duration(child.(float64)*1000) * time.Millisecond)
return 0, nil
t := time.NewTimer(time.Duration(child.(float64) * 1000) * time.Millisecond)
defer t.Stop()

select {
case <-ctx.Done():
return 0, context.Canceled
case <-t.C:
return 0, nil
}
}

// String implements the Stringer interface.