Documentation ΒΆ
Overview ΒΆ
netlimit is a package that allows to control the bandwidth of the net.Conn connections and the limiter itself. Below is a simplified architecture diagram:
Listener βββββββββββββββββββββββββββββββββ updates limits β βββββββββββββββββββββββββββββββββββ β β β β β β β β β β β global limiter β β β βββββββββββββββββββΌββ ββββββββββββββββ¬βββββββββββββββββ β β
β β β β β β β βββββββββββββββββββ²ββ β β β β β β net.Conn β Allocator β βββββββββββββββΌββββ ββββββββββββββββββ β β β β β β β ββββ β β β β β β β local limiter β β β β βββ β ββββββββββββββ β β β β β βrequests β β β β β β β β ββββββββββββββΊ ββββββββββββββ βββββββββββββ β β β βbandwitdh β β allocates ββββββ¬βββββββββββββ β β ββββββββββββββββββ bandwidth β β β βββββ¬ββββββββββββ β β β βββββββββββββββ
Index ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ( ErrLimitChangedInflight = errors.New("limit changed while inflight") ErrCouldNotReserveGlobal = errors.New("could not reserve quota in a global limiter") )
var ( // ErrLimitGreaterThanTotal is returned when the limit is greater than the total limit of the listener. ErrLimitGreaterThanTotal = errors.New("limit per conn cannot be greater than total limit") )
Functions ΒΆ
This section is empty.
Types ΒΆ
type Conn ΒΆ
Conn is a net.Conn that obeys quota limits managed by Allocator
func (*Conn) Read ΒΆ
Read reads data from the connection. Read can be made to time out and return an error after a fixed time limit; see SetDeadline and SetReadDeadline. Read will obey quota rules set by Listener
type DefaultAllocator ΒΆ
type DefaultAllocator struct {
// contains filtered or unexported fields
}
DefaultAllocator is responsible for controlling requested allocations and ensuring that they not exceed requested limits. DefaultAllocator controls a single connection
func NewDefaultAllocator ΒΆ
func NewDefaultAllocator(global *rate.Limiter, limit int) *DefaultAllocator
NewDefaultAllocator creates a new allocator with the given global and local limits. Allocator controls requested bandwidth allocations and ensures that they not exceed requested limits.
func (*DefaultAllocator) SetLimit ΒΆ
func (a *DefaultAllocator) SetLimit(limit int) error
SetLimit sets the limit of the local limiter. setting new limit will attempt to cancel inflight allocations.
type Listener ΒΆ
Listener is a net.Listener that allows to control the bandwidth of the net.Conn connections it accepts.
func Listen ΒΆ
Listen returns a *Listener that will be bound to addr with the specified limits. Listen starts gc like process in separate goroutine that attempts to clean up dangling Conn connections limitGlobal is the maximum bytes per second allowed for all net.Conn connections combined limitLocal is the maximum bytes per second allowed for a single net.Conn connection
func ListenCtx ΒΆ
func ListenCtx(ctx context.Context, network, addr string, limitTotal, limitConn int) (*Listener, error)
ListenCtx does the same as Listen but also takes a context.Context. The context argument is not used for anything after Listen returns. It's there to permit an early return for a DNS lookup, and because functions like internetSocket take a context argument even though it won't be used for the particular case of Listen
func (*Listener) SetGlobalLimit ΒΆ
SetGlobalLimit sets the limit of the bandwidth of all net.Conn connections currently active combined.
func (*Listener) SetLocalLimit ΒΆ
SetLocalLimit sets the limit of the bandwidth of all net.Conn active and future connections accepted by the listener.