client

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

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 HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

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 Requester

type Requester interface {
	Perform(ctx context.Context, url string, method string, requestBody interface{}, target interface{}) error
}

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

func (r *Retry) Run(ctx context.Context, fn func(ctx context.Context) error) error

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

func (r *Retry) SetMaxRetries(n int)

SetMaxRetries overrides default max retries but provided value is non-zero.

type Retryer

type Retryer interface {
	Run(ctx context.Context, fn func(ctx context.Context) error) error
	SetMaxRetries(n int)
}

Jump to

Keyboard shortcuts

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