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

Commit b83a1dc

Browse files
author
Juanjo Alvarez
committed
Use shorter, simpler and better solution by @creachadair
Signed-off-by: Juanjo Alvarez <[email protected]> Fix time scale Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent c6c2ccf commit b83a1dc

File tree

1 file changed

+4
-21
lines changed

1 file changed

+4
-21
lines changed

sql/expression/function/sleep.go

+4-21
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,12 @@ func (s *Sleep) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
3737
return nil, err
3838
}
3939

40-
fchild := child.(float64)
41-
if fchild <= 0 {
40+
select {
41+
case <-time.After(time.Duration(child.(float64)) * 1000.0 * time.Millisecond):
42+
return 0, nil
43+
case <-ctx.Done():
4244
return 0, nil
4345
}
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-
62-
return 0, nil
6346
}
6447

6548
// String implements the Stringer interface.

0 commit comments

Comments
 (0)