have Conn use Cancellation instead of manually setting up timers

This commit is contained in:
Arceliar 2019-07-17 21:37:45 -05:00
parent 6bf182e341
commit cf3ebe04a7
2 changed files with 48 additions and 37 deletions

View file

@ -75,6 +75,8 @@ func CancellationChild(parent Cancellation) Cancellation {
return child
}
var CancellationTimeoutError = errors.New("timeout")
func CancellationWithTimeout(parent Cancellation, timeout time.Duration) Cancellation {
child := CancellationChild(parent)
go func() {
@ -83,7 +85,7 @@ func CancellationWithTimeout(parent Cancellation, timeout time.Duration) Cancell
select {
case <-child.Finished():
case <-timer.C:
child.Cancel(errors.New("timeout"))
child.Cancel(CancellationTimeoutError)
}
}()
return child