github_ratelimit

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2023 License: MIT Imports: 5 Imported by: 43

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRateLimitWaiterClient

func NewRateLimitWaiterClient(base http.RoundTripper, opts ...Option) (*http.Client, error)

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

func WithUserContext(ctx context.Context) Option

WithUserContext sets the user context to be passed to callbacks.

type SecondaryRateLimitWaiter

type SecondaryRateLimitWaiter struct {
	// contains filtered or unexported fields
}

func NewRateLimitWaiter

func NewRateLimitWaiter(base http.RoundTripper, opts ...Option) (*SecondaryRateLimitWaiter, error)

func (*SecondaryRateLimitWaiter) RoundTrip

func (t *SecondaryRateLimitWaiter) RoundTrip(request *http.Request) (*http.Response, error)

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 it processed, a few other (parallel) requests may be issued.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL