idempotent-request/spec/support/memory_storage.rb
Dmytro Zakharov 17a2fed1f6 This commit is intended to fix an issue, when concurrent requests sent to an endpoint won't be protected by idempotency until the 1st request is finished.
This works adding a "lock" key in redis and returning 429 http error when concurrent requests are sent.
2018-06-28 16:09:02 +02:00

32 lines
473 B
Ruby

module IdempotentRequest
class MemoryStorage
def initialize
@memory = {}
end
def lock(key)
namespaced_key = lock_key(key)
return false if @memory.key?(namespaced_key)
@memory[namespaced_key] = true
end
def read(key)
@memory[key]
end
def write(key, payload)
@memory[key] = payload
end
def clear
@memory = {}
end
private
def lock_key(key)
"lock:#{key}"
end
end
end