github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/inspector/internal/cache/cluster_prefixed_received_tracker.go (about)

     1  package cache
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/libp2p/go-libp2p/core/peer"
     7  	"github.com/rs/zerolog"
     8  	"go.uber.org/atomic"
     9  
    10  	"github.com/onflow/flow-go/model/flow"
    11  	"github.com/onflow/flow-go/module"
    12  )
    13  
    14  // ClusterPrefixedMessagesReceivedTracker struct that keeps track of the amount of cluster prefixed control messages received by a peer.
    15  type ClusterPrefixedMessagesReceivedTracker struct {
    16  	cache *RecordCache
    17  	// activeClusterIds atomic pointer that stores the current active cluster IDs. This ensures safe concurrent access to the activeClusterIds internal flow.ChainIDList.
    18  	activeClusterIds *atomic.Pointer[flow.ChainIDList]
    19  }
    20  
    21  // NewClusterPrefixedMessagesReceivedTracker returns a new *ClusterPrefixedMessagesReceivedTracker.
    22  func NewClusterPrefixedMessagesReceivedTracker(logger zerolog.Logger, sizeLimit uint32, clusterPrefixedCacheCollector module.HeroCacheMetrics, decay float64) (*ClusterPrefixedMessagesReceivedTracker,
    23  	error) {
    24  	config := &RecordCacheConfig{
    25  		sizeLimit:   sizeLimit,
    26  		logger:      logger,
    27  		collector:   clusterPrefixedCacheCollector,
    28  		recordDecay: decay,
    29  	}
    30  	recordCache, err := NewRecordCache(config, NewClusterPrefixedMessagesReceivedRecord)
    31  	if err != nil {
    32  		return nil, fmt.Errorf("failed to create new record cahe: %w", err)
    33  	}
    34  	return &ClusterPrefixedMessagesReceivedTracker{cache: recordCache, activeClusterIds: atomic.NewPointer[flow.ChainIDList](&flow.ChainIDList{})}, nil
    35  }
    36  
    37  // Inc increments the cluster prefixed control messages received Gauge for the peer.
    38  // All errors returned from this func are unexpected and irrecoverable.
    39  func (c *ClusterPrefixedMessagesReceivedTracker) Inc(pid peer.ID) (float64, error) {
    40  	count, err := c.cache.ReceivedClusterPrefixedMessage(pid)
    41  	if err != nil {
    42  		return 0, fmt.Errorf("failed to increment cluster prefixed received tracker gauge value for peer %s: %w", pid, err)
    43  	}
    44  	return count, nil
    45  }
    46  
    47  // Load loads the current number of cluster prefixed control messages received by a peer.
    48  // All errors returned from this func are unexpected and irrecoverable.
    49  func (c *ClusterPrefixedMessagesReceivedTracker) Load(pid peer.ID) (float64, error) {
    50  	count, _, err := c.cache.GetWithInit(pid)
    51  	if err != nil {
    52  		return 0, fmt.Errorf("failed to get cluster prefixed received tracker gauge value for peer %s: %w", pid, err)
    53  	}
    54  	return count, nil
    55  }
    56  
    57  // StoreActiveClusterIds stores the active cluster Ids in the underlying record cache.
    58  func (c *ClusterPrefixedMessagesReceivedTracker) StoreActiveClusterIds(clusterIdList flow.ChainIDList) {
    59  	c.activeClusterIds.Store(&clusterIdList)
    60  }
    61  
    62  // GetActiveClusterIds gets the active cluster Ids from the underlying record cache.
    63  func (c *ClusterPrefixedMessagesReceivedTracker) GetActiveClusterIds() flow.ChainIDList {
    64  	return *c.activeClusterIds.Load()
    65  }