forked from Shopify/ghostferry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
141 lines (114 loc) · 2.58 KB
/
utils.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package ghostferry
import (
"context"
"crypto/rand"
"encoding/binary"
"sync"
"sync/atomic"
"time"
"github.com/sirupsen/logrus"
)
func WithRetries(maxRetries int, sleep time.Duration, logger *logrus.Entry, verb string, f func() error) (err error) {
return WithRetriesContext(nil, maxRetries, sleep, logger, verb, f)
}
func WithRetriesContext(ctx context.Context, maxRetries int, sleep time.Duration, logger *logrus.Entry, verb string, f func() error) (err error) {
try := 1
if logger == nil {
logger = logrus.NewEntry(logrus.StandardLogger())
}
for {
err = f()
if err == nil || err == context.Canceled {
return err
}
if maxRetries != 0 && try >= maxRetries {
break
}
logger.WithError(err).Errorf("failed to %s, %d of %d max retries", verb, try, maxRetries)
try++
if ctx != nil {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(sleep):
}
} else {
time.Sleep(sleep)
}
}
logger.WithError(err).Errorf("failed to %s after %d attempts, retry limit exceeded", verb, try)
return
}
func randomServerId() uint32 {
var buf [4]byte
if _, err := rand.Read(buf[:]); err != nil {
panic(err)
}
return binary.LittleEndian.Uint32(buf[:])
}
type AtomicBoolean int32
func (a *AtomicBoolean) Set(b bool) {
var v int32 = 0
if b {
v = 1
}
atomic.StoreInt32((*int32)(a), v)
}
func (a *AtomicBoolean) Get() bool {
return atomic.LoadInt32((*int32)(a)) == int32(1)
}
type WorkerPool struct {
Concurrency int
Process func(int) (interface{}, error)
}
// Returns a list of results of the size same as the concurrency number.
// Returns the first error that occurs during the run. Also as soon as
// a single worker errors, all workers terminates.
func (p *WorkerPool) Run(n int) ([]interface{}, error) {
results := make([]interface{}, p.Concurrency)
errCh := make(chan error, p.Concurrency)
workQueue := make(chan int)
wg := &sync.WaitGroup{}
wg.Add(p.Concurrency)
for j := 0; j < p.Concurrency; j++ {
go func(j int) {
defer wg.Done()
for workIndex := range workQueue {
result, err := p.Process(workIndex)
results[j] = result
if err != nil {
errCh <- err
return
}
}
errCh <- nil
}(j)
}
var err error = nil
i := 0
loop:
for i < n {
select {
case workQueue <- i:
i++
case err = <-errCh: // abort pool if an error was discovered
if err != nil {
break loop
}
default:
time.Sleep(500 * time.Millisecond)
}
}
close(workQueue)
wg.Wait()
close(errCh)
if err != nil {
return results, err
}
for e := range errCh {
if e != nil {
err = e
}
}
return results, err
}