-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
88 lines (81 loc) · 2 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package util
import (
"fmt"
"runtime"
"strings"
"sync"
"time"
xxhash "github.com/cespare/xxhash/v2"
"github.com/go-sif/sif"
itypes "github.com/go-sif/sif/internal/types"
)
// CreateAsyncErrorChannel produces a channel for errors
func CreateAsyncErrorChannel() chan error {
return make(chan error)
}
// WaitAndFetchError attempts to fetch an error from an async goroutine
func WaitAndFetchError(wg *sync.WaitGroup, errors chan error) error {
// use reading from the errors channel to block, rather than
// the WaitGroup directly. when the wg is done, we know there
// aren't any more errors. Closing the channel breaks us out
// of the infinite loop.
go func() {
wg.Wait()
close(errors)
}()
for {
select {
case err := <-errors:
if err != nil {
return err
}
return nil
default:
time.Sleep(50 * time.Millisecond)
}
}
}
// GetTrace produces the string representation of a stack trace
func GetTrace() string {
var name, file string
var line int
var pc [16]uintptr
var res strings.Builder
n := runtime.Callers(3, pc[:])
for _, pc := range pc[:n] {
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
file, line = fn.FileLine(pc)
name = fn.Name()
if !strings.HasPrefix(name, "runtime.") {
fmt.Fprintf(&res, "%s\n\t%s:%d\n", name, file, line)
}
}
return res.String()
}
// FormatMultiError formats multierrors for logging
func FormatMultiError(merrs []error) string {
var msg = ""
for i := 0; i < len(merrs); i++ {
msg += fmt.Sprintf("%+v\n", merrs[i])
}
return msg
}
// KeyColumns is a shortcut for defining a KeyingOperation which uses multiple source
// column values to produce a compound key.
func KeyColumns(colNames ...string) sif.KeyingOperation {
return func(row sif.Row) ([]byte, error) {
irow := row.(itypes.AccessibleRow) // access row internals
hasher := xxhash.New()
for _, cname := range colNames {
data, err := irow.GetHashableColData(cname)
if err != nil {
return nil, err
}
hasher.Write(data)
}
return hasher.Sum(nil), nil
}
}