-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notarization absence detection for leader failover
Signed-off-by: Yacov Manevich <[email protected]>
- Loading branch information
Showing
4 changed files
with
351 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package simplex | ||
|
||
import ( | ||
"context" | ||
"go.uber.org/zap" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type Monitor struct { | ||
logger Logger | ||
close chan struct{} | ||
time atomic.Value | ||
ticks chan time.Time | ||
tasks chan func() | ||
futureTask atomic.Value | ||
} | ||
|
||
type futureTask struct { | ||
deadline time.Time | ||
f func() | ||
} | ||
|
||
func NewMonitor(startTime time.Time, logger Logger) *Monitor { | ||
m := &Monitor{ | ||
logger: logger, | ||
close: make(chan struct{}), | ||
tasks: make(chan func(), 1), | ||
ticks: make(chan time.Time, 1), | ||
} | ||
|
||
m.time.Store(startTime) | ||
|
||
go m.run() | ||
|
||
return m | ||
} | ||
|
||
func (m *Monitor) AdvanceTime(t time.Time) { | ||
m.time.Store(t) | ||
select { | ||
case m.ticks <- t: | ||
default: | ||
} | ||
} | ||
|
||
func (m *Monitor) tick(now time.Time, taskID uint64) { | ||
ft := m.futureTask.Load() | ||
if ft == nil { | ||
return | ||
} | ||
|
||
task := ft.(*futureTask) | ||
|
||
if task.f == nil || task.deadline.IsZero() || now.Before(task.deadline) { | ||
return | ||
} | ||
|
||
m.logger.Debug("Executing f", zap.Uint64("taskID", taskID), zap.Time("deadline", task.deadline)) | ||
task.f() | ||
m.logger.Debug("Executed f with deadline", zap.Uint64("taskID", taskID)) | ||
|
||
// clean up future task to mark we have already executed it and to release memory | ||
m.futureTask.Store(&futureTask{}) | ||
} | ||
|
||
func (m *Monitor) run() { | ||
var taskID uint64 | ||
for m.shouldRun() { | ||
select { | ||
case tick := <-m.ticks: | ||
m.tick(tick, taskID) | ||
case f := <-m.tasks: | ||
m.logger.Debug("Executing f", zap.Uint64("taskID", taskID)) | ||
f() | ||
m.logger.Debug("Task executed", zap.Uint64("taskID", taskID)) | ||
} | ||
} | ||
} | ||
|
||
func (m *Monitor) shouldRun() bool { | ||
select { | ||
case <-m.close: | ||
return false | ||
default: | ||
return true | ||
} | ||
} | ||
|
||
func (m *Monitor) Close() { | ||
select { | ||
case <-m.close: | ||
return | ||
default: | ||
close(m.close) | ||
} | ||
} | ||
|
||
func (m *Monitor) WaitFor(f func()) { | ||
select { | ||
case m.tasks <- f: | ||
default: | ||
|
||
} | ||
} | ||
|
||
func (m *Monitor) WaitUntil(timeout time.Duration, f func()) context.CancelFunc { | ||
t := m.time.Load() | ||
time := t.(time.Time) | ||
|
||
m.futureTask.Store(&futureTask{ | ||
f: f, | ||
deadline: time.Add(timeout), | ||
}) | ||
|
||
return nil | ||
} |
Oops, something went wrong.