You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
506 B
34 lines
506 B
8 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"time"
|
||
|
|
||
|
"github.com/qiniu/log"
|
||
|
)
|
||
|
|
||
|
var ErrGoTimeout = errors.New("GoTimeoutFunc")
|
||
|
|
||
|
func GoFunc(f func() error) chan error {
|
||
|
ch := make(chan error)
|
||
|
go func() {
|
||
|
ch <- f()
|
||
|
}()
|
||
|
return ch
|
||
|
}
|
||
|
|
||
|
func GoTimeoutFunc(timeout time.Duration, f func() error) chan error {
|
||
|
ch := make(chan error)
|
||
|
go func() {
|
||
|
var err error
|
||
|
select {
|
||
|
case err = <-GoFunc(f):
|
||
|
ch <- err
|
||
|
case <-time.After(timeout):
|
||
|
log.Debugf("timeout: %v", f)
|
||
|
ch <- ErrGoTimeout
|
||
|
}
|
||
|
}()
|
||
|
return ch
|
||
|
}
|