You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
979 B
Go
56 lines
979 B
Go
1 year ago
|
package stario
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var ERR_TIMEOUT = errors.New("TIME OUT")
|
||
|
|
||
|
func WaitUntilTimeout(tm time.Duration, fn func(chan struct{}) error) error {
|
||
|
var err error
|
||
|
finished := make(chan struct{})
|
||
|
imout := make(chan struct{})
|
||
|
go func() {
|
||
|
err = fn(imout)
|
||
|
finished <- struct{}{}
|
||
|
}()
|
||
|
select {
|
||
|
case <-finished:
|
||
|
return err
|
||
|
case <-time.After(tm):
|
||
|
close(imout)
|
||
|
return ERR_TIMEOUT
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func WaitUntilFinished(fn func() error) <-chan error {
|
||
|
finished := make(chan error)
|
||
|
go func() {
|
||
|
err := fn()
|
||
|
finished <- err
|
||
|
}()
|
||
|
return finished
|
||
|
}
|
||
|
|
||
|
func WaitUntilTimeoutFinished(tm time.Duration, fn func(chan struct{}) error) <-chan error {
|
||
|
var err error
|
||
|
finished := make(chan struct{})
|
||
|
result := make(chan error)
|
||
|
imout := make(chan struct{})
|
||
|
go func() {
|
||
|
err = fn(imout)
|
||
|
finished <- struct{}{}
|
||
|
}()
|
||
|
go func() {
|
||
|
select {
|
||
|
case <-finished:
|
||
|
result <- err
|
||
|
case <-time.After(tm):
|
||
|
close(imout)
|
||
|
result <- ERR_TIMEOUT
|
||
|
}
|
||
|
}()
|
||
|
return result
|
||
|
}
|