Skip to content

Commit

Permalink
fix(listener): log sleep duration more accurately (#4444)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Nov 6, 2023
1 parent 9221ed0 commit ed4481f
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pkg/postage/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ func (l *listener) Listen(ctx context.Context, from uint64, updater postage.Even

synced := make(chan error)
closeOnce := new(sync.Once)
paged := make(chan struct{}, 1)
paged <- struct{}{}
paged := true

lastProgress := time.Now()
lastConfirmedBlock := uint64(0)
Expand All @@ -252,25 +251,33 @@ func (l *listener) Listen(ctx context.Context, from uint64, updater postage.Even
return ErrPostageSyncingStalled
}

select {
case <-ctx.Done():
return ctx.Err()
default:
}

// if we have a last blocknumber from the backend we can make a good estimate on when we need to requery
// otherwise we just use the backoff time
var expectedWaitTime time.Duration
if lastConfirmedBlock != 0 {
nextExpectedBatchBlock := (lastConfirmedBlock/batchFactor + 1) * batchFactor
remainingBlocks := nextExpectedBatchBlock - lastConfirmedBlock
expectedWaitTime = l.blockTime * time.Duration(remainingBlocks)
l.logger.Debug("sleeping until next block batch", "duration", expectedWaitTime)
} else {
expectedWaitTime = l.backoffTime
}

select {
case <-paged:
// if we paged then it means there's more things to sync on
case <-time.After(expectedWaitTime):
case <-ctx.Done():
return ctx.Err()
if !paged {
l.logger.Debug("sleeping until next block batch", "duration", expectedWaitTime)
select {
case <-time.After(expectedWaitTime):
case <-ctx.Done():
return ctx.Err()
}
}
paged = false

start := time.Now()

l.metrics.BackendCalls.Inc()
Expand Down Expand Up @@ -305,7 +312,7 @@ func (l *listener) Listen(ctx context.Context, from uint64, updater postage.Even

// do some paging (sub-optimal)
if to-from >= blockPage {
paged <- struct{}{}
paged = true
to = from + blockPage - 1
} else {
closeOnce.Do(func() { synced <- nil })
Expand Down

0 comments on commit ed4481f

Please sign in to comment.