Moves CollectErrors into pkg/utils (#493)

Merging but creating a PR for tests shortly
This commit is contained in:
Dylan Lott 2018-10-17 14:23:44 -06:00 committed by GitHub
parent d1e7534f9e
commit 6b88f0a36b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 31 deletions

View File

@ -147,35 +147,5 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
errch <- runCfg.Uplink.Run(ctx)
}()
return collectErrors(errch, 5*time.Second)
}
// collectErrors returns first error from channel and all errors that happen within duration
func collectErrors(errch chan error, duration time.Duration) error {
errch = discardNil(errch)
errs := []error{<-errch}
timeout := time.After(duration)
for {
select {
case err := <-errch:
errs = append(errs, err)
case <-timeout:
return utils.CombineErrors(errs...)
}
}
}
// discard nil errors that are returned from services
func discardNil(ch chan error) chan error {
r := make(chan error)
go func() {
for err := range ch {
if err == nil {
continue
}
r <- err
}
close(r)
}()
return r
return utils.CollectErrors(errch, 5*time.Second)
}

View File

@ -8,6 +8,7 @@ import (
"encoding/gob"
"net/url"
"strings"
"time"
)
// GetBytes transforms an empty interface type into a byte slice
@ -75,3 +76,33 @@ func (errs combinedError) Error() string {
}
return ""
}
// CollectErrors returns first error from channel and all errors that happen within duration
func CollectErrors(errch chan error, duration time.Duration) error {
errch = discardNil(errch)
errs := []error{<-errch}
timeout := time.After(duration)
for {
select {
case err := <-errch:
errs = append(errs, err)
case <-timeout:
return CombineErrors(errs...)
}
}
}
// discard nil errors that are returned from services
func discardNil(ch chan error) chan error {
r := make(chan error)
go func() {
for err := range ch {
if err == nil {
continue
}
r <- err
}
close(r)
}()
return r
}