Giter Club home page Giter Club logo

gohystrix's Introduction

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)

gohystrix's People

Contributors

dahernan avatar zeisss avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.