Last active
June 8, 2017 06:33
-
-
Save myouju/9e91c80bb544b5b9a4e395cab03e6f4c to your computer and use it in GitHub Desktop.
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
func polling(interval, maxTime int) error { | |
ticker := time.NewTicker(time.Duration(interval) * time.Second) | |
timeout := time.NewTimer(time.Duration(maxTime) * time.Second) | |
defer timeout.Stop() | |
defer ticker.Stop() | |
for { | |
select { | |
case <-timeout.C: | |
return errors.New("timeout") | |
case <-ticker.C: | |
if f() { | |
return nil | |
} | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment