Merge pull request #3 from qonto/unlock-other-http-status

Unlock redis for unsuccessful HTTP status
This commit is contained in:
Dmytro Zakharov 2018-07-16 18:14:13 +02:00 committed by GitHub
commit 8643fda53e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -1,3 +1,3 @@
module IdempotentRequest module IdempotentRequest
VERSION = "0.1.3" VERSION = "0.1.4"
end end

View File

@ -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) { {} }

View File

@ -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

View File

@ -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