hystrix-go Documentation

repository·master·Indexed 26 days ago

https://github.com/afex/hystrix-go

A Go implementation of the Netflix Hystrix pattern for latency and fault tolerance. It provides tools to isolate remote service calls, prevent cascading failures, and implement graceful fallbacks using synchronous (hystrix.Do) and asynchronous (hystrix.Go) commands. Includes support for command configuration, Hystrix dashboard metrics, and Statsd integration.

Tokens
1.1K
Snippets
8
Records
10
Agent score
38%

What's inside hystrix-go

  1. Run the load testing integration app

    master

    The load testing integration app is used to measure the behavior of circuits under load. You can run the service using go run and optionally specify a Statsd host for metrics collection using the -statsd flag.

    go run service/main.go -statsd mystatsdhost:8125
  2. Send circuit metrics to Statsd

    master

    You can export Hystrix metrics to Statsd by initializing a Statsd collector and registering it with the metric collector registry.

    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)
  3. Enable Hystrix dashboard metrics

    master

    To visualize command metrics using a Hystrix Dashboard, register an event stream HTTP handler. You should run this handler in a goroutine on a dedicated port.

    hystrixStreamHandler := hystrix.NewStreamHandler()
    hystrixStreamHandler.Start()
    go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler)
  4. Configure Hystrix command settings

    master
    You can tune settings for specific commands during application boot using hystrix.ConfigureCommand. Alternatively, use hystrix.Configure to apply settings to multiple commands via a map[string]CommandConfig.
  5. Wait for output from an asynchronous Hystrix command

    master

    Since hystrix.Go returns an error channel, you can use a select statement to wait for either your application's output or a Hystrix error.

    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
    }
  6. Execute load tests using Apache Benchmark (ab)

    master

    Once the integration app is running, you can use Apache Benchmark (ab) to simulate high concurrency and request volume against the service (defaulting to http://localhost:8888/).

    ab -n 10000000 -c 10 http://localhost:8888/
  7. Define fallback behavior for Hystrix commands

    master

    To handle service outages gracefully, provide a second function to hystrix.Go. This fallback function is triggered when your primary logic returns an error or when Hystrix determines the service is unhealthy (e.g., due to timeouts or circuit breaker trips).

    hystrix.Go("my_command", func() error {
    	// talk to other services
    	return nil
    }, func(err error) error {
    	// do this when services are down
    	return nil
    })
  8. Execute code as an asynchronous Hystrix command

    master

    Use hystrix.Go to run application logic that relies on external systems in a separate goroutine. This function returns a channel of errors that you can monitor to determine if the command succeeded or failed.

    import "github.com/afex/hystrix-go/hystrix"
    
    errors := hystrix.Go("my_command", func() error {
    	// talk to other services
    	return nil
    }, nil)
  9. Execute a Hystrix command synchronously

    master

    If you need to wait for a command to finish immediately, use hystrix.Do. This function blocks until the command completes and returns a single error.

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