Hystrix in Golang

In Go, the thread-hogging of requests of the Java/Tomcat world is not really a concern, since the web frameworks use goroutine(s) to service requests rather than dedicated threads. Goroutines are much more lightweight than threads, thus the problem solved by the Bulkhead Pattern is less of an issue. But, still the Hystrix solution is vital to enable fail-fast and circuit breaker behavior.

The most-used Golang library for Hystrix at the time of writing is https://github.com/afex/hystrix-go/. The following is the 'hello world' snippet of using the library:

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

errors := hystrix.Go("a_command", func() error {
// talk to other dependent services
// return the error if one occurs
return nil
}, func(err error) error {
// do fallback action here
return nil
})

Calling hystrix.Go is like launching a goroutine, and it returns a channel or error (errors) in the preceding snippet. Here, the first closure function is the Command Object, and this performs the actual interactions with the dependent service. If there is an error, the second closure function is called with the error code returned by the first command function.

Values can be returned from the Command using channels, like so:

out := make(chan string, 1)
errors := hystrix.Go("a_command", func() error {
// talk to other dependent services
// return the error if one occurs
output <- "a good response"
return nil
}, func(err error) error {
// do fallback action
output <- "fallback here"
return nil
})

Then, the client code can do a select comprehension over both the output and the errors, like so:

select {   
    case ret := <-out:   
    //   success   
    //   process the return value    
case err := <-errors:   
    //   failure   
    //   handle failure   
}   

You can configure settings, such as timeout and maximum concurrent requests for each command, using a separate API, like so:

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

The CommandConfig struct is defined as follows:

type   CommandConfig struct {   
      Timeout                 int `json:"timeout"`   
      MaxConcurrentRequests   int `json:"max_concurrent_requests"`   
      RequestVolumeThreshold  int `json:"request_volume_threshold"`   
      SleepWindow             int `json:"sleep_window"`   
      ErrorPercentThreshold   int `json:"error_percent_threshold"`   
}   

The various tuneables are as follows:

Tunables Description

Timeout

How long to wait for a command to complete in milliseconds.

MaxConcurrentRequests

How many instances of this command can run at the same time.

RequestVolumeThreshold

The minimum number of requests that must happen before a circuit can be tripped due to health.

SleepWindow

The time to wait after a circuit opens before going to the half-open state (to test for recovery).

ErrorPercentThreshold

The percentage threshold of the number of errors to cause the circuit to open.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset