ratelimit

package
v0.29.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2023 License: AGPL-3.0 Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BandWidthRateLimiter

type BandWidthRateLimiter struct {
	// contains filtered or unexported fields
}

BandWidthRateLimiter unicast rate limiter that limits the bandwidth that can be sent by a peer per some configured interval.

func NewBandWidthRateLimiter

func NewBandWidthRateLimiter(limit rate.Limit, burst int, lockout time.Duration, opts ...p2p.RateLimiterOpt) *BandWidthRateLimiter

NewBandWidthRateLimiter returns a new BandWidthRateLimiter. The cleanup loop will be started in a separate goroutine and should be stopped by calling Close.

func (*BandWidthRateLimiter) Allow

func (b *BandWidthRateLimiter) Allow(peerID peer.ID, msgSize int) bool

Allow checks the cached limiter for the peer and returns limiter.AllowN(msg.Size()) which will check if a peer is able to send a message of msg.Size(). If a limiter is not cached one is created.

func (*BandWidthRateLimiter) IsRateLimited

func (b *BandWidthRateLimiter) IsRateLimited(peerID peer.ID) bool

IsRateLimited returns true is a peer is currently rate limited.

func (*BandWidthRateLimiter) SetTimeNowFunc

func (b *BandWidthRateLimiter) SetTimeNowFunc(now p2p.GetTimeNow)

SetTimeNowFunc overrides the default time.Now func with the GetTimeNow func provided.

func (*BandWidthRateLimiter) Start

func (b *BandWidthRateLimiter) Start()

Start starts cleanup loop for underlying caches.

func (*BandWidthRateLimiter) Stop

func (b *BandWidthRateLimiter) Stop()

Stop sends cleanup signal to underlying rate limiters and rate limited peers maps. After the rate limiter is stopped it can not be reused.

type MessageRateLimiter

type MessageRateLimiter struct {
	// contains filtered or unexported fields
}

MessageRateLimiter unicast rate limiter that limits the amount of streams that can be created per some configured interval. A new stream is created each time a libP2P node sends a direct message.

func NewMessageRateLimiter

func NewMessageRateLimiter(limit rate.Limit, burst int, lockoutDuration time.Duration, opts ...p2p.RateLimiterOpt) *MessageRateLimiter

NewMessageRateLimiter returns a new MessageRateLimiter. The cleanup loop will be started in a separate goroutine and should be stopped by calling Close.

func (*MessageRateLimiter) Allow

func (s *MessageRateLimiter) Allow(peerID peer.ID, _ int) bool

Allow checks the cached limiter for the peer and returns limiter.Allow(). If a limiter is not cached for a peer one is created.

func (*MessageRateLimiter) IsRateLimited

func (s *MessageRateLimiter) IsRateLimited(peerID peer.ID) bool

IsRateLimited returns true is a peer is currently rate limited.

func (*MessageRateLimiter) SetTimeNowFunc

func (s *MessageRateLimiter) SetTimeNowFunc(now p2p.GetTimeNow)

SetTimeNowFunc overrides the default time.Now func with the GetTimeNow func provided.

func (*MessageRateLimiter) Start

func (s *MessageRateLimiter) Start()

Start starts cleanup loop for underlying caches.

func (*MessageRateLimiter) Stop

func (s *MessageRateLimiter) Stop()

Stop sends cleanup signal to underlying rate limiters and rate limited peers maps. After the rate limiter is closed it can not be reused.

type NoopRateLimiter

type NoopRateLimiter struct{}

func NewNoopRateLimiter

func NewNoopRateLimiter() *NoopRateLimiter

func (*NoopRateLimiter) Allow

func (n *NoopRateLimiter) Allow(_ peer.ID, _ int) bool

func (*NoopRateLimiter) IsRateLimited

func (n *NoopRateLimiter) IsRateLimited(_ peer.ID) bool

func (*NoopRateLimiter) SetTimeNowFunc

func (n *NoopRateLimiter) SetTimeNowFunc(_ p2p.GetTimeNow)

func (*NoopRateLimiter) Start

func (n *NoopRateLimiter) Start()

func (*NoopRateLimiter) Stop

func (n *NoopRateLimiter) Stop()

type OnRateLimitedPeerFunc

type OnRateLimitedPeerFunc func(pid peer.ID, role, msgType string, topic channels.Topic, reason RateLimitReason) // the callback called each time a peer is rate limited

type RateLimitReason

type RateLimitReason string
var (
	ReasonMessageCount RateLimitReason = "messagecount"
	ReasonBandwidth    RateLimitReason = "bandwidth"
)

func (RateLimitReason) String

func (r RateLimitReason) String() string

type RateLimiters

type RateLimiters struct {
	MessageRateLimiter   p2p.RateLimiter
	BandWidthRateLimiter p2p.RateLimiter
	OnRateLimitedPeer    OnRateLimitedPeerFunc // the callback called each time a peer is rate limited
	// contains filtered or unexported fields
}

RateLimiters used to manage stream and bandwidth rate limiters

func NewRateLimiters

func NewRateLimiters(messageLimiter, bandwidthLimiter p2p.RateLimiter, onRateLimitedPeer OnRateLimitedPeerFunc, opts ...RateLimitersOption) *RateLimiters

NewRateLimiters returns *RateLimiters

func NoopRateLimiters

func NoopRateLimiters() *RateLimiters

NoopRateLimiters returns noop rate limiters.

func (*RateLimiters) BandwidthAllowed

func (r *RateLimiters) BandwidthAllowed(peerID peer.ID, originRole string, msgSize int, msgType string, msgTopic channels.Topic) bool

BandwidthAllowed will return result from BandWidthRateLimiter.Allow. It will invoke the OnRateLimitedPeerFunc callback each time a peer is not allowed.

func (*RateLimiters) MessageAllowed

func (r *RateLimiters) MessageAllowed(peerID peer.ID) bool

MessageAllowed will return result from MessageRateLimiter.Allow. It will invoke the OnRateLimitedPeerFunc callback each time a peer is not allowed.

func (*RateLimiters) Start

func (r *RateLimiters) Start()

Start starts the cleanup loop for all limiters

func (*RateLimiters) Stop

func (r *RateLimiters) Stop()

Stop stops all limiters.

type RateLimitersOption

type RateLimitersOption func(*RateLimiters)

func WithDisabledRateLimiting

func WithDisabledRateLimiting(disabled bool) RateLimitersOption

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL