use time.NewTimer instead of time.After in Sleep
To fix a potential temporary goroutine leak; see the added comment. The leaked goroutine would eventually stop once the timer is fired, but until then, the goroutine could be unnecessarily left around.
This commit is contained in:
parent
0d568ec2a4
commit
ad8809efb7
|
@ -47,9 +47,13 @@ func (t Tasks) Do(ctx context.Context, h cdp.Executor) error {
|
||||||
// been able to be written/tested.
|
// been able to be written/tested.
|
||||||
func Sleep(d time.Duration) Action {
|
func Sleep(d time.Duration) Action {
|
||||||
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
|
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
|
||||||
|
// Don't use time.After, to avoid a temporary goroutine leak if
|
||||||
|
// ctx is cancelled before the timer fires.
|
||||||
|
t := time.NewTimer(d)
|
||||||
select {
|
select {
|
||||||
case <-time.After(d):
|
case <-t.C:
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
t.Stop()
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue
Block a user