Skip to content

Commit 28fbf1a

Browse files
committed
Limit max worker count. Closes #11.
1 parent 5f44eac commit 28fbf1a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

main.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
attach string
2323
files []string
2424
debug bool
25+
workerCount int
2526
)
2627

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

4244
requiredFlagNames := []string{"text", "csv", "server", "port", "user",
4345
"password", "sender", "subject"}
@@ -83,14 +85,24 @@ func main() {
8385
smtpPort,
8486
)
8587

88+
jobs := make(chan Recipient, len(*recipients))
8689
success := make(chan *email.Email)
8790
fail := make(chan error)
8891

89-
go func() {
90-
for _, recipient := range *recipients {
91-
go sendMail(recipient, *emailField, &mailer, debug, success, fail)
92-
}
93-
}()
92+
// Start workers
93+
for i := 0; i < workerCount; i++ {
94+
go func() {
95+
for recipient := range jobs {
96+
sendMail(recipient, *emailField, &mailer, debug, success, fail)
97+
}
98+
}()
99+
}
100+
101+
// Send jobs to workers
102+
for _, recipient := range *recipients {
103+
jobs <- recipient
104+
}
105+
close(jobs)
94106

95107
for i := 0; i < len(*recipients); i++ {
96108
select {

0 commit comments

Comments
 (0)