22
22
attach string
23
23
files []string
24
24
debug bool
25
+ workerCount int
25
26
)
26
27
27
28
var flags , requiredFlags []* flag.Flag
@@ -38,6 +39,7 @@ func main() {
38
39
flag .StringVar (& subject , "subject" , "" , "subject of email" )
39
40
flag .BoolVar (& debug , "debug" , false , "print emails to stdout instead of sending" )
40
41
flag .StringVar (& attach , "attach" , "" , "attach a list of comma separated files" )
42
+ flag .IntVar (& workerCount , "c" , 8 , "number of concurrent requests to have" )
41
43
42
44
requiredFlagNames := []string {"text" , "csv" , "server" , "port" , "user" ,
43
45
"password" , "sender" , "subject" }
@@ -83,14 +85,24 @@ func main() {
83
85
smtpPort ,
84
86
)
85
87
88
+ jobs := make (chan Recipient , len (* recipients ))
86
89
success := make (chan * email.Email )
87
90
fail := make (chan error )
88
91
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 )
94
106
95
107
for i := 0 ; i < len (* recipients ); i ++ {
96
108
select {
0 commit comments