-
Notifications
You must be signed in to change notification settings - Fork 178
/
timeout_collector_distributor.go
69 lines (58 loc) · 1.98 KB
/
timeout_collector_distributor.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
58
59
60
61
62
63
64
65
66
67
68
69
package pubsub
import (
"sync"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/model/flow"
)
// TimeoutCollectorDistributor ingests notifications about timeout aggregation and
// distributes them to consumers. Such notifications are produced by the timeout aggregation logic.
// Concurrently safe.
type TimeoutCollectorDistributor struct {
lock sync.RWMutex
consumers []hotstuff.TimeoutCollectorConsumer
}
var _ hotstuff.TimeoutCollectorConsumer = (*TimeoutCollectorDistributor)(nil)
func NewTimeoutCollectorDistributor() *TimeoutCollectorDistributor {
return &TimeoutCollectorDistributor{}
}
func (d *TimeoutCollectorDistributor) AddTimeoutCollectorConsumer(consumer hotstuff.TimeoutCollectorConsumer) {
d.lock.Lock()
defer d.lock.Unlock()
d.consumers = append(d.consumers, consumer)
}
func (d *TimeoutCollectorDistributor) OnTcConstructedFromTimeouts(tc *flow.TimeoutCertificate) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, consumer := range d.consumers {
consumer.OnTcConstructedFromTimeouts(tc)
}
}
func (d *TimeoutCollectorDistributor) OnPartialTcCreated(view uint64, newestQC *flow.QuorumCertificate, lastViewTC *flow.TimeoutCertificate) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, consumer := range d.consumers {
consumer.OnPartialTcCreated(view, newestQC, lastViewTC)
}
}
func (d *TimeoutCollectorDistributor) OnNewQcDiscovered(qc *flow.QuorumCertificate) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, consumer := range d.consumers {
consumer.OnNewQcDiscovered(qc)
}
}
func (d *TimeoutCollectorDistributor) OnNewTcDiscovered(tc *flow.TimeoutCertificate) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, consumer := range d.consumers {
consumer.OnNewTcDiscovered(tc)
}
}
func (d *TimeoutCollectorDistributor) OnTimeoutProcessed(timeout *model.TimeoutObject) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, subscriber := range d.consumers {
subscriber.OnTimeoutProcessed(timeout)
}
}