hystrix

package
v3.0.9 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2021 License: MIT, Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the hystrix client implementation

func NewClient

func NewClient(opts ...Option) *Client

NewClient returns a new instance of hystrix Client

func (*Client) AddPlugin

func (hhc *Client) AddPlugin(p heimdall.Plugin)

AddPlugin Adds plugin to client

func (*Client) Delete

func (hhc *Client) Delete(url string, headers http.Header) (*http.Response, error)

Delete makes a HTTP DELETE request with provided URL

func (*Client) Do

func (hhc *Client) Do(request *http.Request) (*http.Response, error)

Do makes an HTTP request with the native `http.Do` interface

func (*Client) Get

func (hhc *Client) Get(url string, headers http.Header) (*http.Response, error)

Get makes a HTTP GET request to provided URL

func (*Client) Patch

func (hhc *Client) Patch(url string, body io.Reader, headers http.Header) (*http.Response, error)

Patch makes a HTTP PATCH request to provided URL and requestBody

func (*Client) Post

func (hhc *Client) Post(url string, body io.Reader, headers http.Header) (*http.Response, error)

Post makes a HTTP POST request to provided URL and requestBody

func (*Client) Put

func (hhc *Client) Put(url string, body io.Reader, headers http.Header) (*http.Response, error)

Put makes a HTTP PUT request to provided URL and requestBody

type Option

type Option func(*Client)

Option represents the hystrix client options

func WithCommandName

func WithCommandName(name string) Option

WithCommandName sets the hystrix command name

func WithErrorPercentThreshold

func WithErrorPercentThreshold(errorPercentThreshold int) Option

WithErrorPercentThreshold sets hystrix error percent threshold

func WithFallbackFunc

func WithFallbackFunc(fn fallbackFunc) Option

WithFallbackFunc sets the fallback function

func WithHTTPClient

func WithHTTPClient(client heimdall.Doer) Option

WithHTTPClient sets a custom http client for hystrix client

Example
m := &mockClient{}
c := NewClient(WithHTTPClient(m))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
_, _ = c.Do(req)
Output:

mock client called

func WithHTTPTimeout

func WithHTTPTimeout(timeout time.Duration) Option

WithHTTPTimeout sets hystrix timeout

Example
c := NewClient(WithHTTPTimeout(5 * time.Second))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
res, err := c.Do(req)
if err != nil {
	panic(err)
}
fmt.Println("Response status : ", res.StatusCode)
Output:

Response status :  200
Example (Expired)
c := NewClient(WithHTTPTimeout(1 * time.Millisecond))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
res, err := c.Do(req)
if err != nil {
	fmt.Println("error:", err)
	return
}
fmt.Println("Response status : ", res.StatusCode)
Output:

func WithHystrixTimeout

func WithHystrixTimeout(timeout time.Duration) Option

WithHystrixTimeout sets hystrix timeout

func WithMaxConcurrentRequests

func WithMaxConcurrentRequests(maxConcurrentRequests int) Option

WithMaxConcurrentRequests sets hystrix max concurrent requests

func WithRequestVolumeThreshold

func WithRequestVolumeThreshold(requestVolumeThreshold int) Option

WithRequestVolumeThreshold sets hystrix request volume threshold

func WithRetrier

func WithRetrier(retrier heimdall.Retriable) Option

WithRetrier sets the strategy for retrying

Example
c := NewClient(WithHTTPTimeout(1*time.Millisecond), WithRetryCount(3), WithRetrier(&mockRetrier{}))
req, err := http.NewRequest(http.MethodGet, "https://www.link.doesnt.exist.io/", nil)
if err != nil {
	panic(err)
}
res, err := c.Do(req)
if err != nil {
	fmt.Println("error")
	return
}
fmt.Println("Response status : ", res.StatusCode)
Output:

retry attempt 0
retry attempt 1
retry attempt 2
retry attempt 3
error

func WithRetryCount

func WithRetryCount(retryCount int) Option

WithRetryCount sets the retry count for the Client

Example
c := NewClient(WithHTTPTimeout(1*time.Millisecond), WithRetryCount(3))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
res, err := c.Do(req)
if err != nil {
	fmt.Println("error:", err)
	return
}
fmt.Println("Response status : ", res.StatusCode)
Output:

func WithSleepWindow

func WithSleepWindow(sleepWindow int) Option

WithSleepWindow sets hystrix sleep window

func WithStatsDCollector

func WithStatsDCollector(addr, prefix string) Option

WithStatsDCollector exports hystrix metrics to a statsD backend

Example
c := NewClient(WithStatsDCollector("localhost:8125", "myapp.hystrix"))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
res, err := c.Do(req)
if err != nil {
	panic(err)
}
fmt.Println("Response status : ", res.StatusCode)
Output:

Response status :  200

Jump to

Keyboard shortcuts

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