github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/collection/events/cluster_events_distributor.go (about)

     1  package events
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/onflow/flow-go/engine/collection"
     7  	"github.com/onflow/flow-go/model/flow"
     8  )
     9  
    10  // ClusterEventsDistributor distributes cluster events to a list of subscribers.
    11  type ClusterEventsDistributor struct {
    12  	subscribers []collection.ClusterEvents
    13  	mu          sync.RWMutex
    14  }
    15  
    16  var _ collection.ClusterEvents = (*ClusterEventsDistributor)(nil)
    17  
    18  // NewClusterEventsDistributor returns a new events *ClusterEventsDistributor.
    19  func NewClusterEventsDistributor() *ClusterEventsDistributor {
    20  	return &ClusterEventsDistributor{}
    21  }
    22  
    23  func (d *ClusterEventsDistributor) AddConsumer(consumer collection.ClusterEvents) {
    24  	d.mu.Lock()
    25  	defer d.mu.Unlock()
    26  	d.subscribers = append(d.subscribers, consumer)
    27  }
    28  
    29  // ActiveClustersChanged distributes events to all subscribers.
    30  func (d *ClusterEventsDistributor) ActiveClustersChanged(list flow.ChainIDList) {
    31  	d.mu.RLock()
    32  	defer d.mu.RUnlock()
    33  	for _, sub := range d.subscribers {
    34  		sub.ActiveClustersChanged(list)
    35  	}
    36  }