bwlimit

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 5 Imported by: 3

README

build coverage goreport Docs

bwlimit

Go net.Conn bandwidth limiter.

Only depends on the standard library.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultNetDialer = &net.Dialer{}

Functions

This section is empty.

Types

type Conn

type Conn struct {
	net.Conn // underlying net.Conn
	*Limiter // Limiter to use
}

func (*Conn) Read

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

func (*Conn) Write

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

type DialContextFn added in v0.3.0

type DialContextFn func(ctx context.Context, network string, address string) (net.Conn, error)

type DialFn added in v0.7.0

type DialFn func(network string, address string) (net.Conn, error)

type Dialer

type Dialer struct {
	DialContextFn // DialContext function we wrap
	*Limiter      // Limiter to use
}

func (*Dialer) Dial

func (d *Dialer) Dial(network string, address string) (net.Conn, error)

func (*Dialer) DialContext

func (d *Dialer) DialContext(ctx context.Context, network, address string) (conn net.Conn, err error)

type Limiter

type Limiter struct {
	Reads  *Operation
	Writes *Operation
}

func NewLimiter

func NewLimiter(ctx context.Context, limits ...int64) *Limiter

NewLimiter returns a new limiter. If you provide limits, the first will set both read and write limits, the second will set the write limit.

func (*Limiter) Wrap added in v0.7.0

func (l *Limiter) Wrap(fn DialContextFn) DialContextFn

Wrap returns a DialContextFn using the given fn that is bandwidth limited by this Limiter. If fn is nil we use DefaultNetDialer.DialContext.

type Listener

type Listener struct {
	net.Listener // underlying net.Listener
	*Limiter     // Limiter to use
}

func (*Listener) Accept

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

type Operation

type Operation struct {
	Limit atomic.Int64 // bandwith limit in bytes/sec
	Rate  atomic.Int64 // current rate in bytes/sec
	// contains filtered or unexported fields
}

func NewOperation

func NewOperation(ctx context.Context, limits []int64, idx int) (op *Operation)

Jump to

Keyboard shortcuts

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