Skip to content

Instantly share code, notes, and snippets.

@thomaspoignant
Created January 22, 2021 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaspoignant/0f1169c155400c0646c21dc71ba52dc9 to your computer and use it in GitHub Desktop.
Save thomaspoignant/0f1169c155400c0646c21dc71ba52dc9 to your computer and use it in GitHub Desktop.
var cache map[string]interface{}
func main() {
ticker = time.NewTicker(3 * time.Second)
defer ticker.Stop() // stop the ticker
updaterChan = make(chan struct{})
defer close(updaterChan) // close the channel
// ...
// launch a go routine to update the cache in the background
go startUpdaterDaemon(ticker, updaterChan)
}
// startUpdaterDaemon is running in background
func startUpdaterDaemon(ticker *time.Ticker, updaterChan chan struct{}) {
for {
select {
case <- ticker.C:
// update cache
// ...
case <-updaterChan:
// stop the daemon
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment