Created
January 22, 2021 17:57
-
-
Save thomaspoignant/0f1169c155400c0646c21dc71ba52dc9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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