-
Notifications
You must be signed in to change notification settings - Fork 2
/
sim_queue.go
181 lines (161 loc) 路 4.89 KB
/
sim_queue.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package preconshare
import (
"context"
"encoding/json"
"sync"
"time"
"github.com/cairoeth/preconfirmations/preconf-share/metrics"
"github.com/cairoeth/preconfirmations/preconf-share/simqueue"
"github.com/cenkalti/backoff/v4"
"github.com/ethereum/go-ethereum/common"
"go.uber.org/zap"
"golang.org/x/time/rate"
)
var (
consumeSimulationTimeout = 15 * time.Second
simCacheTimeout = 1 * time.Second
)
type SimQueue struct {
log *zap.Logger
queue simqueue.Queue
eth EthClient
workers []SimulationWorker
workersPerNode int
}
func NewQueue(
log *zap.Logger, queue simqueue.Queue, eth EthClient, sim []SimulationBackend, simRes SimulationResult,
workersPerNode int, backgroundWg *sync.WaitGroup, cancelCache *RedisCancellationCache,
) *SimQueue {
log = log.Named("queue")
q := &SimQueue{
log: log,
queue: queue,
eth: eth,
workers: make([]SimulationWorker, 0, len(sim)),
workersPerNode: workersPerNode,
}
for i := range sim {
worker := SimulationWorker{
log: log.Named("worker").With(zap.Int("worker-id", i)),
simulationBackend: sim[i],
simRes: simRes,
cancelCache: cancelCache,
backgroundWg: backgroundWg,
}
q.workers = append(q.workers, worker)
}
return q
}
func (q *SimQueue) Start(ctx context.Context) *sync.WaitGroup {
process := make([]simqueue.ProcessFunc, 0, len(q.workers)*q.workersPerNode)
for i := range q.workers {
if q.workersPerNode > 1 {
workers := simqueue.MultipleWorkers(q.workers[i].Process, q.workersPerNode, rate.Inf, 1)
process = append(process, workers...)
} else {
process = append(process, q.workers[i].Process)
}
}
blockNumber, err := q.eth.BlockNumber(ctx)
if err != nil {
q.log.Warn("Failed to get block number", zap.Error(err))
} else {
_ = q.queue.UpdateBlock(blockNumber)
}
wg := q.queue.StartProcessLoop(ctx, process)
wg.Add(1)
go func() {
defer wg.Done()
back := backoff.NewExponentialBackOff()
back.MaxInterval = 3 * time.Second
back.MaxElapsedTime = 12 * time.Second
ticker := time.NewTicker(100 * time.Millisecond)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := backoff.Retry(func() error {
blockNumber, err := q.eth.BlockNumber(ctx)
if err != nil {
return err
}
return q.queue.UpdateBlock(blockNumber)
}, back)
if err != nil {
q.log.Error("Failed to update block number", zap.Error(err))
}
}
}
}()
return wg
}
func (q *SimQueue) ScheduleRequest(ctx context.Context, request *SendRequestArgs, highPriority bool) error {
startAt := time.Now()
defer func() {
metrics.RecordRequestAddQueueDuration(time.Since(startAt).Milliseconds())
}()
data, err := json.Marshal(request)
if err != nil {
return err
}
return q.queue.Push(ctx, data, highPriority, uint64(request.Inclusion.DesiredBlock), uint64(request.Inclusion.MaxBlock))
}
type SimulationWorker struct {
log *zap.Logger
simulationBackend SimulationBackend
simRes SimulationResult
cancelCache *RedisCancellationCache
backgroundWg *sync.WaitGroup
}
func (w *SimulationWorker) Process(ctx context.Context, data []byte, info simqueue.QueueItemInfo) (err error) {
startAt := time.Now()
defer func() {
metrics.RecordBundleProcessDuration(time.Since(startAt).Milliseconds())
}()
var bundle SendRequestArgs
err = json.Unmarshal(data, &bundle)
if err != nil {
w.log.Error("Failed to unmarshal bundle simulation data", zap.Error(err))
return err
}
var hash common.Hash
if bundle.Metadata != nil {
hash = bundle.Metadata.RequestHash
}
logger := w.log.With(zap.String("bundle", hash.Hex()))
// Check if bundle was cancelled
cancelled, err := w.isBundleCancelled(ctx, &bundle)
if err != nil {
// We don't return error here, because we would consider this error as non-critical as our cancellations are "best effort".
logger.Error("Failed to check if bundle was cancelled", zap.Error(err))
}
if cancelled {
logger.Info("Bundle is not simulated because it was cancelled")
return simqueue.ErrProcessUnrecoverable
}
w.backgroundWg.Add(1)
go func() {
defer w.backgroundWg.Done()
resCtx, cancel := context.WithTimeout(context.Background(), consumeSimulationTimeout)
defer cancel()
err = w.simRes.SimulatedBundle(resCtx, &bundle, info)
if err != nil {
w.log.Error("Failed to consume matched share bundle", zap.Error(err))
}
}()
return nil
}
func (w *SimulationWorker) isBundleCancelled(ctx context.Context, bundle *SendRequestArgs) (bool, error) {
ctx, cancel := context.WithTimeout(ctx, simCacheTimeout)
defer cancel()
if bundle.Metadata == nil {
w.log.Error("Bundle has no metadata, skipping cancel check")
return false, nil
}
res, err := w.cancelCache.IsCancelled(ctx, append([]common.Hash{bundle.Metadata.RequestHash}, bundle.Metadata.BodyHashes...))
if err != nil {
return false, err
}
return res, nil
}