Skip to content

Instantly share code, notes, and snippets.

@araddon
Created May 25, 2014 19:09
Show Gist options
  • Save araddon/54ee97432e3913cc540a to your computer and use it in GitHub Desktop.
Save araddon/54ee97432e3913cc540a to your computer and use it in GitHub Desktop.
limited size bg queue
// from
// https://github.com/sbhackerspace/sbhx-boardinator/blob/0b210dd088502ff2a55453897601ef2361679514/types/email.go#L65
func StartEmailQueue() {
var semaphore = make(chan bool, 10) // Can only send 10 at once
go func() {
for {
email := <-EmailQueue
email.Status = QUEUED
// Spawn goroutine immediately
go func(e *Email) {
// Will only block if 10 emails are already being sent.
semaphore <- true
defer func() {
// Drain value from channel to make room
// for another email sender
<-semaphore
}()
err := e.Send()
if err != nil {
e.Failed(err)
return
}
e.Status = SUCCESS
}(email)
}
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment