boomerang

package module
v1.3.1-0...-3395e50 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2021 License: MIT Imports: 15 Imported by: 1

README

boomerang

This project is inspired by

1) Go's default http package
2) Jitter/Backoff ----> https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
3) Hashicorp library https://github.com/hashicorp/go-retryablehttp

TODO

1) Test cases for hystrix, basic testing for hystrix client
2) More test cases for http client
3) Add Prometheus metric support on the client

EXAMPLE USAGE

1) HTTP Client
	client := NewHttpClient(10*time.Millisecond, DefaultTransport())
	backoffStratergy := NewExponentialBackoff(2*time.Millisecond, 10*time.Millisecond, 2.0)
	client.SetRetries(3)
	client.SetBackoff(backoffStratergy)


	resp, err := client.Get("/foo/bar")
	if err!=nil{
		resp.Body.Close()
	}

2) Hystrix Client (TODO)

Documentation

Index

Constants

View Source
const (
	DefaultMaxHttpRetries = 1
	DefaultTimeout        = 100 * time.Millisecond
)
View Source
const (
	DEFAULT_PROM_METRICS_NAMESPACE = "boomerang"
)
View Source
const (
	DefaultMaxHystrixRetries = 1
)

Variables

This section is empty.

Functions

func DefaultClient

func DefaultClient() *http.Client

DefaultClient returns a new http.Client with similar default values to http.Client, but with a non-shared Transport, idle connections disabled, and keepalives disabled.

func DefaultPooledClient

func DefaultPooledClient() *http.Client

DefaultPooledClient returns a new http.Client with similar default values to http.Client, but with a shared Transport. Do not use this function for transient clients as it can leak file descriptors over time. Only use this for clients that will be re-used for the same host(s).

func DefaultPooledTransport

func DefaultPooledTransport() *http.Transport

DefaultPooledTransport returns a new http.Transport with similar default values to http.DefaultTransport. Do not use this for transient transports as it can leak file descriptors over time. Only use this for transports that will be re-used for the same host(s).

func DefaultRetryPolicy

func DefaultRetryPolicy(resp *http.Response, err error) (bool, error)

DefaultRetryPolicy provides a default callback for Client.CheckRetry, which will retry on connection errors and server errors.

func DefaultTransport

func DefaultTransport() *http.Transport

DefaultTransport returns a new http.Transport with similar default values to http.DefaultTransport, but with idle connections and keepalives disabled.

func NewRequest

func NewRequest(method, url string, body io.ReadSeeker) (*http.Request, error)

Types

type Backoff

type Backoff interface {
	NextInterval(retry int) time.Duration
}

func NewBackoffFunc

func NewBackoffFunc(f BackoffFunc) Backoff

func NewConstantBackoff

func NewConstantBackoff(timeout time.Duration) Backoff

NewConstanctBackoff returns an instance of ConstantBackoff

func NewExponentialBackoff

func NewExponentialBackoff(minTimeout, maxTimeout time.Duration, exponentFactor float64) Backoff

NewExponentialBackoff returns an instance of ExponentialBackoff

func NewJitterBackoff

func NewJitterBackoff(minTimeout, maxTimeout time.Duration, exponentFactor float64) Backoff

NewExponentialBackoff returns an instance of ExponentialBackoff

type BackoffFunc

type BackoffFunc func(retry int) time.Duration

func (BackoffFunc) NextInterval

func (b BackoffFunc) NextInterval(retry int) time.Duration

type CheckRetry

type CheckRetry func(resp *http.Response, err error) (bool, error)

type Client

type Client interface {
	Get(url string) (*http.Response, error)
	Head(url string) (*http.Response, error)
	Post(url string, contentType string, body io.ReadSeeker) (*http.Response, error)
	PostForm(url string, data url.Values) (*http.Response, error)
	Do(req *http.Request) (*http.Response, error)
}

Client Is a generic HTTP client interface

func DefaultHttpClient

func DefaultHttpClient(config *ClientConfig) Client

type ClientConfig

type ClientConfig struct {
	RecordMetrics   bool
	MetricNamespace string
	Timeout         time.Duration
	Transport       *http.Transport
	Backoff         Backoff
	RetryFunc       CheckRetry
	MaxRetries      int
}

type HttpClient

type HttpClient struct {
	Logger *log.Logger // Customer logger instance.

	Backoff Backoff
	// CheckRetry specifies the policy for handling retries, and is called
	// after each request. The default policy is DefaultRetryPolicy.
	CheckRetry CheckRetry
	MaxRetries int
	// To explicitly state if no metrics are to be recorded for this client
	RecordMetrics bool
	MetricsCtx    Metrics
	// contains filtered or unexported fields
}

func NewHttpClient

func NewHttpClient(config *ClientConfig) *HttpClient

func (*HttpClient) Do

func (c *HttpClient) Do(req *http.Request) (*http.Response, error)

func (*HttpClient) Get

func (c *HttpClient) Get(url string) (*http.Response, error)

func (*HttpClient) Head

func (c *HttpClient) Head(url string) (*http.Response, error)

func (*HttpClient) Post

func (c *HttpClient) Post(url string, contentType string, body io.ReadSeeker) (*http.Response, error)

func (*HttpClient) PostForm

func (c *HttpClient) PostForm(url string, data url.Values) (*http.Response, error)

func (*HttpClient) QuietMode

func (c *HttpClient) QuietMode()

func (*HttpClient) SetBackoff

func (c *HttpClient) SetBackoff(bc Backoff)

func (*HttpClient) SetRetries

func (c *HttpClient) SetRetries(retry int)

func (*HttpClient) TurnOffMetrics

func (c *HttpClient) TurnOffMetrics()

type HystrixClient

type HystrixClient struct {
	Logger *log.Logger // Customer logger instance.

	Backoff Backoff

	// CheckRetry specifies the policy for handling retries, and is called
	// after each request. The default policy is DefaultRetryPolicy.
	CheckRetry CheckRetry
	MaxRetries int
	// contains filtered or unexported fields
}

func NewHystrixClient

func NewHystrixClient(timeout time.Duration, hc HystrixCommandConfig) *HystrixClient

func (*HystrixClient) Do

func (c *HystrixClient) Do(req *http.Request) (*http.Response, error)

func (*HystrixClient) Get

func (c *HystrixClient) Get(url string) (*http.Response, error)

func (*HystrixClient) Head

func (c *HystrixClient) Head(url string) (*http.Response, error)

func (*HystrixClient) Post

func (c *HystrixClient) Post(url string, contentType string, body io.ReadSeeker) (*http.Response, error)

func (*HystrixClient) PostForm

func (c *HystrixClient) PostForm(url string, data url.Values) (*http.Response, error)

func (*HystrixClient) SetFallbackFunc

func (c *HystrixClient) SetFallbackFunc(fbf func(err error) error)

type HystrixCommandConfig

type HystrixCommandConfig struct {
	Timeout                int `json:"timeout"`
	MaxConcurrentRequests  int `json:"max_concurrent_requests"`
	RequestVolumeThreshold int `json:"request_volume_threshold"`
	SleepWindow            int `json:"sleep_window"`
	ErrorPercentThreshold  int `json:"error_percent_threshold"`
	CommandName            string
	Transport              *http.Transport
}

type LenReader

type LenReader interface {
	Len() int
}

LenReader is an interface implemented by many in-memory io.Reader's. Used for automatically sending the right Content-Length header when possible.

type Metrics

type Metrics interface {
	Record(time.Time, int, error)
}

func NewPrometheusMetrics

func NewPrometheusMetrics(namespace, subsystem string) Metrics

Jump to

Keyboard shortcuts

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