goHystrix

package module
v0.0.0-...-24081d0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2016 License: Apache-2.0 Imports: 8 Imported by: 3

README

Implementation in Go (aka golang) of some of the stability patterns, like Circuit Breaker.

You can also check breaker my other implementaion of the Circuit Breaker, simpler and with not dependencies: https://github.com/dahernan/breaker

How to use

You need to implement goHystrix.Interface

type Interface interface {
	Run() (interface{}, error)
}

There is also a goHystrix.FallbackInterface, if you need a fallback function:

type FallbackInterface interface {
	Interface
	Fallback() (interface{}, error)
}

Basic command with a String

import (
	"fmt"
	"github.com/dahernan/goHystrix"
	"testing"
	"time"
)

type MyStringCommand struct {
	message string
}

// This is the normal method to execute (circuit is close) 
func (c *MyStringCommand) Run() (interface{}, error) {
	return c.message, nil
}

// This is the method to execute in case the circuit is open
func (c *MyStringCommand) Fallback() (interface{}, error) {
	return "FALLBACK", nil
}

func TestString(t *testing.T) {
	// creates a new command
	command := goHystrix.NewCommand("commandName", "commandGroup", &MyStringCommand{"helloooooooo"})
	
	// Sync execution
	value, err := command.Execute()

	fmt.Println("Sync call ---- ")
	fmt.Println("Value: ", value)
	fmt.Println("Error: ", err)

	// Async execution

	valueChan, errorChan := command.Queue()

	fmt.Println("Async call ---- ")
	select {
	case value = <-valueChan:
		fmt.Println("Value: ", value)
	case err = <-errorChan:
		fmt.Println("Error: ", err)
	}

	fmt.Println("Succesfull Calls ", command.HealthCounts().Success)
	fmt.Println("Mean: ", command.Stats().Mean())

	fmt.Println("All the circuits in JSON format: ", goHystrix.Circuits().ToJSON())


}

Default circuit values when you create a command

ErrorPercetageThreshold - 50.0 - If (number_of_errors / total_calls * 100) > 50.0 the circuit will be open
MinimumNumberOfRequest - if total_calls < 20 the circuit will be close
NumberOfSecondsToStore - 20 seconds (for health counts you only evaluate the last 20 seconds of calls)
NumberOfSamplesToStore - 50 values (you store the duration of 50 successful calls using reservoir sampling)
Timeout - 2 * time.Seconds

You can customize the default values when you create the command


// ErrorPercetageThreshold - 60.0
// MinimumNumberOfRequest - 3
// NumberOfSecondsToStore - 5
// NumberOfSamplesToStore - 10
// Timeout - 10 * time.Second
goHystrix.NewCommandWithOptions("commandName", "commandGroup", &MyStringCommand{"helloooooooo"}, goHystrix.CommandOptions{
		ErrorsThreshold:        60.0,
		MinimumNumberOfRequest: 3,
		NumberOfSecondsToStore: 5,
		NumberOfSamplesToStore: 10,
		Timeout:                10 * time.Second,
	})

Exposes all circuits information by http in JSON format

import	_ "github.com/dahernan/goHystrix/httpexp"

GET - http://host/debug/circuits

Exposes the metrics using statds

// host and prefix for statds server, and send the gauges of the state of the circuits every 3 Seconds
goHystrix.UseStatsd("0.0.0.0:8125", "myprefix", 3*time.Second)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CircuitsReset

func CircuitsReset()

func SetExporter

func SetExporter(export MetricExport)

func UseStatsd

func UseStatsd(address string, prefix string, dur time.Duration)

Types

type CircuitBreaker

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

func NewCircuit

func NewCircuit(group string, name string, options CommandOptions) *CircuitBreaker

func NewCircuitNoParams

func NewCircuitNoParams(group string, name string) *CircuitBreaker

func (*CircuitBreaker) IsOpen

func (c *CircuitBreaker) IsOpen() (bool, string)

func (*CircuitBreaker) Metric

func (c *CircuitBreaker) Metric() *Metric

func (*CircuitBreaker) ToJSON

func (c *CircuitBreaker) ToJSON() string

type CircuitHolder

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

func Circuits

func Circuits() *CircuitHolder

func NewCircuitsHolder

func NewCircuitsHolder() *CircuitHolder

func (*CircuitHolder) Get

func (holder *CircuitHolder) Get(group string, name string) (*CircuitBreaker, bool)

func (*CircuitHolder) Set

func (holder *CircuitHolder) Set(group string, name string, value *CircuitBreaker)

func (*CircuitHolder) ToJSON

func (holder *CircuitHolder) ToJSON() string

type Command

type Command struct {
	Interface
	*Executor
}

func NewCommand

func NewCommand(name string, group string, command Interface) *Command

NewCommand- create a new command with the default values

func NewCommandFunc

func NewCommandFunc(name string, group string, commandFunc CommandFunc) *Command

func NewCommandFuncFallback

func NewCommandFuncFallback(name string, group string, commandFunc CommandFunc, fallbackFunc CommandFunc) *Command

func NewCommandWithOptions

func NewCommandWithOptions(name string, group string, command Interface, options CommandOptions) *Command

type CommandError

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

func NewCommandError

func NewCommandError(group string, name string, runError error, fallbackError error) CommandError

func (CommandError) Error

func (e CommandError) Error() string

Nested error handling

type CommandFunc

type CommandFunc func() (interface{}, error)

Same API but with Funcional flavor

type CommandFuncFallbackWrap

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

func (CommandFuncFallbackWrap) Fallback

func (c CommandFuncFallbackWrap) Fallback() (interface{}, error)

func (CommandFuncFallbackWrap) Run

func (c CommandFuncFallbackWrap) Run() (interface{}, error)

type CommandFuncWrap

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

func (CommandFuncWrap) Run

func (c CommandFuncWrap) Run() (interface{}, error)

type CommandOptions

type CommandOptions struct {
	ErrorsThreshold        float64
	MinimumNumberOfRequest int64
	NumberOfSecondsToStore int
	NumberOfSamplesToStore int
	Timeout                time.Duration
}

CommandOptions, you can custimize the values, for the Circuit Breaker and the Metrics stores ErrorsThreshold - if number_of_errors / total_calls * 100 > errorThreshold the circuit will be open MinimumNumberOfRequest - if total_calls < minimumNumberOfRequest the circuit will be close NumberOfSecondsToStore - Is the number of seconds to count the stats, for example 10 stores just the last 10 seconds of calls NumberOfSamplesToStore - Is the number of samples to store for calculate the stats, greater means more precision to get Mean, Max, Min... Timeout - the timeout for the command

func CommandOptionsDefaults

func CommandOptionsDefaults() CommandOptions

CommandOptionsDefaults ErrorsThreshold - 50 - If number_of_errors / total_calls * 100 > 50.0 the circuit will be open MinimumNumberOfRequest - if total_calls < 20 the circuit will be close NumberOfSecondsToStore - 20 seconds NumberOfSamplesToStore - 50 values Timeout - 2 * time.Seconds

type Executor

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

func NewExecutor

func NewExecutor(name string, group string, command Interface, options CommandOptions) *Executor

func (*Executor) Execute

func (ex *Executor) Execute() (interface{}, error)

func (*Executor) HealthCounts

func (ex *Executor) HealthCounts() HealthCounts

func (*Executor) Metric

func (ex *Executor) Metric() *Metric

func (*Executor) Queue

func (ex *Executor) Queue() (chan interface{}, chan error)

type FallbackInterface

type FallbackInterface interface {
	Interface
	Fallback() (interface{}, error)
}

type HealthCounts

type HealthCounts struct {
	HealthCountsBucket
	Total           int64
	ErrorPercentage float64
}

type HealthCountsBucket

type HealthCountsBucket struct {
	Failures       int64
	Success        int64
	Fallback       int64
	FallbackErrors int64
	Timeouts       int64
	Panics         int64
	// contains filtered or unexported fields
}

func (*HealthCountsBucket) Reset

func (c *HealthCountsBucket) Reset()

type Interface

type Interface interface {
	Run() (interface{}, error)
}

type Metric

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

func NewMetric

func NewMetric(group string, name string) *Metric

func NewMetricWithParams

func NewMetricWithParams(group string, name string, numberOfSecondsToStore int, sampleSize int) *Metric

func (*Metric) Fail

func (m *Metric) Fail()

func (*Metric) Fallback

func (m *Metric) Fallback()

func (*Metric) FallbackError

func (m *Metric) FallbackError()

func (*Metric) HealthCounts

func (m *Metric) HealthCounts() HealthCounts

func (*Metric) LastFailure

func (m *Metric) LastFailure() time.Time

func (*Metric) LastSuccess

func (m *Metric) LastSuccess() time.Time

func (*Metric) LastTimeout

func (m *Metric) LastTimeout() time.Time

func (*Metric) Panic

func (m *Metric) Panic()

func (*Metric) Stats

func (m *Metric) Stats() sample.Sample

func (*Metric) Success

func (m *Metric) Success(duration time.Duration)

func (*Metric) Timeout

func (m *Metric) Timeout()

type MetricExport

type MetricExport interface {
	Success(group string, name string, duration time.Duration)
	Fail(group string, name string)
	Fallback(group string, name string)
	FallbackError(group string, name string)
	Timeout(group string, name string)
	Panic(group string, name string)
	State(circuits *CircuitHolder)
}

func Exporter

func Exporter() MetricExport

func NewNilExport

func NewNilExport() MetricExport

func NewStatsdExport

func NewStatsdExport(statsdClient statsd.Statter, prefix string) MetricExport

type NilExport

type NilExport struct {
}

func (NilExport) Fail

func (NilExport) Fail(group string, name string)

func (NilExport) Fallback

func (NilExport) Fallback(group string, name string)

func (NilExport) FallbackError

func (NilExport) FallbackError(group string, name string)

func (NilExport) Panic

func (NilExport) Panic(group string, name string)

func (NilExport) State

func (NilExport) State(circuits *CircuitHolder)

func (NilExport) Success

func (NilExport) Success(group string, name string, duration time.Duration)

func (NilExport) Timeout

func (NilExport) Timeout(group string, name string)

type StatsdExport

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

func (StatsdExport) Fail

func (s StatsdExport) Fail(group string, name string)

func (StatsdExport) Fallback

func (s StatsdExport) Fallback(group string, name string)

func (StatsdExport) FallbackError

func (s StatsdExport) FallbackError(group string, name string)

func (StatsdExport) Panic

func (s StatsdExport) Panic(group string, name string)

func (StatsdExport) State

func (s StatsdExport) State(holder *CircuitHolder)

func (StatsdExport) Success

func (s StatsdExport) Success(group string, name string, duration time.Duration)

func (StatsdExport) Timeout

func (s StatsdExport) Timeout(group string, name string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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