bwlimit

package module
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2025 License: MIT Imports: 6 Imported by: 3

README

build coverage goreport Docs

bwlimit

Go net.Conn bandwidth limiter.

Only depends on the standard library.

Usage

go get github.com/linkdata/bwlimit

Example

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/linkdata/bwlimit"
)

func main() {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello world!"))
	}))
	defer srv.Close()

	// limit reads to 100 bytes/sec, unlimited writes
	lim := bwlimit.NewLimiter(100, 0)
	defer lim.Stop()

	// set the default http transport DialContext
	tp := http.DefaultTransport.(*http.Transport)
	tp.DialContext = lim.Wrap(nil).DialContext

	// make a request and time it
	now := time.Now()
	resp, err := http.Get(srv.URL)
	elapsed := time.Since(now)

	if err == nil {
		var body []byte
		if body, err = io.ReadAll(resp.Body); err == nil {
			fmt.Printf("%v %v %q\n", elapsed >= time.Second, lim.Reads.Count.Load() > 100, string(body))
		}
	}
}

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 ContextDialer added in v0.12.0

type ContextDialer interface {
	DialContext(ctx context.Context, network, address string) (conn net.Conn, err error)
}

type Dialer

type Dialer struct {
	ContextDialer // ContextDialer 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 {
	*Ticker
	Reads  *Operation
	Writes *Operation
}

func NewLimiter

func NewLimiter(limits ...int64) *Limiter

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

To stop the Limiter and free it's resources, call Stop.

func (*Limiter) Stop added in v0.11.0

func (l *Limiter) Stop()

Stop stops the Limiter and frees any resources. Reads and writes on a stopped and rate-limited Limiter returns io.EOF. On an unlimited Limiter they function as normal.

func (*Limiter) Wrap added in v0.7.0

func (l *Limiter) Wrap(cd ContextDialer) ContextDialer

Wrap returns a ContextDialer wrapping cd that is bandwidth limited by this Limiter.

If cd is nil we use DefaultNetDialer. If cd is already limited by this Limiter, cd is returned unchanged.

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 {
	*Ticker              // Ticker we belong to
	Limit   atomic.Int64 // bandwith limit in bytes/sec
	Rate    atomic.Int64 // current rate in bytes/sec
	Count   atomic.Int64 // number of bytes seen
	// contains filtered or unexported fields
}

func NewOperation

func NewOperation(t *Ticker, limits []int64, idx int) (op *Operation)

func (*Operation) Stop added in v0.11.0

func (op *Operation) Stop()

type Ticker added in v0.10.0

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

A Ticker synchronizes rate calculation among multiple Limiters.

var DefaultTicker *Ticker

func (*Ticker) NewLimiter added in v0.11.0

func (ot *Ticker) NewLimiter(limits ...int64) (l *Limiter)

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

To stop the limiter and free it's resources, call Stop.

func (*Ticker) WaitCh added in v0.11.0

func (ot *Ticker) WaitCh() (ch <-chan struct{})

WaitCh returns a channel that will close when the current rate limit time slice runs out.

Jump to

Keyboard shortcuts

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