Documentation ¶
Index ¶
Constants ¶
const (
ErrRateCheckFailure errutil.Class = "rate check failure"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RateChecker ¶
type RateChecker struct {
// contains filtered or unexported fields
}
RateChecker is a simple queue based rate measurer that will return an error if `Check` is called too often.
func NewRateChecker ¶
func NewRateChecker(numActions int, quantum time.Duration) *RateChecker
NewRateChecker returns a new rate checker.
func (*RateChecker) Check ¶
func (rc *RateChecker) Check() error
Check returns an error if it is called more than numActions times in quantum duration.
type Wait ¶
Wait is a type that allows you to throttle actions with sleeps based on a desired rate.
The effect is that each call will incur a small duration tax per call to stay beneath the rate formed by `NumberOfActions` and `Quantum`.
func (Wait) Calculate ¶
Calculate takes the observed rate and the desired rate, and returns a quantum to sleep for that adjusts the observed rate to match the desired rate.
If the observed rate is _lower_ than the desired rate, the returned value will be negative and you're free to ignore it.
If the observed rate is _higher_ than the desired rate, a positive duration will be returned which you can pass to a `time.Sleep(...)` or similar.
The wait quantum is derrived from the following algebraic steps (where ? is what we're solving for):
pb/(pq+?) = rb/rq 1/(pq+?) = rb/pb*rq pq+? = (pb*rq)/rb ? = ((pb*rq)/rb) - pq