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

Commit 4eaee9b

Browse files
authored
Make Sleep check for cancelled context every second (#798)
Make Sleep check for cancelled context every second
2 parents 550cc54 + 7673ad0 commit 4eaee9b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

server/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func DefaultSessionBuilder(c *mysql.Conn, addr string) sql.Session {
2424

2525
// SessionManager is in charge of creating new sessions for the given
2626
// connections and keep track of which sessions are in each connection, so
27-
// they can be cancelled is the connection is closed.
27+
// they can be cancelled if the connection is closed.
2828
type SessionManager struct {
2929
addr string
3030
tracer opentracing.Tracer

sql/expression/function/sleep.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package function
22

33
import (
4+
"context"
45
"fmt"
5-
"time"
6-
76
"github.com/src-d/go-mysql-server/sql"
87
"github.com/src-d/go-mysql-server/sql/expression"
8+
"time"
99
)
1010

1111
// 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) {
3737
return nil, err
3838
}
3939

40-
time.Sleep(time.Duration(child.(float64)*1000) * time.Millisecond)
41-
return 0, nil
40+
t := time.NewTimer(time.Duration(child.(float64) * 1000) * time.Millisecond)
41+
defer t.Stop()
42+
43+
select {
44+
case <-ctx.Done():
45+
return 0, context.Canceled
46+
case <-t.C:
47+
return 0, nil
48+
}
4249
}
4350

4451
// String implements the Stringer interface.

0 commit comments

Comments
 (0)