4
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2025-06-16 00:56:09 +00:00

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