mirror of
https://github.com/wweir/sower.git
synced 2024-04-21 12:42:15 +00:00
24 lines
326 B
Go
24 lines
326 B
Go
package util
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
func WithTimeout(fn func() error, timeout time.Duration) error {
|
|
var okCh = make(chan struct{})
|
|
var err error
|
|
|
|
go func() {
|
|
err = fn()
|
|
close(okCh)
|
|
}()
|
|
|
|
select {
|
|
case <-okCh:
|
|
return err
|
|
case <-time.After(timeout):
|
|
return errors.New("timeout: " + timeout.String())
|
|
}
|
|
}
|