mirror of
https://github.com/ditkrg/idempotent-request.git
synced 2026-01-22 22:06:44 +00:00
Merge pull request #3 from qonto/unlock-other-http-status
Unlock redis for unsuccessful HTTP status
This commit is contained in:
commit
8643fda53e
@ -12,6 +12,10 @@ module IdempotentRequest
|
|||||||
setnx_with_expiration(lock_key(key), true)
|
setnx_with_expiration(lock_key(key), true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unlock(key)
|
||||||
|
redis.del(lock_key(key))
|
||||||
|
end
|
||||||
|
|
||||||
def read(key)
|
def read(key)
|
||||||
redis.get(namespaced_key(key))
|
redis.get(namespaced_key(key))
|
||||||
end
|
end
|
||||||
|
|||||||
@ -12,6 +12,10 @@ module IdempotentRequest
|
|||||||
storage.lock(key)
|
storage.lock(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unlock
|
||||||
|
storage.unlock(key)
|
||||||
|
end
|
||||||
|
|
||||||
def read
|
def read
|
||||||
status, headers, response = parse_data(storage.read(key)).values
|
status, headers, response = parse_data(storage.read(key)).values
|
||||||
|
|
||||||
@ -23,8 +27,13 @@ module IdempotentRequest
|
|||||||
def write(*data)
|
def write(*data)
|
||||||
status, headers, response = data
|
status, headers, response = data
|
||||||
response = response.body if response.respond_to?(:body)
|
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
|
data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
module IdempotentRequest
|
module IdempotentRequest
|
||||||
VERSION = "0.1.3"
|
VERSION = "0.1.4"
|
||||||
end
|
end
|
||||||
|
|||||||
@ -23,6 +23,21 @@ RSpec.describe IdempotentRequest::RedisStorage do
|
|||||||
end
|
end
|
||||||
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
|
describe '#write' do
|
||||||
let(:key) { 'key' }
|
let(:key) { 'key' }
|
||||||
let(:payload) { {} }
|
let(:payload) { {} }
|
||||||
|
|||||||
@ -36,6 +36,13 @@ RSpec.describe IdempotentRequest::RequestManager do
|
|||||||
end
|
end
|
||||||
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
|
describe '#read' do
|
||||||
context 'when there is no data' do
|
context 'when there is no data' do
|
||||||
it 'should return nil' do
|
it 'should return nil' do
|
||||||
@ -137,6 +144,11 @@ RSpec.describe IdempotentRequest::RequestManager do
|
|||||||
request_storage.write(*data)
|
request_storage.write(*data)
|
||||||
expect(memory_storage.read(request.key)).to be_nil
|
expect(memory_storage.read(request.key)).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should unlock stored key' do
|
||||||
|
expect(memory_storage).to receive(:unlock).with(request.key)
|
||||||
|
request_storage.write(*data)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,12 @@ module IdempotentRequest
|
|||||||
@memory[namespaced_key] = true
|
@memory[namespaced_key] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unlock(key)
|
||||||
|
namespaced_key = lock_key(key)
|
||||||
|
@memory.delete(namespaced_key)
|
||||||
|
@memory[namespaced_key]
|
||||||
|
end
|
||||||
|
|
||||||
def read(key)
|
def read(key)
|
||||||
@memory[key]
|
@memory[key]
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user