-
Notifications
You must be signed in to change notification settings - Fork 175
/
distributer.go
37 lines (29 loc) · 1.14 KB
/
distributer.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
package requester
import (
"sync"
"github.com/onflow/flow-go/module/executiondatasync/execution_data"
"github.com/onflow/flow-go/module/state_synchronization"
)
// ExecutionDataDistributor subscribes to execution data received events from the requester and
// distributes them to subscribers
type ExecutionDataDistributor struct {
consumers []state_synchronization.OnExecutionDataReceivedConsumer
lock sync.RWMutex
}
func NewExecutionDataDistributor() *ExecutionDataDistributor {
return &ExecutionDataDistributor{}
}
// AddOnExecutionDataReceivedConsumer adds a consumer to be notified when new execution data is received
func (p *ExecutionDataDistributor) AddOnExecutionDataReceivedConsumer(consumer state_synchronization.OnExecutionDataReceivedConsumer) {
p.lock.Lock()
defer p.lock.Unlock()
p.consumers = append(p.consumers, consumer)
}
// OnExecutionDataReceived is called when new execution data is received
func (p *ExecutionDataDistributor) OnExecutionDataReceived(executionData *execution_data.BlockExecutionDataEntity) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, consumer := range p.consumers {
consumer(executionData)
}
}