mirror of
https://github.com/ditkrg/idempotent-request.git
synced 2026-01-22 13:56:45 +00:00
Unlock redis for unsuccessful HTTP status
This commit is contained in:
parent
c0374cf032
commit
505395d3a5
@ -12,6 +12,10 @@ module IdempotentRequest
|
||||
setnx_with_expiration(lock_key(key), true)
|
||||
end
|
||||
|
||||
def unlock(key)
|
||||
redis.del(lock_key(key))
|
||||
end
|
||||
|
||||
def read(key)
|
||||
redis.get(namespaced_key(key))
|
||||
end
|
||||
|
||||
@ -12,6 +12,10 @@ module IdempotentRequest
|
||||
storage.lock(key)
|
||||
end
|
||||
|
||||
def unlock
|
||||
storage.unlock(key)
|
||||
end
|
||||
|
||||
def read
|
||||
status, headers, response = parse_data(storage.read(key)).values
|
||||
|
||||
@ -23,8 +27,13 @@ module IdempotentRequest
|
||||
def write(*data)
|
||||
status, headers, response = data
|
||||
response = response.body if response.respond_to?(:body)
|
||||
return data unless (200..226).include?(status)
|
||||
storage.write(key, payload(status, headers, response))
|
||||
|
||||
if (200..226).cover?(status)
|
||||
storage.write(key, payload(status, headers, response))
|
||||
else
|
||||
unlock
|
||||
end
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
|
||||
@ -23,6 +23,21 @@ RSpec.describe IdempotentRequest::RedisStorage do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#unlock' do
|
||||
let(:key) { 'key' }
|
||||
let(:lock_key) { "#{namespace}:lock:#{key}" }
|
||||
|
||||
before { redis_storage.lock(key) }
|
||||
|
||||
it 'should unlock' do
|
||||
expect(redis).to receive(:del).with(lock_key).and_call_original
|
||||
|
||||
redis_storage.unlock(key)
|
||||
|
||||
expect(redis_storage.read(key)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe '#write' do
|
||||
let(:key) { 'key' }
|
||||
let(:payload) { {} }
|
||||
|
||||
@ -36,6 +36,13 @@ RSpec.describe IdempotentRequest::RequestManager do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#unlock' do
|
||||
it 'delegates to storage service' do
|
||||
expect(memory_storage).to receive(:unlock).with(request.key)
|
||||
request_storage.unlock
|
||||
end
|
||||
end
|
||||
|
||||
describe '#read' do
|
||||
context 'when there is no data' do
|
||||
it 'should return nil' do
|
||||
@ -137,6 +144,11 @@ RSpec.describe IdempotentRequest::RequestManager do
|
||||
request_storage.write(*data)
|
||||
expect(memory_storage.read(request.key)).to be_nil
|
||||
end
|
||||
|
||||
it 'should unlock stored key' do
|
||||
expect(memory_storage).to receive(:unlock).with(request.key)
|
||||
request_storage.write(*data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -10,6 +10,12 @@ module IdempotentRequest
|
||||
@memory[namespaced_key] = true
|
||||
end
|
||||
|
||||
def unlock(key)
|
||||
namespaced_key = lock_key(key)
|
||||
@memory.delete(namespaced_key)
|
||||
@memory[namespaced_key]
|
||||
end
|
||||
|
||||
def read(key)
|
||||
@memory[key]
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user