ratelimit

package
v0.30.7 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: AGPL-3.0 Imports: 10 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 {
	*ratelimiter.RateLimiter
}

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.

type NoopRateLimiter

type NoopRateLimiter struct {
	component.Component
}

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) Start

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
	// contains filtered or unexported fields
}

RateLimiters used to manage stream and bandwidth rate limiters

func NewRateLimiters

func NewRateLimiters(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) Limiters added in v0.30.0

func (r *RateLimiters) Limiters() []p2p.RateLimiter

Limiters returns list of all underlying rate limiters.

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.

type RateLimitersOption

type RateLimitersOption func(*RateLimiters)

func WithBandwidthRateLimiter added in v0.30.0

func WithBandwidthRateLimiter(bandwidthLimiter p2p.RateLimiter) RateLimitersOption

func WithDisabledRateLimiting

func WithDisabledRateLimiting(disabled bool) RateLimitersOption

func WithMessageRateLimiter added in v0.30.0

func WithMessageRateLimiter(messageLimiter p2p.RateLimiter) RateLimitersOption

func WithNotifier added in v0.30.0

func WithNotifier(notifier p2p.RateLimiterConsumer) RateLimitersOption

type UnicastRateLimiterDistributor added in v0.30.0

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

UnicastRateLimiterDistributor subscribes to rate limited peer events from RateLimiters.

func NewUnicastRateLimiterDistributor added in v0.30.0

func NewUnicastRateLimiterDistributor() *UnicastRateLimiterDistributor

NewUnicastRateLimiterDistributor returns a new UnicastRateLimiterDistributor.

func (*UnicastRateLimiterDistributor) AddConsumer added in v0.30.0

func (r *UnicastRateLimiterDistributor) AddConsumer(consumer p2p.RateLimiterConsumer)

AddConsumer adds a consumer to the consumers list.

func (*UnicastRateLimiterDistributor) OnRateLimitedPeer added in v0.30.0

func (r *UnicastRateLimiterDistributor) OnRateLimitedPeer(pid peer.ID, role, msgType, topic, reason string)

OnRateLimitedPeer invokes each consumer callback with the rate limited peer info.

Jump to

Keyboard shortcuts

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