Class: Stoplight::Infrastructure::FailSafe::Storage::RecoveryLock Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stoplight/infrastructure/fail_safe/storage/recovery_lock.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A wrapper around a store that provides fail-safe mechanisms using a circuit breaker. It ensures that operations on the store can gracefully handle failures by falling back to default values when necessary.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primary_store:, error_notifier:, failover_store:, circuit_breaker:) ⇒ RecoveryLock

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RecoveryLock.



21
22
23
24
25
26
# File 'lib/stoplight/infrastructure/fail_safe/storage/recovery_lock.rb', line 21

def initialize(primary_store:, error_notifier:, failover_store:, circuit_breaker:)
  @primary_store = primary_store
  @error_notifier = error_notifier
  @failover_store = failover_store
  @circuit_breaker = circuit_breaker
end

Instance Attribute Details

#error_notifierObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
# File 'lib/stoplight/infrastructure/fail_safe/storage/recovery_lock.rb', line 17

def error_notifier
  @error_notifier
end

#failover_storeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The fallback store used when the primary fails.



19
20
21
# File 'lib/stoplight/infrastructure/fail_safe/storage/recovery_lock.rb', line 19

def failover_store
  @failover_store
end

#primary_storeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The underlying primary store being used



16
17
18
# File 'lib/stoplight/infrastructure/fail_safe/storage/recovery_lock.rb', line 16

def primary_store
  @primary_store
end

Instance Method Details

#acquire_lockObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
31
32
33
34
35
36
# File 'lib/stoplight/infrastructure/fail_safe/storage/recovery_lock.rb', line 28

def acquire_lock
  fallback = ->(error) {
    error_notifier.call(error) if error
    wrap_token(:failover, failover_store.acquire_lock)
  }
  circuit_breaker.run(fallback) do
    wrap_token(:primary, primary_store.acquire_lock)
  end
end

#release_lock(recovery_lock_token) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Routes release to correct store based on token type. Redis tokens release via primary (with error notification on failure). Memory tokens release via failover directly.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stoplight/infrastructure/fail_safe/storage/recovery_lock.rb', line 42

def release_lock(recovery_lock_token)
  case recovery_lock_token.origin
  in :primary
    fallback = ->(error) {
      error_notifier.call(error) if error
    }

    circuit_breaker.run(fallback) do
      primary_store.release_lock(recovery_lock_token.underlying_token)
    end
  in :failover
    failover_store.release_lock(recovery_lock_token.underlying_token)
  end
end