-
Notifications
You must be signed in to change notification settings - Fork 176
/
engine.go
57 lines (49 loc) · 1.39 KB
/
engine.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package checker
import (
"context"
"time"
"github.com/onflow/flow-go/module/component"
"github.com/onflow/flow-go/module/irrecoverable"
)
type Engine struct {
*component.ComponentManager
core *Core
}
// DefaultTimeInterval triggers the check once every minute,
const DefaultTimeInterval = time.Minute * 1
func NewEngine(core *Core) *Engine {
e := &Engine{
core: core,
}
e.ComponentManager = component.NewComponentManagerBuilder().
AddWorker(func(ctx irrecoverable.SignalerContext, ready component.ReadyFunc) {
ready()
err := e.runLoop(ctx, DefaultTimeInterval)
if err != nil {
ctx.Throw(err)
}
}).
Build()
return e
}
// runLoop runs the check every minute.
// Why using a timer instead of listening to finalized and executed events?
// because it's simpler as it doesn't need to subscribe to those events.
// It also runs less checks, note: the checker doesn't need to find the
// first mismatched block, as long as it can find a mismatch, it's good enough.
// A timer could reduce the number of checks, as it only checks once every minute.
func (e *Engine) runLoop(ctx context.Context, tickInterval time.Duration) error {
ticker := time.NewTicker(tickInterval)
defer ticker.Stop() // critical for ticker to be garbage collected
for {
select {
case <-ticker.C:
err := e.core.RunCheck()
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}