rate

package
v0.35.6 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: GPL-3.0 Imports: 5 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 GlobalLimit added in v0.34.9

func GlobalLimit() int64

GlobalLimit returns the limit of all forwarded traffic in bytes per second, or 0 if unlimited.

func GlobalThroughput added in v0.34.9

func GlobalThroughput() (inBps, outBps int64)

GlobalThroughput returns the forwarded bytes per second, in and out as GlobalTraffic counts them.

func GlobalTraffic added in v0.34.9

func GlobalTraffic() (in, out int64)

GlobalTraffic returns the bytes forwarded in total: in from visitors to clients, out from clients to visitors.

func NewRateConn

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

NewRateConn limits conn's reads and writes together by r and by the global limit and optional per-entry directional limits, and counts global traffic; conn is a connection to an npc. A net.Conn stays a net.Conn. A nil r leaves only the global limit. Close wakes a Read or Write waiting for a limit.

func NewVisitorPacketConn added in v0.35.0

func NewVisitorPacketConn(pc net.PacketConn, r *Rate, entry ...*Directional) *byterate.PacketConn

NewVisitorPacketConn limits and counts the datagrams of pc, a socket that visitors send to, as NewRateConn does for a connection to an npc: by r, by the global limit and by the optional per-entry directional limits. The directions are those of the visitor, so datagrams read from pc are uploads and datagrams written to it downloads. Datagrams that the limits do not let through at once are dropped instead of waiting, as a full socket buffer drops them.

func ResetGlobalTraffic added in v0.34.9

func ResetGlobalTraffic()

ResetGlobalTraffic starts the totals over from zero.

func RootGroup added in v0.34.10

func RootGroup() *byterate.Group

RootGroup is the server level shared by all client and resource groups.

func SetGlobalLimit added in v0.34.9

func SetGlobalLimit(limitBps int64)

SetGlobalLimit sets the limit of all forwarded traffic together, in bytes per second; 0 or less means unlimited. It applies to open connections too.

func SetGlobalTraffic added in v0.34.9

func SetGlobalTraffic(in, out int64)

SetGlobalTraffic makes in and out the totals, such as persisted ones, which do not count as new traffic in GlobalThroughput.

Types

type Directional added in v0.34.10

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

Directional shares independent visitor-to-backend and backend-to-visitor buckets across all connections belonging to one tunnel or domain entry.

func (*Directional) Group added in v0.34.10

func (d *Directional) Group(parent *byterate.Group) *byterate.Group

Group is the tunnel/domain level. A nil parent keeps its current ancestry.

func (*Directional) SetLimits added in v0.34.10

func (d *Directional) SetLimits(upload, download int64)

SetLimits updates both byte-per-second limits without replacing the buckets.

func (*Directional) Throughput added in v0.34.10

func (d *Directional) Throughput() (int64, int64)

Throughput returns current upload and download speeds in bytes per second.

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) CurrentRates added in v0.34.10

func (r *Rate) CurrentRates() (int64, int64)

CurrentRates reports visitor uploads and downloads, without changing the legacy combined limiter or its JSON representation.

func (*Rate) Group added in v0.34.10

func (r *Rate) Group() *byterate.Group

Group is the client level, with a single combined rate and a global parent.

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