Class: Overseer::TestingControl::Rails::IdempotencyStore

Inherits:
Object
  • Object
show all
Defined in:
lib/overseer/testing_control/rails/stores.rb

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initialize(limit:) ⇒ IdempotencyStore

Returns a new instance of IdempotencyStore.



16
17
18
19
20
# File 'lib/overseer/testing_control/rails/stores.rb', line 16

def initialize(limit:)
  @limit = limit
  @entries = {}
  @mutex = Mutex.new
end

Instance Method Details

#clear ⇒ Object



39
40
41
# File 'lib/overseer/testing_control/rails/stores.rb', line 39

def clear
  @mutex.synchronize { @entries.clear }
end

#fetch_or_store(key:, digest:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/overseer/testing_control/rails/stores.rb', line 22

def fetch_or_store(key:, digest:)
  @mutex.synchronize do
    existing = @entries[key]
    if existing
      raise IdempotencyConflict, 'Idempotency key was reused with different input' unless
        existing.digest == digest

      return deep_copy(existing.value)
    end
    raise StoreLimitExceeded, 'Idempotency storage reached its bounded limit' if @entries.length >= @limit

    value = yield
    @entries[key] = Entry.new(digest:, value: deep_copy(value))
    deep_copy(value)
  end
end