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
|
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
|
## TODO
|
||||||
- more race condition tests
|
- more race condition tests
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ module Suo
|
|||||||
acquisition_timeout: 0.1,
|
acquisition_timeout: 0.1,
|
||||||
acquisition_delay: 0.01,
|
acquisition_delay: 0.01,
|
||||||
stale_lock_expiration: 3600,
|
stale_lock_expiration: 3600,
|
||||||
resources: 1
|
resources: 1,
|
||||||
|
ttl: 60,
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
BLANK_STR = "".freeze
|
BLANK_STR = "".freeze
|
||||||
@@ -64,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
|
||||||
|
|
||||||
@@ -81,7 +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)
|
break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?)
|
||||||
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
|
||||||
|
|||||||
@@ -16,8 +16,12 @@ module Suo
|
|||||||
@client.get_cas(@key)
|
@client.get_cas(@key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(newval, cas)
|
def set(newval, cas, expire: false)
|
||||||
@client.set_cas(@key, newval, cas)
|
if expire
|
||||||
|
@client.set_cas(@key, newval, cas, @options[:ttl])
|
||||||
|
else
|
||||||
|
@client.set_cas(@key, newval, cas)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initial_set(val = BLANK_STR)
|
def initial_set(val = BLANK_STR)
|
||||||
|
|||||||
@@ -18,9 +18,13 @@ module Suo
|
|||||||
[@client.get(@key), nil]
|
[@client.get(@key), nil]
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(newval, _)
|
def set(newval, _, expire: false)
|
||||||
ret = @client.multi do |multi|
|
ret = @client.multi do |multi|
|
||||||
multi.set(@key, newval)
|
if expire
|
||||||
|
multi.setex(@key, @options[:ttl], newval)
|
||||||
|
else
|
||||||
|
multi.set(@key, newval)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ret && ret[0] == OK_STR
|
ret && ret[0] == OK_STR
|
||||||
|
|||||||
Reference in New Issue
Block a user