From 9acedd8e4d2cfc01d33a27bf19ee7432d67f867c Mon Sep 17 00:00:00 2001 From: gutmet Date: Thu, 4 Jul 2019 18:42:21 +0200 Subject: [PATCH] parallelize calls to sendmail --- picolist.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/picolist.go b/picolist.go index 800888d..40caf0b 100644 --- a/picolist.go +++ b/picolist.go @@ -13,6 +13,7 @@ import ( "os/exec" "path/filepath" "strings" + "sync" "time" ) @@ -118,7 +119,7 @@ func recipients(members hashset) ([]bulkRecipients, error) { 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 { 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) e := cmd.Run() 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 { @@ -140,8 +141,17 @@ func forward(mail *mail.Message, returnPath string, members hashset) []error { if len(errs) > 0 { return errs } + var wg sync.WaitGroup + wg.Add(len(rcpts)) + errChan := make(chan error) 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) } return errs