Class: Berater::Middleware::FailOpen
- Inherits:
-
Object
- Object
- Berater::Middleware::FailOpen
- Defined in:
- lib/berater/middleware/fail_open.rb
Constant Summary collapse
- ERRORS =
Set.new [ Redis::BaseConnectionError, SystemCallError, # openssl Timeout::Error, # connection pool (OpenSSL::OpenSSLError if defined?(OpenSSL)), ].compact
Instance Method Summary collapse
- #call(**opts) ⇒ Object
-
#initialize(errors: nil, on_fail: nil) ⇒ FailOpen
constructor
A new instance of FailOpen.
Constructor Details
Instance Method Details
#call(**opts) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/berater/middleware/fail_open.rb', line 18 def call(*, **opts) yield.tap do |lock| return lock unless lock&.is_a?(Berater::Lock) # wrap lock.release so it fails open # save reference to original function release_fn = lock.method(:release) # make bound variables accessible to block errors = @errors on_fail = @on_fail lock.define_singleton_method(:release) do release_fn.call rescue *errors => e on_fail&.call(e) false end end rescue *@errors => e @on_fail&.call(e) # fail open by faking a lock Berater::Lock.new(opts[:capacity], -1) end |