Class: Idempo::MemoryLock

Inherits:
Object
  • Object
show all
Defined in:
lib/idempo/memory_lock.rb

Overview

A memory lock prevents multiple requests with the same request fingerprint from running concurrently

Instance Method Summary collapse

Constructor Details

#initializeMemoryLock

Returns a new instance of MemoryLock.



4
5
6
7
# File 'lib/idempo/memory_lock.rb', line 4

def initialize
  @requests_in_flight_mutex = Mutex.new
  @in_progress = Set.new
end

Instance Method Details

#with(request_key) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/idempo/memory_lock.rb', line 9

def with(request_key)
  @requests_in_flight_mutex.synchronize do
    if @in_progress.include?(request_key)
      raise Idempo::ConcurrentRequest
    else
      @in_progress << request_key
    end
  end
  yield
ensure
  @requests_in_flight_mutex.synchronize { @in_progress.delete(request_key) }
end