/
worker.go
88 lines (74 loc) · 1.69 KB
/
worker.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 catchup
import (
"context"
"sync"
"time"
"github.com/Conflux-Chain/confura/store"
"github.com/Conflux-Chain/confura/util/rpc"
sdk "github.com/Conflux-Chain/go-conflux-sdk"
"github.com/sirupsen/logrus"
)
type worker struct {
// worker name
name string
// result channel to collect queried epoch data
resultChan chan *store.EpochData
// conflux sdk client delegated to fetch epoch data
cfx sdk.ClientOperator
}
func mustNewWorker(name, nodeUrl string, chanSize int) *worker {
return &worker{
name: name,
resultChan: make(chan *store.EpochData, chanSize),
cfx: rpc.MustNewCfxClient(nodeUrl),
}
}
func (w *worker) Sync(ctx context.Context, wg *sync.WaitGroup, epochFrom, epochTo, stepN uint64) {
defer wg.Done()
for eno := epochFrom; eno <= epochTo; {
epochData, ok := w.fetchEpoch(ctx, eno)
if !ok {
return
}
select {
case <-ctx.Done():
return
default:
select {
case <-ctx.Done():
return
case w.resultChan <- epochData:
eno += stepN
}
}
}
}
func (w *worker) Close() {
w.cfx.Close()
close(w.resultChan)
}
func (w *worker) Data() <-chan *store.EpochData {
return w.resultChan
}
func (w *worker) fetchEpoch(ctx context.Context, epochNo uint64) (*store.EpochData, bool) {
for try := 1; ; try++ {
select {
case <-ctx.Done():
return nil, false
default:
}
epochData, err := store.QueryEpochData(w.cfx, epochNo, true)
if err == nil {
return &epochData, true
}
logger := logrus.WithFields(logrus.Fields{
"epochNo": epochNo, "workerName": w.name,
}).WithError(err)
logf := logger.Debug
if try%50 == 0 {
logf = logger.Error
}
logf("Catch-up worker failed to fetch epoch")
time.Sleep(time.Second)
}
}