Moves CollectErrors into pkg/utils (#493)
Merging but creating a PR for tests shortly
This commit is contained in:
parent
d1e7534f9e
commit
6b88f0a36b
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user