transports

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMaximumRetryAttempts = errors.New("maximum retry attempts reached")

Functions

This section is empty.

Types

type RateLimitAlign

type RateLimitAlign func(ctx context.Context, res *http.Response) (limit, remaining int64, ok bool)

RateLimitAlign is a function that's used after a response is retrieved to align the rate limit. It takes the request context and the HTTP response as parameters and returns the actual rate limit values. If the alignment is successful, it returns the rate limit, remaining limit, and a boolean flag indicating success. If the alignment is not possible or the response does not contain the rate limit information, the function should return zero values for the rate limit and remaining limit, and false for the success flag. This function can be used to handle cases where the API returns a status code other than 429 (Too Many Requests) but the rate limit has been reached such as GraphQL.

type RateLimitTransport

type RateLimitTransport struct {
	Transport  http.RoundTripper // The underlying HTTP transport
	Align      RateLimitAlign    // The rate limit align function
	Limiter    *limiter.Limiter  // The rate limiter
	LimiterKey string            // The key used to identify the rate limiter
}

RateLimitTransport is a custom transport that handles rate limiting of HTTP requests. It wraps an existing http.RoundTripper implementation and enforces rate limits using a limiter.

func (RateLimitTransport) L

L returns a logger instance associated with the RateLimitTransport. It provides a structured logging experience with trace information.

func (*RateLimitTransport) RoundTrip

func (rt *RateLimitTransport) RoundTrip(req *http.Request) (res *http.Response, err error)

RoundTrip implements the http.RoundTripper interface. It performs the HTTP request and enforces rate limits using the configured limiter. If the rate limit is reached, it waits for the duration until the rate limit resets before retrying the request. If the response status code is 429 (Too Many Requests), it increments the rate limit count by the limit value. If an alignment function is provided, it adjusts the rate limit based on the actual values returned by the alignment function.

type RetryTransport

type RetryTransport struct {
	Transport     http.RoundTripper // The underlying HTTP transport
	RetryCount    int               // The maximum number of retry attempts
	RetryDuration time.Duration     // The duration to wait between retries
}

RetryTransport is a custom transport that implements retry logic for HTTP requests. It wraps an existing http.RoundTripper implementation and retries failed requests up to a maximum count.

func (RetryTransport) L

L returns a logger instance associated with the RetryTransport. It provides a structured logging experience with trace information.

func (*RetryTransport) RoundTrip

func (rt *RetryTransport) RoundTrip(req *http.Request) (res *http.Response, err error)

RoundTrip implements the http.RoundTripper interface. It performs the HTTP request and retries failed requests up to the maximum retry count. If a timeout error occurs, it logs the error and waits for the specified retry duration before retrying. If a non-timeout error occurs, it immediately returns the error without retrying.

type UserAgentGenerator

type UserAgentGenerator func() string

UserAgentGenerator is a function type that returns a string representing a User-Agent value. Implementations of this function can be used to generate custom User-Agent strings. For example, you can use `github.com/corpix/uarand`.GetRandom().

type UserAgentTransport

type UserAgentTransport struct {
	Generator UserAgentGenerator // The User-Agent generator function
	Transport http.RoundTripper  // The underlying HTTP transport
}

UserAgentTransport is a custom transport that sets a User-Agent header on outgoing HTTP requests. It wraps an existing http.RoundTripper implementation and adds the functionality to set a custom User-Agent.

func (*UserAgentTransport) RoundTrip

func (t *UserAgentTransport) RoundTrip(req *http.Request) (res *http.Response, err error)

RoundTrip implements the http.RoundTripper interface. It sets the User-Agent header on the request using the provided Generator (if not nil), and delegates the actual request handling to the wrapped Transport. The response and error from the wrapped Transport.RoundTrip method are returned.

Jump to

Keyboard shortcuts

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