Documentation
¶
Index ¶
Constants ¶
const ( HeaderRetryAfter = "retry-after" HeaderXRateLimitReset = "x-ratelimit-reset" HeaderXRateLimitRemaining = "x-ratelimit-remaining" )
const ( SecondaryRateLimitMessage = `` /* 136-byte string literal not displayed */ SecondaryRateLimitDocumentationURL = `https://docs.github.com/rest/overview/resources-in-the-rest-api#secondary-rate-limits` )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CallbackContext ¶
type CallbackContext struct {
UserContext *context.Context
RoundTripper *SecondaryRateLimitWaiter
SleepUntil *time.Time
TotalSleepTime *time.Duration
Request *http.Request
Response *http.Response
}
CallbackContext is passed to all callbacks. Fields might be nillable, depending on the specific callback and field.
type OnLimitDetected ¶
type OnLimitDetected func(*CallbackContext)
OnLimitDetected is a callback to be called when a new rate limit is detected (before the sleep) The totalSleepTime includes the sleep time for the upcoming sleep Note: called while holding the lock.
type OnSingleLimitExceeded ¶
type OnSingleLimitExceeded func(*CallbackContext)
OnSingleLimitPassed is a callback to be called when a rate limit is exceeding the limit for a single sleep. The sleepUntil represents the end of sleep time if the limit was not exceeded. The totalSleepTime does not include the sleep (that is not going to happen). Note: called while holding the lock.
type OnTotalLimitExceeded ¶
type OnTotalLimitExceeded func(*CallbackContext)
OnTotalLimitExceeded is a callback to be called when a rate limit is exceeding the limit for the total sleep. The sleepUntil represents the end of sleep time if the limit was not exceeded. The totalSleepTime does not include the sleep (that is not going to happen). Note: called while holding the lock.
type Option ¶
type Option func(*SecondaryRateLimitWaiter)
func WithLimitDetectedCallback ¶
func WithLimitDetectedCallback(callback OnLimitDetected) Option
WithLimitDetectedCallback adds a callback to be called when a new active rate limit is detected.
func WithSingleSleepLimit ¶
func WithSingleSleepLimit(limit time.Duration, callback OnSingleLimitExceeded) Option
WithSingleSleepLimit adds a limit to the duration allowed to wait for a single sleep (rate limit). The callback parameter is nillable.
func WithTotalSleepLimit ¶
func WithTotalSleepLimit(limit time.Duration, callback OnTotalLimitExceeded) Option
WithTotalSleepLimit adds a limit to the accumulated duration allowed to wait for all sleeps (one or more rate limits). The callback parameter is nillable.
func WithUserContext ¶
WithUserContext sets the user context to be passed to callbacks.
type SecondaryRateLimitBody ¶ added in v1.0.4
type SecondaryRateLimitBody struct {
Message string `json:"message"`
DocumentURL string `json:"documentation_url"`
}
func (SecondaryRateLimitBody) IsSecondaryRateLimit ¶ added in v1.0.4
func (s SecondaryRateLimitBody) IsSecondaryRateLimit() bool
type SecondaryRateLimitWaiter ¶
type SecondaryRateLimitWaiter struct {
Base http.RoundTripper
// contains filtered or unexported fields
}
func NewRateLimitWaiter ¶
func NewRateLimitWaiter(base http.RoundTripper, opts ...Option) (*SecondaryRateLimitWaiter, error)
func (*SecondaryRateLimitWaiter) RoundTrip ¶
RoundTrip handles the secondary rate limit by waiting for it to finish before issuing new requests. If a request got a secondary rate limit error as a response, we retry the request after waiting. Issuing more requests during a secondary rate limit may cause a ban from the server side, so we want to prevent these requests, not just for the sake of cpu/network utilization. Nonetheless, there is no way to prevent subtle race conditions without completely serializing the requests, so we prefer to let some slip in case of a race condition, i.e., after a retry-after response is received and before it is processed, a few other (parallel) requests may be issued.