mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-29 06:35:07 +03:00
have Conn use Cancellation instead of manually setting up timers
This commit is contained in:
parent
6bf182e341
commit
cf3ebe04a7
2 changed files with 48 additions and 37 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue