netlimit

package module
v0.0.0-...-9a766f3 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2022 License: MIT Imports: 8 Imported by: 0

README ΒΆ

netlimit πŸ§™πŸ»β€β™‚οΈ

netlimit is a small package that allows you to control bandwitdh usage of net.Listener and net.Conn, it delivers custom wrapper types around net.Listener and net.Conn interfaces and util functions like netlimit.Listen() and netlimit.ListenCtx() to bootstrap the whole process


Usage

Create netlimit.Listener

//globalLimit limits bandwidth of a listener
globalLimit := 1024 //Bps

//localLimit limits bandwidth of a single connection
localLimit := 512 //Bps

ln, err := netlisten.Listen(proto, addr, globalLimit, localLimit)

Use it as you would any other net.Listener e.g

http.Serve(ln, handler)

You can tweak limits during runtime

Change local(per connection) limit use

err := ln.SetLocalLimit(newLocalLimit)

Change global(server) limit use

err := ln.SetGlobalLimit(newLocalLimit)

Resources

https://pkg.go.dev/github.com/charconstpointer/netlimit

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 ΒΆ

View Source
var (
	ErrLimitChangedInflight  = errors.New("limit changed while inflight")
	ErrCouldNotReserveGlobal = errors.New("could not reserve quota in a global limiter")
)
View Source
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 Allocator ΒΆ

type Allocator interface {
	Alloc(ctx context.Context, n int) (int, error)
	SetLimit(limit int) error
}

type Conn ΒΆ

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

Conn is a net.Conn that obeys quota limits managed by Allocator

func NewConn ΒΆ

func NewConn(conn net.Conn, a Allocator) (*Conn, error)

NewConn returns a new Conn

func (*Conn) Close ΒΆ

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) Read ΒΆ

func (c *Conn) Read(b []byte) (n int, err error)

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

func (*Conn) SetLimit ΒΆ

func (c *Conn) SetLimit(limit int) error

SetLimit sets the limit of the local limiter.

func (*Conn) Write ΒΆ

func (c *Conn) Write(b []byte) (n int, err error)

Write writes data to the connection. Write can be made to time out and return an error after a fixed time limit; see SetDeadline and SetWriteDeadline. Write 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) Alloc ΒΆ

func (a *DefaultAllocator) Alloc(ctx context.Context, requestedQuota int) (int, error)

Alloc blocks until it is allowed to allocate requested quota.

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.

func (*DefaultAllocator) TryAlloc ΒΆ

func (a *DefaultAllocator) TryAlloc(ctx context.Context, quota int) (int, error)

TryAlloc reserves quota in a global limiter and then blocks until it is allowed to allocate the quota in the local limiter. Once the local limiter allows allocation, TryAlloc waits for the readiness or the global reservation

type Listener ΒΆ

type Listener struct {
	net.Listener
	// contains filtered or unexported fields
}

Listener is a net.Listener that allows to control the bandwidth of the net.Conn connections it accepts.

func Listen ΒΆ

func Listen(network, addr string, limitGlobal, limitLocal int) (*Listener, error)

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) Accept ΒΆ

func (l *Listener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

func (*Listener) Close ΒΆ

func (l *Listener) Close() error

func (*Listener) SetGlobalLimit ΒΆ

func (l *Listener) SetGlobalLimit(limit int) error

SetGlobalLimit sets the limit of the bandwidth of all net.Conn connections currently active combined.

func (*Listener) SetLocalLimit ΒΆ

func (l *Listener) SetLocalLimit(newLocalLimit int) error

SetLocalLimit sets the limit of the bandwidth of all net.Conn active and future connections accepted by the listener.

Jump to

Keyboard shortcuts

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