move to expire only when all locks are released

This commit is contained in:
Ian Remillard
2018-10-01 10:35:30 -07:00
parent 6be3a5bdda
commit 1022a6f9d3
4 changed files with 10 additions and 23 deletions

View File

@@ -75,18 +75,11 @@ end
### Time To Live ### Time To Live
```ruby ```ruby
Suo::Client::Redis.new("bar_resource", ttl: 10) #ttl in seconds Suo::Client::Redis.new("bar_resource", ttl: 60) #ttl in seconds
``` ```
All locks are expired and the lock key is removed after the specified `ttl` time runs out. This is counted as the time since any resource for a given key was locked or unlocked. A key representing a set of lockable resources is removed once the last resource lock is released and the `ttl` time runs out. When another lock is acquired and the key has been removed the key has to be recreated.
### Lock Release Removes Key
```ruby
Suo::Client::Redis.new("bar_resource", lock_release_removes_key: true)
```
By default, a key representing a set of resource locks is persisted indefinitely even when no resources are currently locked. When `lock_release_removes_key` is set to `true`, the key is removed when the last resource lock is released. This also means that when another lock is acquired the key has to be recreated.
## TODO ## TODO
- more race condition tests - more race condition tests

View File

@@ -6,8 +6,7 @@ module Suo
acquisition_delay: 0.01, acquisition_delay: 0.01,
stale_lock_expiration: 3600, stale_lock_expiration: 3600,
resources: 1, resources: 1,
lock_release_removes_key: false, ttl: 60,
ttl: nil,
}.freeze }.freeze
BLANK_STR = "".freeze BLANK_STR = "".freeze
@@ -66,7 +65,7 @@ module Suo
refresh_lock(cleared_locks, token) refresh_lock(cleared_locks, token)
break if set(serialize_locks(cleared_locks), cas) break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?)
end end
end end
@@ -83,12 +82,7 @@ module Suo
acquisition_lock = remove_lock(cleared_locks, token) acquisition_lock = remove_lock(cleared_locks, token)
break unless acquisition_lock break unless acquisition_lock
break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?)
if @options[:lock_release_removes_key] && cleared_locks.empty?
break if clear
else
break if set(serialize_locks(cleared_locks), cas)
end
end end
rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions
# ignore - assume success due to optimistic locking # ignore - assume success due to optimistic locking

View File

@@ -16,9 +16,9 @@ module Suo
@client.get_cas(@key) @client.get_cas(@key)
end end
def set(newval, cas) def set(newval, cas, expire:)
if @options[:ttl] if expire
@client.set_cas(@key, newval, cas, {ttl: @options[:ttl]}) @client.set_cas(@key, newval, cas, @options[:ttl])
else else
@client.set_cas(@key, newval, cas) @client.set_cas(@key, newval, cas)
end end

View File

@@ -18,9 +18,9 @@ module Suo
[@client.get(@key), nil] [@client.get(@key), nil]
end end
def set(newval, _) def set(newval, _, expire:)
ret = @client.multi do |multi| ret = @client.multi do |multi|
if @options[:ttl] if expire
multi.setex(@key, @options[:ttl], newval) multi.setex(@key, @options[:ttl], newval)
else else
multi.set(@key, newval) multi.set(@key, newval)