←back to thread

44 points seuros | 2 comments | | HN request time: 0.419s | source

BreakerMachines is a production-ready circuit breaker for Ruby/Rails with built-in async/fiber support, fallback chains, and rich monitoring. Unlike existing gems, it handles modern Ruby's fiber scheduler and avoids dangerous thread timeouts.
Show context
seuros ◴[] No.44479678[source]
Hey HN! I extracted BreakerMachines from my apps after seeing people dealing with cascading failures in production Rails apps.

Key features that set it apart:

- True async/fiber support (works with Falcon, async gems)

- Built-in fallback mechanism with chaining

- Thread-safe without dangerous Timeout.timeout

- Memory-efficient with WeakRef tracking

- Rich introspection and monitoring hooks

- Clean DSL that works with inheritance

With everyone adding AI/LLM APIs to their apps, circuit breakers are more critical than ever.

These APIs can be slow, flaky, or have outages - without protection, your app goes down with them.

The README shows patterns for graceful degradation when a service is down.

I explicitly avoided shipping Redis/DB adapters to keep it focused, the README shows how to implement your own in ~20 lines.

Would love feedback on the API design and any edge cases I might have missed!

I'm still going to add the parallel feature, i removed it because i need to test it in CI.

replies(1): >>44518617 #
1. byroot ◴[] No.44518617[source]
> Would love feedback on the API design

I'd suggest to drop the DSL, at the end of the day, a good old class with a constructor stored in a constant is much more more transparent:

    class PaymentService
      STRIPE_CIRCUIT = BreakerMachines::Circuit.new(
        threshold: { failures: 3, within: 1.minute },
        reset_after: 30.seconds,
        fallback: ->{ { error: "Payment queued for later" } }
      )

      def charge(amount)
        STRIPE_CIRCUIT.wrap do
          Stripe::Charge.create(amount: amount)
        end
      end
    end

Just my 2 cents though.
replies(1): >>44519428 #
2. seuros ◴[] No.44519428[source]
Merci pour la remarque !

Your 2 space credits are worth their weight in dilithium.

Funny thing is the gem originally started exactly like that, pure explicit constructors. Then I wrapped it with the DSL sugar but the explicit approach never lost its capability. Both paths lead to the same destination.

I actually loved this suggestion so much that I added a whole section called "Captain Byroot's Guide to Explicit Circuit Construction" to the docs:

https://github.com/seuros/breaker_machines/blob/master/docs/....

The resistance appreciates your feedback.