Skip to content

Instantly share code, notes, and snippets.

@myouju
Last active June 8, 2017 06:33
Show Gist options
  • Save myouju/9e91c80bb544b5b9a4e395cab03e6f4c to your computer and use it in GitHub Desktop.
Save myouju/9e91c80bb544b5b9a4e395cab03e6f4c to your computer and use it in GitHub Desktop.
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