mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2024-04-21 12:32:08 +00:00
36 lines
649 B
Go
36 lines
649 B
Go
// Copyright (C) 2022 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package sync2
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// WithTimeout calls `do` and when the timeout is reached and `do`
|
|
// has not finished, it'll call `onTimeout` concurrently.
|
|
//
|
|
// When WithTimeout returns it's guaranteed to not call onTimeout.
|
|
func WithTimeout(timeout time.Duration, do, onTimeout func()) {
|
|
workDone := make(chan struct{})
|
|
timeoutExited := make(chan struct{})
|
|
|
|
go func() {
|
|
defer close(timeoutExited)
|
|
|
|
t := time.NewTimer(timeout)
|
|
defer t.Stop()
|
|
|
|
select {
|
|
case <-workDone:
|
|
case <-t.C:
|
|
onTimeout()
|
|
}
|
|
}()
|
|
|
|
do()
|
|
|
|
close(workDone)
|
|
<-timeoutExited
|
|
}
|