Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2372,7 +2372,7 @@ func (e *Epoch) buildBlock() {
}

e.Logger.Debug("Scheduling block building", zap.Uint64("round", metadata.Round))
e.buildBlockScheduler.Schedule(task)
e.buildBlockScheduler.ScheduleOrReplace(task)
}

func (e *Epoch) retrieveBlacklistOfParentBlock(metadata ProtocolMetadata) (Blacklist, bool) {
Expand Down
22 changes: 22 additions & 0 deletions sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,26 @@ func (as *BasicScheduler) Schedule(task Task) {
}
}

// ScheduleOrReplace schedules a task, replacing any queued task if full.
// The cancel function is called before draining the old task.
func (as *BasicScheduler) ScheduleOrReplace(task Task) {
as.mu.Lock()
defer as.mu.Unlock()

if as.closed {
return
}

// Check if full while holding the lock
if len(as.tasks) == cap(as.tasks) {
as.logger.Debug("Scheduler full, draining oldest task")
select {
case <-as.tasks:
default:
}
}

as.tasks <- task
}

type Task func() Digest