steelix

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2019 License: MIT Imports: 7 Imported by: 0

README

Build Status codecov GolangCI Go Report Card Documentation

Steelix

Steelix is an HTTP client reinforcement using resiliency strategy.

Description

Steelix wraps native golang HTTP client with some resiliency strategies. There are two resiliency strategies available, retry and circuit breaker.

Installation

go get -u github.com/indrasaputra/steelix

Usage

Struct steelix.Client wraps http.Client. Therefore, users should prepare their own http.Client, then use constructor to create an instance of steelix.Client.

To use retry and circuit breaker strategy, provide the respective configurations.

For more information, visit documentation in godoc.

package main

import (
	"net/http"
	"time"

	"github.com/indrasaputra/backoff"
	"github.com/indrasaputra/steelix"
)

func main() {
	b := &backoff.ConstantBackoff{
		BackoffInterval: 200 * time.Millisecond,
		JitterInterval:  50 * time.Millisecond,
	}

	rc := &steelix.RetryConfig{
		Backoff:  b,
		MaxRetry: 3,
	}

	bc := &steelix.BreakerConfig{
		Name:                   "steelix-breaker",
		MinRequests:            10,
		MinConsecutiveFailures: 5,
		FailurePercentage:      20,
	}

	client := steelix.NewClient(http.DefaultClient, rc, bc)
	// omitted
}

Then, use Do(req *http.Request) method to send an HTTP request.

req, _ := http.NewRequest(http.MethodGet, "http://localhost:8080", nil)
resp, err := client.Do(req)

Documentation

Overview

Package steelix wraps http client and makes it more resilient.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff interface {
	// NextInterval returns the interval for the subsequent requests.
	NextInterval() time.Duration
}

Backoff is a contract for implementing backoff strategy.

type BreakerConfig

type BreakerConfig struct {
	// Name is the name of circuit breaker.
	Name string
	// MinRequests is the minimum requests needed for breaker to start applying
	// the logic whether it needs to change state.
	//
	// If we set MinRequests = 10, the breaker will apply the logic
	// if there is at least 10 requests. Otherwise, the logic doesn't apply.
	MinRequests uint32
	// MinConsecutiveFailures is the minimum number of failed requests that will
	// make the breaker changes its states from closed to open or half-open.
	//
	// This configuration is used together with MinRequests, which means
	// if we set MinConsecutiveFailures=5 and MinRequests=10, then there are
	// 7 failed requests, the breaker will not change its state.
	//
	// The breaker will change from closed to open
	// if either MinConsecutiveFailures or FailurePercentage condition are met.
	MinConsecutiveFailures uint32
	// FailurePercentage is a percentage which will change the breaker state
	// from closed to open if the percentage of failure requests is equal or higher
	// than the given value.
	//
	// This configuration will run together with MinRequests and alongside
	// the MinConsecutiveFailures.
	//
	// The breaker will change from closed to open
	// if either MinConsecutiveFailures or FailurePercentage condition are met.
	FailurePercentage float64
}

BreakerConfig holds configuration for implementing circuit breaker strategy.

type Client

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

Client wraps native golang http.Client but imbued with retry and circuit breaker strategy if supplied.

func NewClient

func NewClient(hc *http.Client, rc *RetryConfig, bc *BreakerConfig) *Client

NewClient creates an instance of steelix.Client.

If RetryConfig is unset, the default retry config will be applied. Leave the RetryConfig unset is the same thing as not using retry strategy.

Default retry config is:

  • Backoff: NoBackoff
  • MaxRetry: 0

If BreakerConfig is unset, the circuit breaker will not be applied.

func (*Client) Do

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

Do does almost the same things http.Client.Do does. The differences are resiliency strategies. While the native http.Client.Do only sends a request and returns a response, this method wraps it with resiliency strategies, such as retry and circuit breaker.

For example, when RetryConfig is set, the failed request will be repeated until max retry is exceeded. Before sending a request, a header X-Steelix-Retry will be set to the request. Its value is the current retry count.

When BreakerConfig is set, the request will be launched inside circuit breaker.

type RetryConfig

type RetryConfig struct {
	// Backoff is backoff strategy.
	Backoff Backoff
	// MaxRetry sets how many times a request should be tried if error happens.
	// To make it clear, this is how the request life cycle.
	// Say we set MaxRetry to 1.
	// First, a request will be launched. If an error occurred, then the request will be tried once again.
	// In other words, by setup MaxRetry to 1, at most there will be two trials.
	MaxRetry uint32
}

RetryConfig holds configuration for implementing retry strategy.

Jump to

Keyboard shortcuts

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