Created
May 25, 2014 19:09
-
-
Save araddon/54ee97432e3913cc540a to your computer and use it in GitHub Desktop.
limited size bg queue
This file contains 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
// 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