mirror of
https://github.com/dkam/suo.git
synced 2025-01-29 07:42:43 +00:00
Merge pull request #6 from GandalftheGUI/ian_remillard/remove_keys_after_last_lock_released
Add 'Time To Live' to mitigate potential memory leak
This commit is contained in:
@@ -72,6 +72,15 @@ suo.lock do |token|
|
||||
end
|
||||
```
|
||||
|
||||
### Time To Live
|
||||
|
||||
```ruby
|
||||
Suo::Client::Redis.new("bar_resource", ttl: 60) #ttl in seconds
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
|
||||
## TODO
|
||||
- more race condition tests
|
||||
|
||||
|
||||
@@ -5,7 +5,8 @@ module Suo
|
||||
acquisition_timeout: 0.1,
|
||||
acquisition_delay: 0.01,
|
||||
stale_lock_expiration: 3600,
|
||||
resources: 1
|
||||
resources: 1,
|
||||
ttl: 60,
|
||||
}.freeze
|
||||
|
||||
BLANK_STR = "".freeze
|
||||
@@ -64,7 +65,7 @@ module Suo
|
||||
|
||||
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
|
||||
|
||||
@@ -81,7 +82,7 @@ module Suo
|
||||
acquisition_lock = remove_lock(cleared_locks, token)
|
||||
|
||||
break unless acquisition_lock
|
||||
break if set(serialize_locks(cleared_locks), cas)
|
||||
break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?)
|
||||
end
|
||||
rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions
|
||||
# ignore - assume success due to optimistic locking
|
||||
|
||||
@@ -16,9 +16,13 @@ module Suo
|
||||
@client.get_cas(@key)
|
||||
end
|
||||
|
||||
def set(newval, cas)
|
||||
def set(newval, cas, expire: false)
|
||||
if expire
|
||||
@client.set_cas(@key, newval, cas, @options[:ttl])
|
||||
else
|
||||
@client.set_cas(@key, newval, cas)
|
||||
end
|
||||
end
|
||||
|
||||
def initial_set(val = BLANK_STR)
|
||||
@client.set(@key, val)
|
||||
|
||||
@@ -18,10 +18,14 @@ module Suo
|
||||
[@client.get(@key), nil]
|
||||
end
|
||||
|
||||
def set(newval, _)
|
||||
def set(newval, _, expire: false)
|
||||
ret = @client.multi do |multi|
|
||||
if expire
|
||||
multi.setex(@key, @options[:ttl], newval)
|
||||
else
|
||||
multi.set(@key, newval)
|
||||
end
|
||||
end
|
||||
|
||||
ret && ret[0] == OK_STR
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user