parallelize calls to sendmail

This commit is contained in:
gutmet 2019-07-04 18:42:21 +02:00
parent 72a148b4a9
commit 9acedd8e4d

View File

@ -13,6 +13,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time" "time"
) )
@ -118,7 +119,7 @@ func recipients(members hashset) ([]bulkRecipients, error) {
return splitRecipients(rcpts), nil return splitRecipients(rcpts), nil
} }
func sendmail(original *mail.Message, mail string, returnPath string, recps string) error { func sendmail(original *mail.Message, mail string, returnPath string, recps string, errChan chan error, wg *sync.WaitGroup) {
if debug { if debug {
log(original, sendmailPath+" -f "+returnPath+" "+recps+"\n") log(original, sendmailPath+" -f "+returnPath+" "+recps+"\n")
} }
@ -126,9 +127,9 @@ func sendmail(original *mail.Message, mail string, returnPath string, recps stri
cmd.Stdin = strings.NewReader(mail) cmd.Stdin = strings.NewReader(mail)
e := cmd.Run() e := cmd.Run()
if e != nil { if e != nil {
return err("error on call to sendmail", e) errChan <- err("error on call to sendmail", e)
} }
return nil wg.Done()
} }
func forward(mail *mail.Message, returnPath string, members hashset) []error { func forward(mail *mail.Message, returnPath string, members hashset) []error {
@ -140,8 +141,17 @@ func forward(mail *mail.Message, returnPath string, members hashset) []error {
if len(errs) > 0 { if len(errs) > 0 {
return errs return errs
} }
var wg sync.WaitGroup
wg.Add(len(rcpts))
errChan := make(chan error)
for _, bulk := range rcpts { for _, bulk := range rcpts {
err = sendmail(mail, flatmail, returnPath, bulk.String()) go sendmail(mail, flatmail, returnPath, bulk.String(), errChan, &wg)
}
go func(errChan chan error, wg *sync.WaitGroup) {
wg.Wait()
close(errChan)
}(errChan, &wg)
for err := range errChan {
errs = appendErr(errs, err) errs = appendErr(errs, err)
} }
return errs return errs