Throttler implements a client-side throttling recommendation system. All methods are safe for concurrent use by multiple goroutines.
The throttler has the following knobs for which we will use defaults for now. If there is a need to make them configurable at a later point in time, support for the same will be added.
The adaptive throttler attempts to estimate the probability that a request will be throttled using recent history. Server requests (both throttled and accepted) are registered with the throttler (via the RegisterBackendResponse method), which then recommends client-side throttling (via the ShouldThrottle method) with probability given by: (requests - RatioForAccepts * accepts) / (requests + RequestsPadding)
type Throttler struct {
// contains filtered or unexported fields
}
func New() *Throttler
New initializes a new adaptive throttler with the default values.
func (t *Throttler) RegisterBackendResponse(throttled bool)
RegisterBackendResponse registers a response received from the backend for a request allowed by ShouldThrottle. This should be called for every response received from the backend (i.e., once for each request for which ShouldThrottle returned false).
func (t *Throttler) ShouldThrottle() bool
ShouldThrottle returns a probabilistic estimate of whether the server would throttle the next request. This should be called for every request before allowing it to hit the network. If the returned value is true, the request should be aborted immediately (as if it had been throttled by the server).