github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/state_synchronization/requester/distributer.go (about)

     1  package requester
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/onflow/flow-go/module/executiondatasync/execution_data"
     7  	"github.com/onflow/flow-go/module/state_synchronization"
     8  )
     9  
    10  // ExecutionDataDistributor subscribes to execution data received events from the requester and
    11  // distributes them to subscribers
    12  type ExecutionDataDistributor struct {
    13  	consumers []state_synchronization.OnExecutionDataReceivedConsumer
    14  	lock      sync.RWMutex
    15  }
    16  
    17  func NewExecutionDataDistributor() *ExecutionDataDistributor {
    18  	return &ExecutionDataDistributor{}
    19  }
    20  
    21  // AddOnExecutionDataReceivedConsumer adds a consumer to be notified when new execution data is received
    22  func (p *ExecutionDataDistributor) AddOnExecutionDataReceivedConsumer(consumer state_synchronization.OnExecutionDataReceivedConsumer) {
    23  	p.lock.Lock()
    24  	defer p.lock.Unlock()
    25  
    26  	p.consumers = append(p.consumers, consumer)
    27  }
    28  
    29  // OnExecutionDataReceived is called when new execution data is received
    30  func (p *ExecutionDataDistributor) OnExecutionDataReceived(executionData *execution_data.BlockExecutionDataEntity) {
    31  	p.lock.RLock()
    32  	defer p.lock.RUnlock()
    33  
    34  	for _, consumer := range p.consumers {
    35  		consumer(executionData)
    36  	}
    37  }