The Hystrix plug-in uses semaphore to handle requests and is the core implementation of the Soul Gateway’s fusing of traffic. Here’s how Hystrix works:

  1. Each invocation creates a HystrixCommand or HystrixObservableCommand object, which represents a request to a dependent service.
    • HystrixCommand is primarily used for calls that return only one result.
    • HystrixObservableCommand is used primarily for calls that may return more than one result.
    • Execute () for synchronous calls or queue() for asynchronous calls.
    • Observe () subscribes to an Observable, which represents the result returned by the dependent service, and gets a copy of that Observable. ToObservable () returns an Observable, and if we subscribe to it, the corresponding command is executed and returns the result
  2. If the command has request caching enabled and request and response exist in the cache, the result is immediately returned from the cache.
  3. If the circuit-breaker is on, skip to Step 8 and perform the downgrade strategy. If not, go to Step 5.
  4. Determine if the thread pool/semaphore is full, if it is, go to step 8, otherwise continue.
  5. Call HystrixCommand. The run () or HystrixObservableCommand. The construct () :
    • Hystrixcommand-run () : Returns a response or throws an exception.
    • HystrixObservableCommand. The construct () : returns a observables object, you can get more results or send an onError notification
    • If the run() or construct() call times out, the executing thread will throw a TimeoutException (a separate timer thread will throw a TimeoutException if the command itself is not running in its own thread). Perform Step 8 to invoke the Fallback degradation mechanism. Values returned from the run() or construct() methods are also discarded.
  6. Calculate the status of fuses and report all operating status (success, failure, rejection, timeout) to fuses for statistics to determine the status of fuses.
  7. GetFallback () fallback logic.
    • The getFallback call is triggered in four ways:
      1. When the construct () or the run () method to throw a non HystrixBadRequestException anomalies.
      2. Command Execution Timeout
      3. The fuse turns on the interception call
      4. Whether the thread pool/queue/semaphore is full
    • A Command that does not implement getFallback will throw an exception directly
    • Fallback returns if the fallback logic call succeeds
    • The degraded logical call failed to throw an exception
  8. Returns an Observable and transforms the desired object based on the method performed in Step 2.

conclusion

Hystrix is a complex process, and in the next section we’ll use the Hystrix plug-in wrapped in Soul to fuse and degrade.