Documentation ¶
Overview ¶
Package client provides interface to perform api call to external services.
It also provides the retryer interface which can be overridden with our own function.
Index ¶
Constants ¶
View Source
const ( // DefaultRetryDelay is the time to wait between two retries. DefaultRetryDelay = 2 * time.Second DefaultMaxRetries = 3 )
View Source
const ( // DefaultHTTPTimeout expires requests after this period unless overridden by a context. DefaultHTTPTimeout = 30 * time.Second )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶ added in v1.0.0
type Client HTTPClient
func DefaultClient ¶
func DefaultClient() Client
DefaultClient returns a HTTP client with default timeout.
type HTTPClient ¶ added in v1.0.0
type Request ¶
type Request struct { Client Client APIKey string Logger logger.Logger // Debug flag activates verbose mode. It prints out http request and response objects if set to true. Debug bool }
func (*Request) Perform ¶
func (r *Request) Perform(ctx context.Context, url string, method string, requestBody interface{}, target interface{}) error
MakeRequest performs api call to given url with supplied arguments. It will include "requestBody" in the request if it is non-nil. Response from server will be deserialized to "target" interface.
type Retry ¶
type Retry struct { // Delay is time to wait before until next retry Delay time.Duration // MaxRetries is the number attempts to try running given function. MaxRetries int }
func DefaultRetryer ¶
func DefaultRetryer() *Retry
func (*Retry) Run ¶
Run executes given function with constant backoff strategy. It uses a fixed delay window to wait after each retry and executes for 'n' times.
Example:
func testFunction(ctx context.Context) (string, error) { time.Sleep(time.Second) // do some work here return "Hi! I am done", nil } r := NewRetryer().WithMaxRetries(5) ctx:= context.WithDeadline(context.Background(), time.Now().Add(10*time.Second)) execErr := r.Run(ctx, func(ctx context.Context) error { // it it not compulsory to pass context to your function fResp, fErr = testFunction(ctx) return fErr })
func (*Retry) SetMaxRetries ¶
SetMaxRetries overrides default max retries but provided value is non-zero.
Click to show internal directories.
Click to hide internal directories.