Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter provides efficient rate limiting. The zero value is immediately usable.
A Limiter is not safe to use from multiple goroutines simultaneously.
type Ticker ¶ added in v1.3.0
type Ticker struct {
C <-chan struct{} // sends a struct{}{} at most maxrate times per second
// contains filtered or unexported fields
}
func NewTicker ¶
NewTicker returns a Ticker that reads ticks from a parent Ticker and sends a `struct{}{}` at most `*maxrate` times per second.
The effective max rate is thus the lower of the parent Tickers maxrate and this Tickers `*maxrate`.
A nil `parent` Ticker means tick rate is only limited by `maxrate`. If the parent Ticker is closed, this Ticker will stop sending ticks.
A nil `maxrate` or a `*maxrate` of zero or less sends as quickly as possible, so only limited by the parent channel.
func (*Ticker) Close ¶ added in v1.3.0
func (ticker *Ticker) Close()
Close stops the Ticker and frees resources.
It is safe to call multiple times or concurrently. Once Close() returns, no more ticks will be delivered, and if you passed a non-nil ticker counter to NewTicker(), it will be correct.
func (*Ticker) Load ¶ added in v1.8.0
Load returns the current load in permille, or -1 if the rate is unlimited.
Load is rounded up, and is only zero if the rate is zero.
func (*Ticker) Wait ¶ added in v1.5.0
Wait delays until the next tick is available, then adds a "free tick" back to the Ticker.
Returns true if we waited successfully, or false if the Ticker is closed.
Typical use case is to launch goroutines that in turn uses the Ticker to rate limit some resource or action, thus limiting the rate of goroutines spawning without impacting the resource use rate.