-
Notifications
You must be signed in to change notification settings - Fork 53
/
ticker.go
41 lines (33 loc) · 831 Bytes
/
ticker.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
package metrics
import (
"time"
"github.com/relab/hotstuff/eventloop"
"github.com/relab/hotstuff/metrics/types"
"github.com/relab/hotstuff/modules"
)
// Ticker emits TickEvents on the metrics event loop.
type Ticker struct {
tickerID int
interval time.Duration
lastTick time.Time
}
// NewTicker returns a new ticker.
func NewTicker(interval time.Duration) *Ticker {
return &Ticker{interval: interval}
}
// InitModule gives the module access to the other modules.
func (t *Ticker) InitModule(mods *modules.Core) {
var eventLoop *eventloop.EventLoop
mods.Get(&eventLoop)
t.tickerID = eventLoop.AddTicker(t.interval, t.tick)
}
func (t *Ticker) tick(tickTime time.Time) any {
var event any
if !t.lastTick.IsZero() {
event = types.TickEvent{
LastTick: t.lastTick,
}
}
t.lastTick = tickTime
return event
}