Last active
August 29, 2015 14:16
-
-
Save shanemhansen/56e5876dda6be17ca8b6 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
// 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