Skip to content

Instantly share code, notes, and snippets.

@shanemhansen
Last active August 29, 2015 14:16
Show Gist options
  • Save shanemhansen/56e5876dda6be17ca8b6 to your computer and use it in GitHub Desktop.
Save shanemhansen/56e5876dda6be17ca8b6 to your computer and use it in GitHub Desktop.
// original version (my mental model)
func AddMessage(msg string) {
this.mutex.Lock()
messages = append(messages, msg)
this.mutex.Unlock()
}
// worker goroutine
func worker() {
for {
case msg := <-some queue
// addMessage(msg)
if this.immediate {
// save
}
case <-ticker:
// save
}
}
func save() {
this.mutex.Lock()
messages := make([]string, len(this.messages))
copy(messages, this.messages)
this.mutex.Unlock()
// go save messages to the database
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment