Skip to content

Commit

Permalink
Limit max worker count. Closes #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachlatta committed May 15, 2014
1 parent 5f44eac commit 28fbf1a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
attach string
files []string
debug bool
workerCount int
)

var flags, requiredFlags []*flag.Flag
Expand All @@ -38,6 +39,7 @@ func main() {
flag.StringVar(&subject, "subject", "", "subject of email")
flag.BoolVar(&debug, "debug", false, "print emails to stdout instead of sending")
flag.StringVar(&attach, "attach", "", "attach a list of comma separated files")
flag.IntVar(&workerCount, "c", 8, "number of concurrent requests to have")

requiredFlagNames := []string{"text", "csv", "server", "port", "user",
"password", "sender", "subject"}
Expand Down Expand Up @@ -83,14 +85,24 @@ func main() {
smtpPort,
)

jobs := make(chan Recipient, len(*recipients))
success := make(chan *email.Email)
fail := make(chan error)

go func() {
for _, recipient := range *recipients {
go sendMail(recipient, *emailField, &mailer, debug, success, fail)
}
}()
// Start workers
for i := 0; i < workerCount; i++ {
go func() {
for recipient := range jobs {
sendMail(recipient, *emailField, &mailer, debug, success, fail)
}
}()
}

// Send jobs to workers
for _, recipient := range *recipients {
jobs <- recipient
}
close(jobs)

for i := 0; i < len(*recipients); i++ {
select {
Expand Down

0 comments on commit 28fbf1a

Please sign in to comment.