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

Commit c6c2ccf

Browse files
author
Juanjo Alvarez
committed
Make Sleep check for cancelled context every second
Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent 550cc54 commit c6c2ccf

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Diff for: server/context.go

+1-1
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

Diff for: sql/expression/function/sleep.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,28 @@ 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)
40+
fchild := child.(float64)
41+
if fchild <= 0 {
42+
return 0, nil
43+
}
44+
45+
// Wake up every second to check if the context was cancelled
46+
remaining := fchild * 1000.0
47+
for remaining >= 0 {
48+
toSleep := 1000.0
49+
if remaining < 1000 {
50+
toSleep = remaining
51+
}
52+
remaining -= 1000
53+
54+
select {
55+
case <-ctx.Done():
56+
goto End
57+
case <-time.After(time.Duration(toSleep) * time.Millisecond):
58+
}
59+
}
60+
End:
61+
4162
return 0, nil
4263
}
4364

0 commit comments

Comments
 (0)