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

     1  package p2p
     2  
     3  import (
     4  	"github.com/libp2p/go-libp2p/core/peer"
     5  
     6  	"github.com/onflow/flow-go/module/component"
     7  )
     8  
     9  // RateLimiter rate limiter with lockout feature that can be used via the IsRateLimited method.
    10  // This limiter allows users to flag a peer as rate limited for a lockout duration.
    11  type RateLimiter interface {
    12  	BasicRateLimiter
    13  	// IsRateLimited returns true if a peer is rate limited.
    14  	IsRateLimited(peerID peer.ID) bool
    15  }
    16  
    17  // BasicRateLimiter rate limiter interface
    18  type BasicRateLimiter interface {
    19  	component.Component
    20  	// Allow returns true if a message with the give size should be allowed to be processed.
    21  	Allow(peerID peer.ID, msgSize int) bool
    22  }
    23  
    24  type RateLimiterOpt func(limiter RateLimiter)
    25  
    26  // UnicastRateLimiterDistributor consumes then distributes notifications from the ratelimit.RateLimiters whenever a peer is rate limited.
    27  type UnicastRateLimiterDistributor interface {
    28  	RateLimiterConsumer
    29  	AddConsumer(consumer RateLimiterConsumer)
    30  }
    31  
    32  // RateLimiterConsumer consumes notifications from the ratelimit.RateLimiters whenever a peer is rate limited.
    33  // Implementations must:
    34  //   - be concurrency safe
    35  //   - be non-blocking
    36  type RateLimiterConsumer interface {
    37  	OnRateLimitedPeer(pid peer.ID, role, msgType, topic, reason string)
    38  }