@@ -52,7 +52,8 @@ type deliverTxTask struct {
52
52
ValidateCh chan struct {}
53
53
}
54
54
55
- func (dt * deliverTxTask ) Reset () {
55
+ func (dt * deliverTxTask ) Increment () {
56
+ dt .Incarnation ++
56
57
dt .Status = statusPending
57
58
dt .Response = nil
58
59
dt .Abort = nil
@@ -62,10 +63,6 @@ func (dt *deliverTxTask) Reset() {
62
63
dt .ValidateCh = make (chan struct {}, 1 )
63
64
}
64
65
65
- func (dt * deliverTxTask ) Increment () {
66
- dt .Incarnation ++
67
- }
68
-
69
66
// Scheduler processes tasks concurrently
70
67
type Scheduler interface {
71
68
ProcessAll (ctx sdk.Context , reqs []* sdk.DeliverTxEntry ) ([]types.ResponseDeliverTx , error )
@@ -247,8 +244,8 @@ func (s *scheduler) validateTask(ctx sdk.Context, task *deliverTxTask) bool {
247
244
_ , span := s .traceSpan (ctx , "SchedulerValidate" , task )
248
245
defer span .End ()
249
246
250
- if s .shouldRerun (task ) {
251
- task .Reset ()
247
+ if ok := s .shouldRerun (task ); ok {
248
+ task .Increment ()
252
249
return false
253
250
}
254
251
return true
@@ -275,8 +272,7 @@ func (s *scheduler) validateAll(ctx sdk.Context, tasks []*deliverTxTask) ([]*del
275
272
wg .Add (1 )
276
273
go func (task * deliverTxTask ) {
277
274
defer wg .Done ()
278
- if ! s .validateTask (ctx , task ) {
279
- task .Increment ()
275
+ if ok := s .validateTask (ctx , task ); ! ok {
280
276
mx .Lock ()
281
277
res = append (res , task )
282
278
mx .Unlock ()
@@ -304,10 +300,9 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error {
304
300
workers = len (tasks )
305
301
}
306
302
307
- // validationWg waits for all validations to complete
308
- // validations happen in separate goroutines in order to wait on previous index
309
- validationWg := & sync.WaitGroup {}
310
- validationWg .Add (len (tasks ))
303
+ wg := & sync.WaitGroup {}
304
+
305
+ wg .Add (len (tasks ))
311
306
for i := 0 ; i < workers ; i ++ {
312
307
grp .Go (func () error {
313
308
for {
@@ -318,21 +313,27 @@ func (s *scheduler) executeAll(ctx sdk.Context, tasks []*deliverTxTask) error {
318
313
if ! ok {
319
314
return nil
320
315
}
321
- s .prepareAndRunTask (validationWg , ctx , task )
316
+ s .prepareAndRunTask (wg , ctx , task )
322
317
}
323
318
}
324
319
})
325
320
}
326
-
327
- for _ , task := range tasks {
328
- ch <- task
329
- }
330
- close (ch )
321
+ grp .Go (func () error {
322
+ defer close (ch )
323
+ for _ , task := range tasks {
324
+ select {
325
+ case <- gCtx .Done ():
326
+ return gCtx .Err ()
327
+ case ch <- task :
328
+ }
329
+ }
330
+ return nil
331
+ })
331
332
332
333
if err := grp .Wait (); err != nil {
333
334
return err
334
335
}
335
- validationWg .Wait ()
336
+ wg .Wait ()
336
337
337
338
return nil
338
339
}
@@ -346,7 +347,6 @@ func (s *scheduler) prepareAndRunTask(wg *sync.WaitGroup, ctx sdk.Context, task
346
347
go func () {
347
348
defer wg .Done ()
348
349
defer close (task .ValidateCh )
349
- // wait on previous task to finish validation
350
350
if task .Index > 0 {
351
351
<- s .allTasks [task .Index - 1 ].ValidateCh
352
352
}
0 commit comments