Permalink
Browse files

feature: added new method expire() that can change the TTL of the loc…

…k being held.

Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
  • Loading branch information...
dndx authored and agentzh committed May 25, 2017
1 parent 20cd49f commit d06eb95036f5deab8bb155d8f3ce33d083876841
Showing with 113 additions and 0 deletions.
  1. +17 −0 README.markdown
  2. +23 −0 lib/resty/lock.lua
  3. +73 −0 t/sanity.t
View
@@ -14,6 +14,7 @@ Table of Contents
* [new](#new)
* [lock](#lock)
* [unlock](#unlock)
* [expire](#expire)
* [For Multiple Lua Light Threads](#for-multiple-lua-light-threads)
* [For Cache Locks](#for-cache-locks)
* [Prerequisites](#prerequisites)
@@ -162,6 +163,22 @@ Returns `1` on success. Returns `nil` and a string describing the error otherwis
If you call `unlock` when no lock is currently held, the error "unlocked" will be returned.
expire
------
`syntax: ok, err = obj:expire(timeout)`
Sets the TTL of the lock held by the current `resty.lock` object instance. This will reset the
timeout of the lock to `timeout` seconds if it is given, otherwise the `timeout` provided while
calling [new](#new) will be used.
Note that the `timeout` supplied inside this function is independent from the `timeout` provided while
calling [new](#new). Calling `expire()` will not change the `timeout` value specified inside [new](#new)
and subsequent `expire(nil)` call will still use the `timeout` number from [new](#new).
Returns `true` on success. Returns `nil` and a string describing the error otherwise.
If you call `expire` when no lock is currently held, the error "unlocked" will be returned.
[Back to TOC](#table-of-contents)
For Multiple Lua Light Threads
View
@@ -135,6 +135,7 @@ function _M.lock(self, key)
local ok, err = dict:add(key, true, exptime)
if ok then
cdata.key_id = ref_obj(key)
self.key = key
return 0
end
if err ~= "exists" then
@@ -154,6 +155,7 @@ function _M.lock(self, key)
local ok, err = dict:add(key, true, exptime)
if ok then
cdata.key_id = ref_obj(key)
self.key = key
return elapsed
end
@@ -193,4 +195,25 @@ function _M.unlock(self)
end
function _M.expire(self, time)
local dict = self.dict
local cdata = self.cdata
local key_id = tonumber(cdata.key_id)
if key_id <= 0 then
return nil, "unlocked"
end
if not time then
time = self.exptime
end
local ok, err = dict:replace(self.key, true, time)
if not ok then
return nil, err
end
return true
end
return _M
View
@@ -468,3 +468,76 @@ lock 2: unlock: nil, unlocked
--- no_error_log
[error]
=== TEST 13: expire()
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local lock = require "resty.lock"
for i = 1, 2 do
local lock1 = lock:new("cache_locks", { timeout = 0, exptime = 0.1 })
local lock2 = lock:new("cache_locks", { timeout = 0, exptime = 0.1 })
local exp, err = lock1:expire()
ngx.say("lock 1: expire: ", exp, ", ", err)
local elapsed, err = lock1:lock("foo")
ngx.say("lock 1: lock: ", elapsed, ", ", err)
ngx.sleep(0.06)
local exp, err = lock1:expire()
ngx.say("lock 1: expire: ", exp, ", ", err)
ngx.sleep(0.06)
local elapsed, err = lock2:lock("foo")
ngx.say("lock 2: lock: ", elapsed, ", ", err)
local exp, err = lock1:expire(0.2)
ngx.say("lock 1: expire: ", exp, ", ", err)
ngx.sleep(0.15)
local elapsed, err = lock2:lock("foo")
ngx.say("lock 2: lock: ", elapsed, ", ", err)
ngx.sleep(0.1)
local elapsed, err = lock2:lock("foo")
ngx.say("lock 2: lock: ", elapsed, ", ", err)
local ok, err = lock2:unlock()
ngx.say("lock 2: unlock: ", ok, ", ", err)
local exp, err = lock2:expire(0.2)
ngx.say("lock 2: expire: ", exp, ", ", err)
end
}
}
--- request
GET /t
--- response_body
lock 1: expire: nil, unlocked
lock 1: lock: 0, nil
lock 1: expire: true, nil
lock 2: lock: nil, timeout
lock 1: expire: true, nil
lock 2: lock: nil, timeout
lock 2: lock: 0, nil
lock 2: unlock: 1, nil
lock 2: expire: nil, unlocked
lock 1: expire: nil, unlocked
lock 1: lock: 0, nil
lock 1: expire: true, nil
lock 2: lock: nil, timeout
lock 1: expire: true, nil
lock 2: lock: nil, timeout
lock 2: lock: 0, nil
lock 2: unlock: 1, nil
lock 2: expire: nil, unlocked
--- no_error_log
[error]

0 comments on commit d06eb95

Please sign in to comment.