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) io.ReadWriteCloser
NewRateConn limits conn's reads and writes together by r and by the global limit, and counts them in the 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 ResetGlobalTraffic ¶ added in v0.34.9
func ResetGlobalTraffic()
ResetGlobalTraffic starts the totals over from zero.
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 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 (*Rate) Limit ¶ added in v0.33.13
Limit returns the limit in bytes per second, or 0 if unlimited.
func (*Rate) MarshalJSON ¶ added in v0.33.13
func (*Rate) ResetLimit ¶ added in v0.33.14
ResetLimit sets the limit, wakes waiting connections and starts over with a full burst.