Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Circuit Breakers em Ruby

Lucas Mazza
November 19, 2016

Circuit Breakers em Ruby

Lucas Mazza

November 19, 2016

Transcript

70 slides
  1. ❓

  2. “You wrap a protected function call in a circuit breaker

    object, which monitors for failures.” http://martinfowler.com/bliki/CircuitBreaker.html
  3. require 'resilient/circuit_breaker' circuit_breaker = Resilient::CircuitBreaker.get('acme-co-api') if circuit_breaker.allow_request? begin # API

    Call circuit_breaker.success rescue => boom circuit_breaker.failure # do fallback end else # do fallback end
  4. circuit_breaker = Resilient::CircuitBreaker.get('example', { # at what percentage of errors

    should we open the circuit error_threshold_percentage: 50, # do not try request again for 5 seconds sleep_window_seconds: 5, # do not open circuit until at least 5 requests have happened request_volume_threshold: 5, }) # etc etc etc
  5. “Semian is not a trivial library to understand, introduces complexity

    and thus should be introduced with care.” https://github.com/shopify/semian#do-i-need-semian
  6. “It is paramount that you understand Semian before including it

    in production as you may otherwise be surprised by its behaviour.” https://github.com/shopify/semian#do-i-need-semian
  7. light = Stoplight('acme-co-api') { do_api_call } .with_fallback { do_fallback }

    .with_threshold(3) # => Stoplight::Light light.color #=> 'green', 'yellow' ou 'red' light.run
  8. require 'redis' redis = Redis.new data_store = Stoplight::DataStore::Redis.new(redis) Stoplight::Light.default_data_store =

    data_store slack = Slack::Notifier.new('http://www.example.com/webhook-url') notifier = Stoplight::Notifier::Slack.new(slack) Stoplight::Light.default_notifiers += [notifier] notifier = Stoplight::Notifier::Logger.new(Rails.logger) Stoplight::Light.default_notifiers += [notifier]
  9. class CircuitBreaker def initialize(client, name) @client = client @name =

    name end def method_missing(method, *args, &block) Stoplight(@name) { @client.public_send(method, *args, &block) }.run end end
  10. gh_client = GitHub::Client.new('acme-co-oauth2-token') client = CircuitBreaker.new(gh_client, 'client-acme-co') # Requests feito

    dentro do Circuit Breaker client.repo('plataformatec/devise') client.repo('plataformatec/simple_form') client.repo('plataformatec/faraday-http-cache')

More Decks by Lucas Mazza

Other Decks in Programming