2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-01 15:15:38 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2018-12-12 13:15:34 +00:00
|
|
|
"fmt"
|
2018-08-23 08:05:56 +01:00
|
|
|
"strings"
|
2018-10-17 21:23:44 +01:00
|
|
|
"time"
|
2019-02-06 16:40:55 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2018-08-01 15:15:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetBytes transforms an empty interface type into a byte slice
|
|
|
|
func GetBytes(key interface{}) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := gob.NewEncoder(&buf)
|
|
|
|
err := enc.Encode(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
2018-08-23 08:05:56 +01:00
|
|
|
|
2018-12-12 13:15:34 +00:00
|
|
|
// SplitDBURL returns the driver and DSN portions of a URL
|
|
|
|
func SplitDBURL(s string) (string, string, error) {
|
|
|
|
// consider https://github.com/xo/dburl if this ends up lacking
|
|
|
|
parts := strings.SplitN(s, "://", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return "", "", fmt.Errorf("Could not parse DB URL %s", s)
|
2018-08-23 08:05:56 +01:00
|
|
|
}
|
2018-12-12 13:15:34 +00:00
|
|
|
if parts[0] == "postgres" {
|
|
|
|
parts[1] = s // postgres wants full URLS for its DSN
|
2018-12-04 16:26:30 +00:00
|
|
|
}
|
2018-12-12 13:15:34 +00:00
|
|
|
return parts[0], parts[1], nil
|
2018-08-23 08:05:56 +01:00
|
|
|
}
|
2018-09-05 17:10:35 +01:00
|
|
|
|
|
|
|
// CombineErrors combines multiple errors to a single error
|
2018-09-11 14:57:12 +01:00
|
|
|
func CombineErrors(errs ...error) error {
|
2018-12-11 18:24:31 +00:00
|
|
|
var errlist ErrorGroup
|
|
|
|
errlist.Add(errs...)
|
|
|
|
return errlist.Finish()
|
2018-09-11 14:57:12 +01:00
|
|
|
}
|
2018-09-05 17:10:35 +01:00
|
|
|
|
|
|
|
type combinedError []error
|
|
|
|
|
|
|
|
func (errs combinedError) Cause() error {
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs[0]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (errs combinedError) Error() string {
|
|
|
|
if len(errs) > 0 {
|
|
|
|
limit := 5
|
|
|
|
if len(errs) < limit {
|
|
|
|
limit = len(errs)
|
|
|
|
}
|
|
|
|
allErrors := errs[0].Error()
|
|
|
|
for _, err := range errs[1:limit] {
|
|
|
|
allErrors += "\n" + err.Error()
|
|
|
|
}
|
|
|
|
return allErrors
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2018-10-17 21:23:44 +01:00
|
|
|
|
2018-12-11 18:24:31 +00:00
|
|
|
// ErrorGroup contains a set of non-nil errors
|
2019-02-06 16:40:55 +00:00
|
|
|
type ErrorGroup errs.Group
|
2018-12-11 18:24:31 +00:00
|
|
|
|
|
|
|
// Add adds an error to the ErrorGroup if it is non-nil
|
2019-02-06 16:40:55 +00:00
|
|
|
func (e *ErrorGroup) Add(errrs ...error) {
|
|
|
|
(*errs.Group)(e).Add(errrs...)
|
2018-12-11 18:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finish returns nil if there were no non-nil errors, the first error if there
|
|
|
|
// was only one non-nil error, or the result of CombineErrors if there was more
|
|
|
|
// than one non-nil error.
|
2019-02-06 16:40:55 +00:00
|
|
|
func (e *ErrorGroup) Finish() error {
|
|
|
|
return (*errs.Group)(e).Err()
|
2018-12-11 18:24:31 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 21:23:44 +01:00
|
|
|
// 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
|
|
|
|
}
|