Documentation ¶
Overview ¶
Package hardy contains a wrapper for http.Client with some extra features, like common headers, debugger, fallback and exponential backoff as retry mechanism.
Index ¶
- Constants
- type Client
- type Debugger
- type Error
- type ErrorCode
- type FallbackFunc
- type Option
- func WithBackoffMultiplier(multiplier float64) Option
- func WithDebugDisabled() Option
- func WithDebugger(debugger Debugger) Option
- func WithHttpClient(httpClient *http.Client) Option
- func WithMaxInterval(interval time.Duration) Option
- func WithMaxRetries(maxRetries int) Option
- func WithNoUserAgentHeader() Option
- func WithUserAgentHeader(userAgent string) Option
- func WithWaitInterval(interval time.Duration) Option
- type ReaderFunc
Constants ¶
const ( // ClientVersion is the client version ClientVersion = "0.2.0" // DefaultWaitIntervalMilliseconds is the default wait interval in milliseconds between each retry. DefaultWaitIntervalMilliseconds = 500 // DefaultMaxIntervalInMilliseconds is the default maximum wait interval in milliseconds between each retry. DefaultMaxIntervalInMilliseconds = 5000 // DefaultMaxRetries is the default maximum allowed retries. DefaultMaxRetries = 3 // DefaultBackoffMultiplier is the default backoff multiplier used to get next intervals. DefaultBackoffMultiplier = 2 // DefaultTimeoutInSeconds is the maximum timeout for each attempt in seconds. DefaultTimeoutInSeconds = 10 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
NewClient creates a new Hardy wrapper with the defaults or an error if it was misconfigured by some given option.
func (*Client) Try ¶
func (c *Client) Try(ctx context.Context, req *http.Request, readerFunc ReaderFunc, fallbackFunc FallbackFunc) error
Try tries to perform the given request as per configurations. If some FallbackFunc is given, after max retries were reached, it will be called. It might return the following errors:
- ErrNoReaderFuncFound - when no reader function was provided.
- ErrMaxRetriesReached - if max retries were reached.
- context.DeadlineExceeded or context.Canceled - if the given context was gone.
- ErrUnexpected is the error returned when no one of the previous errors match.
type Debugger ¶ added in v0.2.0
type Debugger interface {
Println(v ...any)
}
Debugger declares the methods that the debuggers should implement.
type Error ¶
type Error struct { // ErrorCode is a well-known error code. ErrorCode ErrorCode `json:"error_code"` // HTTPStatusCode is the equivalent HTTP status code. HTTPStatusCode int `json:"status_code"` // Message is the user-friendly error message. Message string `json:"message"` // contains filtered or unexported fields }
Error represents the structured errors returned by the client.
type ErrorCode ¶ added in v0.2.0
type ErrorCode string
ErrorCode is the type of well-known error codes.
const ( // ErrInvalidClientConfiguration is the error returned when some client configuration is invalid. ErrInvalidClientConfiguration ErrorCode = "invalid_configuration_error" // ErrNoDebuggerFound is the error returned when the debug mode was enabled but debugger was given. ErrNoDebuggerFound ErrorCode = "no_debugger_found_error" // ErrNoHTTPClientFound is the error returned when no HTTP Client was given. ErrNoHTTPClientFound ErrorCode = "no_http_client_found_error" // ErrNoReaderFuncFound is the error returned when no ReaderFunc was given. ErrNoReaderFuncFound ErrorCode = "no_reader_func_found_error" // ErrMaxRetriesReached is the error returned when the max allowed retries were reached. ErrMaxRetriesReached ErrorCode = "max_retries_reached_error" // ErrUnexpected is the error returned when no one of the previous errors match. ErrUnexpected ErrorCode = "unexpected_error" )
type FallbackFunc ¶
type FallbackFunc func() error
FallbackFunc defines the function that should be used as fallback when max retries was reached out.
type Option ¶ added in v0.2.0
Option defines the optional configurations for the Client.
func WithBackoffMultiplier ¶ added in v0.2.0
WithBackoffMultiplier Determines the multiplier that should be used to calculate the backoff interval.
func WithDebugDisabled ¶ added in v0.2.0
func WithDebugDisabled() Option
WithDebugDisabled disables the debug mode.
func WithDebugger ¶ added in v0.2.0
WithDebugger enables the debug mode, dumping the requests to output using the client logger.
func WithHttpClient ¶ added in v0.2.0
WithHttpClient overrides the default HTTP Client used by the one given.
func WithMaxInterval ¶ added in v0.2.0
WithMaxInterval determines the max interval between each fail request.
func WithMaxRetries ¶ added in v0.2.0
WithMaxRetries determines how many retries should be attempted.
func WithNoUserAgentHeader ¶ added in v0.2.0
func WithNoUserAgentHeader() Option
WithNoUserAgentHeader disables adding the User-Agent header in the request.
func WithUserAgentHeader ¶ added in v0.2.0
WithUserAgentHeader enables adding the User-Agent header in the request and overrides the default one.
func WithWaitInterval ¶ added in v0.2.0
WithWaitInterval determines the base duration between each fail request.
type ReaderFunc ¶
ReaderFunc defines the function responsible to read the HTTP response and also determines if a new retry must be performed returning an error or not, returning nil.
Keep in mind while writing your reader function that we shouldn't perform a retry if the response contains an error due to a client error (400-499 HTTP error codes), but consider only the ones not caused by them instead, as 500 and 503 HTTP error codes, for instance.