-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
288 lines (241 loc) · 8.08 KB
/
sender.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package send
import (
"crypto/rand"
"errors"
"math/big"
"github.com/go-resty/resty/v2"
"github.com/warp-contracts/syncer/src/utils/bundlr"
irysResponses "github.com/warp-contracts/syncer/src/utils/bundlr/responses"
"github.com/warp-contracts/syncer/src/utils/config"
"github.com/warp-contracts/syncer/src/utils/model"
"github.com/warp-contracts/syncer/src/utils/monitoring"
"github.com/warp-contracts/syncer/src/utils/task"
"github.com/warp-contracts/syncer/src/utils/turbo"
turboResponses "github.com/warp-contracts/syncer/src/utils/turbo/responses"
"gorm.io/gorm"
)
type Sender struct {
*task.Task
db *gorm.DB
input chan *model.DataItem
monitor monitoring.Monitor
// Bundling and signing
irysClient *bundlr.Client
turboClient *turbo.Client
// Updated data items
Output chan *model.DataItem
}
// Receives bundle items from the input channel and sends them to bundlr
func NewSender(config *config.Config, db *gorm.DB) (self *Sender) {
self = new(Sender)
self.db = db
self.Output = make(chan *model.DataItem)
self.Task = task.NewTask(config, "sender").
// Pool of workers that perform requests
// It's possible to run multiple requests in parallel.
// We're limiting the number of parallel requests with the number of workers.
WithWorkerPool(config.Sender.BundlerNumBundlingWorkers, config.Sender.WorkerPoolQueueSize).
WithSubtaskFunc(self.run).
WithOnAfterStop(func() {
close(self.Output)
})
return
}
func (self *Sender) WithIrysClient(client *bundlr.Client) *Sender {
self.irysClient = client
return self
}
func (self *Sender) WithTurboClient(client *turbo.Client) *Sender {
self.turboClient = client
return self
}
func (self *Sender) WithInputChannel(in chan *model.DataItem) *Sender {
self.input = in
return self
}
func (self *Sender) WithMonitor(monitor monitoring.Monitor) *Sender {
self.monitor = monitor
return self
}
func (self *Sender) setBundleProvider(item *model.DataItem) (err error) {
v, err := rand.Int(rand.Reader, big.NewInt(100))
if err != nil {
self.Log.WithError(err).Panic("Failed to generate random number")
return
}
if v.Int64() < int64(self.Config.Bundlr.IrysSendProbability) {
return item.Service.Set(model.BundlingServiceIrys)
}
return item.Service.Set(model.BundlingServiceTurbo)
}
func (self *Sender) upload(dataItem *model.DataItem, item *bundlr.BundleItem) (resp *resty.Response, err error) {
switch model.BundlingService(dataItem.Service.String) {
case model.BundlingServiceTurbo:
var uploadResponse *turboResponses.Upload
uploadResponse, resp, err = self.turboClient.Upload(self.Ctx, item)
if err != nil {
if resp != nil {
self.Log.WithError(err).
WithField("data_item_id", dataItem.DataItemID).
WithField("resp", string(resp.Body())).
WithField("code", resp.StatusCode()).
WithField("url", resp.Request.URL).
Error("Failed to upload data item to Turbo")
} else {
self.Log.WithError(err).
WithField("data_item_id", dataItem.DataItemID).
Error("Failed to upload data item to Turbo, no response")
}
return
}
// Check if the response is valid
if len(uploadResponse.Id) == 0 {
err = errors.New("Turbo response has empty ID")
self.Log.WithError(err).WithField("id", dataItem.DataItemID).Error("Bad Turbo response")
// self.monitor.GetReport().Sender.Errors.TurboError.Inc()
return
}
// We'll store the JSON response
err = dataItem.Response.Set(uploadResponse)
if err != nil {
self.monitor.GetReport().Sender.Errors.TurboMarshalError.Inc()
self.Log.WithError(err).Error("Failed to marshal response")
return
}
case model.BundlingServiceIrys:
var uploadResponse *irysResponses.Upload
uploadResponse, resp, err = self.irysClient.Upload(self.Ctx, item)
if err != nil {
if resp != nil {
self.Log.WithError(err).
WithField("data_item_id", dataItem.DataItemID).
WithField("resp", string(resp.Body())).
WithField("code", resp.StatusCode()).
WithField("url", resp.Request.URL).
Error("Failed to upload data item to Irys")
} else {
self.Log.WithError(err).
WithField("data_item_id", dataItem.DataItemID).
Error("Failed to upload data item to Irys, no response")
}
return
}
// Check if the response is valid
if len(uploadResponse.Id) == 0 {
err = errors.New("Irys response has empty ID")
self.Log.WithError(err).WithField("id", dataItem.DataItemID).Error("Bad Irys response")
self.monitor.GetReport().Sender.Errors.IrysError.Inc()
return
}
// We'll store the JSON response
err = dataItem.Response.Set(uploadResponse)
if err != nil {
self.monitor.GetReport().Sender.Errors.IrysMarshalError.Inc()
self.Log.WithError(err).Error("Failed to marshal response")
return
}
default:
err = errors.New("Unknown bundling service")
self.Log.WithError(err).WithField("service", dataItem.Service).Error("Unknown bundling service")
}
return
}
func (self *Sender) run() (err error) {
// Waits for new data items
// Finishes when when the source of items is closed
// It should be safe to assume all pending items are processed
for item := range self.input {
// Update stats
self.monitor.GetReport().Sender.State.AllBundlesFromDb.Inc()
// Copy the pointer so it's not overwritten in the next iteration
item := item
self.SubmitToWorker(func() {
self.Log.WithField("data_item_id", item.DataItemID).Info("-> Send")
defer self.Log.WithField("data_item_id", item.DataItemID).Info("<- Send")
if self.IsStopping.Load() {
// Don't start sending new items if we're stopping
return
}
var (
resp *resty.Response
err error
)
bundleItem, err := self.parse(item)
if err != nil {
self.Log.WithError(err).
WithField("data_item_id", item.DataItemID).
Error("Failed parse and validate data item")
item.State = model.BundleStateMalformed
goto end
}
// Pick random
err = self.setBundleProvider(item)
if err != nil {
return
}
err = item.Response.Set(nil)
if err != nil {
return
}
// Send the bundle item to the bundling service
resp, err = self.upload(item, bundleItem)
if err != nil {
if errors.Is(err, bundlr.ErrAlreadyReceived) {
item.State = model.BundleStateDuplicate
} else if errors.Is(err, bundlr.ErrPaymentRequired) {
item.State = model.BundleStateUploading
} else {
if resp != nil && resp.StatusCode() > 399 && resp.StatusCode() < 500 {
// Bad request shouldn't be retried
item.State = model.BundleStateMalformed
}
// Update stats
switch model.BundlingService(item.Service.String) {
case model.BundlingServiceTurbo:
self.monitor.GetReport().Sender.Errors.TurboError.Inc()
case model.BundlingServiceIrys:
self.monitor.GetReport().Sender.Errors.IrysError.Inc()
}
}
goto end
}
// Update state
item.State = model.BundleStateUploaded
// Don't keep the data item in memory, let gc do its job
// This won't remove data item from the database
item.DataItem.Bytes = nil
// Update stats
switch model.BundlingService(item.Service.String) {
case model.BundlingServiceTurbo:
self.monitor.GetReport().Sender.State.TurboSuccess.Inc()
case model.BundlingServiceIrys:
self.monitor.GetReport().Sender.State.IrysSuccess.Inc()
}
self.monitor.GetReport().Sender.State.AllSuccess.Inc()
end:
// Note: Don't wait for the Ctx to be done,
// this wouldn't save updated state to the database, which is done in the next step
self.Output <- item
})
}
return nil
}
func (self *Sender) parse(item *model.DataItem) (bundleItem *bundlr.BundleItem, err error) {
bundleItem = new(bundlr.BundleItem)
err = bundleItem.Unmarshal(item.DataItem.Bytes)
if err != nil {
self.Log.WithError(err).WithField("data_item_id", item.DataItemID).Error("Failed to unmarshal data item")
return
}
err = bundleItem.Verify()
if err != nil {
self.Log.WithError(err).WithField("data_item_id", item.DataItemID).Error("Failed to verify data item")
return
}
err = bundleItem.VerifySignature()
if err != nil {
self.Log.WithError(err).WithField("data_item_id", item.DataItemID).Error("Failed to verify data item signature")
return
}
return
}