rate

package
v0.34.8 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: GPL-3.0 Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package rate limits the bytes a client's connections transfer, on top of github.com/djylb/byterate.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRateConn

func NewRateConn(conn io.ReadWriteCloser, r *Rate) io.ReadWriteCloser

NewRateConn limits conn's reads and writes together by r. A net.Conn stays a net.Conn. With a nil r, conn is returned as it is. Close wakes a Read or Write waiting for the limit.

Types

type Rate

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

Rate is a byte rate limiter shared by all connections of a client: their reads and writes are charged to the same bucket. A limit <= 0 means unlimited. The zero value is an unlimited, running Rate.

Its JSON form is {"NowRate":...,"Limit":...}, the measured throughput and the limit in bytes per second. Decoding ignores both: they are runtime state, and the limit comes from the client's configuration.

func NewRate

func NewRate(limitBps int64) *Rate

NewRate returns a running Rate limited to limitBps bytes per second.

func (*Rate) Limit added in v0.33.13

func (r *Rate) Limit() int64

Limit returns the limit in bytes per second, or 0 if unlimited.

func (*Rate) MarshalJSON added in v0.33.13

func (r *Rate) MarshalJSON() ([]byte, error)

func (*Rate) Now added in v0.33.13

func (r *Rate) Now() int64

Now returns the measured throughput in bytes per second.

func (*Rate) ResetLimit added in v0.33.14

func (r *Rate) ResetLimit(limitBps int64)

ResetLimit sets the limit, wakes waiting connections and starts over with a full burst.

func (*Rate) Start

func (r *Rate) Start()

Start undoes Stop, starting over with a full burst.

func (*Rate) Stop

func (r *Rate) Stop()

Stop lets all traffic pass unlimited and unmetered until Start, and wakes waiting connections.

Jump to

Keyboard shortcuts

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