MCPcopy Index your code
hub / github.com/afex/hystrix-go

github.com/afex/hystrix-go @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
201 symbols 706 edges 26 files 72 documented · 36% 4 cross-repo links updated 2y ago★ 4,41828 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

hystrix-go

Build Status GoDoc Documentation

Hystrix is a great project from Netflix.

Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

I think the Hystrix patterns of programmer-defined fallbacks and adaptive health monitoring are good for any distributed system. Go routines and channels are great concurrency primitives, but don't directly help our application stay available during failures.

hystrix-go aims to allow Go programmers to easily build applications with similar execution semantics of the Java-based Hystrix library.

For more about how Hystrix works, refer to the Java Hystrix wiki

For API documentation, refer to GoDoc

How to use

import "github.com/afex/hystrix-go/hystrix"

Execute code as a Hystrix command

Define your application logic which relies on external systems, passing your function to hystrix.Go. When that system is healthy this will be the only thing which executes.

hystrix.Go("my_command", func() error {
    // talk to other services
    return nil
}, nil)

Defining fallback behavior

If you want code to execute during a service outage, pass in a second function to hystrix.Go. Ideally, the logic here will allow your application to gracefully handle external services being unavailable.

This triggers when your code returns an error, or whenever it is unable to complete based on a variety of health checks.

hystrix.Go("my_command", func() error {
    // talk to other services
    return nil
}, func(err error) error {
    // do this when services are down
    return nil
})

Waiting for output

Calling hystrix.Go is like launching a goroutine, except you receive a channel of errors you can choose to monitor.

output := make(chan bool, 1)
errors := hystrix.Go("my_command", func() error {
    // talk to other services
    output <- true
    return nil
}, nil)

select {
case out := <-output:
    // success
case err := <-errors:
    // failure
}

Synchronous API

Since calling a command and immediately waiting for it to finish is a common pattern, a synchronous API is available with the hystrix.Do function which returns a single error.

err := hystrix.Do("my_command", func() error {
    // talk to other services
    return nil
}, nil)

Configure settings

During application boot, you can call hystrix.ConfigureCommand() to tweak the settings for each command.

hystrix.ConfigureCommand("my_command", hystrix.CommandConfig{
    Timeout:               1000,
    MaxConcurrentRequests: 100,
    ErrorPercentThreshold: 25,
})

You can also use hystrix.Configure() which accepts a map[string]CommandConfig.

Enable dashboard metrics

In your main.go, register the event stream HTTP handler on a port and launch it in a goroutine. Once you configure turbine for your Hystrix Dashboard to start streaming events, your commands will automatically begin appearing.

hystrixStreamHandler := hystrix.NewStreamHandler()
hystrixStreamHandler.Start()
go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler)

Send circuit metrics to Statsd

c, err := plugins.InitializeStatsdCollector(&plugins.StatsdCollectorConfig{
    StatsdAddr: "localhost:8125",
    Prefix:     "myapp.hystrix",
})
if err != nil {
    log.Fatalf("could not initialize statsd client: %v", err)
}

metricCollector.Registry.Register(c.NewStatsdCollector)

FAQ

What happens if my run function panics? Does hystrix-go trigger the fallback?

No. hystrix-go does not use recover() so panics will kill the process like normal.

Build and Test

  • Install vagrant and VirtualBox
  • Clone the hystrix-go repository
  • Inside the hystrix-go directory, run vagrant up, then vagrant ssh
  • cd /go/src/github.com/afex/hystrix-go
  • go test ./...

Extension points exported contracts — how you extend this code

MetricCollector (Interface)
MetricCollector represents the contract that all collectors must fulfill to gather circuit statistics. Implementations o [4 …
hystrix/metric_collector/metric_collector.go
DatadogClient (Interface)
DatadogClient is the minimum interface needed by NewDatadogCollectorWithClient
plugins/datadog_collector.go

Core symbols most depended-on inside this repo

Sum
called by 60
hystrix/rolling/rolling.go
DefaultCollector
called by 59
hystrix/metrics.go
Flush
called by 25
hystrix/circuit.go
GoC
called by 24
hystrix/hystrix.go
ConfigureCommand
called by 20
hystrix/settings.go
Error
called by 20
hystrix/hystrix.go
GetCircuit
called by 18
hystrix/circuit.go
Increment
called by 14
hystrix/rolling/rolling.go

Shape

Method 87
Function 77
Struct 29
FuncType 4
Interface 3
TypeAlias 1

Languages

Go100%

Modules by API surface

hystrix/metric_collector/default_metric_collector.go17 symbols
hystrix/hystrix_test.go16 symbols
hystrix/eventstream.go16 symbols
hystrix/rolling/rolling_timing.go14 symbols
hystrix/hystrix.go14 symbols
hystrix/circuit.go12 symbols
plugins/statsd_collector.go11 symbols
hystrix/metrics.go11 symbols
hystrix/eventstream_test.go11 symbols
hystrix/rolling/rolling.go10 symbols
plugins/datadog_collector.go9 symbols
plugins/graphite_aggregator.go8 symbols

For agents

$ claude mcp add hystrix-go \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact