httpclient

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package httpclient provides a retrying circuit-breaked http.Client.

Index

Constants

View Source
const (
	DefaultTimeout      = 10 * time.Second
	DefaultInterval     = 10 * time.Minute
	DefaultFailureRatio = 0.6
)

Variables

This section is empty.

Functions

func New

func New(name string) *retryablehttp.Client

New returns a *retryablehttp.Client, with the default http.Client, DefaultTimeout, DefaultInterval and DefaultFailureRatio.

func NewRequest

func NewRequest(method, URL string, body interface{}) (*retryablehttp.Request, error)

NewRequest calls github.com/hashicorp/go-retryablehttp's NewRequest.

func NewWithClient

func NewWithClient(name string, cl *http.Client, timeout, interval time.Duration, failureRatio float64, Log func(...interface{}) error) *retryablehttp.Client

NewWithClient returns a *retryablehttp.Client based on the given *http.Client. The accompanying circuit breaker is set with the given timeout and interval.

If failureRatio is < 0, then no circuit breaker will be used, if failureRatio == 0, then DefaultFailureRation will be used.

Types

type Breaker

type Breaker interface {
	// Execute runs the given request if the circuit breaker is closed or half-open states.
	// An error is instantly returned when the circuit breaker is tripped.
	Execute(fn func() (interface{}, error)) (interface{}, error)

	// Opened reports whether the breaker is opened at the moment.
	Opened() bool
}

Breaker is the interface for a circuit breaker.

type GoBreaker

type GoBreaker struct {
	*gobreaker.CircuitBreaker
}

GoBreaker adapts a *gobreaker.CircuitBreaker to the Breaker interface.

func (GoBreaker) Opened

func (b GoBreaker) Opened() bool

Closed reports whether the circuit breaker is in opened state.

type TransportWithBreaker

type TransportWithBreaker struct {
	Tripper http.RoundTripper
	Breaker Breaker
}

TransportWithBreaker shrink-wraps a http.RoundTripper with a circuit Breaker.

func (TransportWithBreaker) RoundTrip

func (t TransportWithBreaker) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip sends the request and returns the response, waiting the circuit breaker to be in a closed state.

Jump to

Keyboard shortcuts

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