Were running a simple web application on Amazon EC2, cassandra as the database.
It would be good if the user signup email could be deferred/queued, and maybe even rate limited.
What is the most "go like" way to achieve this without making things too complicated? I know there are lots of queue/task/worker like solutions, but I am more interested in something that is lightweight.
Have 1 goroutine to handle the emails and communicate with it using channels, for example :
var emailch = make(chan *UserInfo)
//you could make it buffered, that way it will block if requests are coming too fast
//var emailch = make(chan *UserInfo, 1000)
func init() {
go func() {
for ui := range emailch {
send_email(ui.Email)
}
}()
}
func Register(rw http.ResponseWriter, req *http.Request) {
//code
emailch <- &UserInfo{....}
}
Just for completion, I would like to add that the above code does not create a unbuffered Channel. The comments indicate this in some way.
//you could make it buffered, that way it will block if requests are coming too fast
//var emailch = make(chan *UserInfo, 1000)
The above Code make(chan *UserInfo)
creates a channel with a buffer of size 0. This is not an unbuffered channel, it will block the first sending until it is Receiver.
The comments regarding buffering are not false, but some kind of misleading.
On this page you can learn more about channels and try interactively. https://tour.golang.org/concurrency/2