Created
February 10, 2015 18:14
-
-
Save ngauthier/d6e6f80ce977bedca601 to your computer and use it in GitHub Desktop.
Golang timeout and tick loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// keepDoingSomething will keep trying to doSomething() until either | |
// we get a result from doSomething() or the timeout expires | |
func keepDoingSomething() (bool, error) { | |
timeout := time.After(5 * time.Second) | |
tick := time.Tick(500 * time.Millisecond) | |
// Keep trying until we're timed out or got a result or got an error | |
for { | |
select { | |
// Got a timeout! fail with a timeout error | |
case <-timeout: | |
return false, errors.New("timed out") | |
// Got a tick, we should check on doSomething() | |
case <-tick: | |
ok, err := doSomething() | |
// Error from doSomething(), we should bail | |
if err != nil { | |
return false, err | |
// doSomething() worked! let's finish up | |
} else if ok { | |
return true, nil | |
} | |
// doSomething() didn't work yet, but it didn't fail, so let's try again | |
// this will exit up to the for loop | |
} | |
} | |
} |
ing p
isn't "timeout" already a buffered channel?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If doDomething() waits more than timeout, this example will not be timeouted. Channel must be buffered, for non blocking processing.