Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/resty/lock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function _M.new(_, dict_name, opts)
step = opts.step
ratio = opts.ratio
max_step = opts.max_step
safe_add = opts.safe_add

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

safe_add is NOT local var

end

if not exptime then
Expand All @@ -107,6 +108,7 @@ function _M.new(_, dict_name, opts)
local self = {
cdata = cdata,
dict = dict,
dict_add = safe_add and dict.safe_add or dict.add,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code readability is not good enough.
how about this:
local dict_add = dict.add
if safe_add then
dict_add = dict.safe_add
end

timeout = timeout or 5,
exptime = exptime,
step = step or 0.001,
Expand All @@ -128,7 +130,7 @@ function _M.lock(self, key)
return nil, "locked"
end
local exptime = self.exptime
local ok, err = dict:add(key, true, exptime)
local ok, err = self.dict_add(dict, key, true, exptime)
if ok then
cdata.key_id = ref_obj(key)
if not shdict_mt then
Expand All @@ -154,7 +156,7 @@ function _M.lock(self, key)
elapsed = elapsed + step
timeout = timeout - step

local ok, err = dict:add(key, true, exptime)
local ok, err = self.dict_add(dict, key, true, exptime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better cache self.dict_add in a local variable since it may get called for multiple times later on in this method.

if ok then
cdata.key_id = ref_obj(key)
if not shdict_mt then
Expand Down
31 changes: 31 additions & 0 deletions t/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,34 @@ lock 2: unlock: nil, unlocked
--- no_error_log
[error]



=== TEST 14: serial lock and unlock with safe_add
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua '
local lock = require "resty.lock"
for i = 1, 2 do
local lock = lock:new("cache_locks", {safe_add = true})
local elapsed, err = lock:lock("foo")
ngx.say("lock: ", elapsed, ", ", err)
local ok, err = lock:unlock()
if not ok then
ngx.say("failed to unlock: ", err)
end
ngx.say("unlock: ", ok)
end
';
}
--- request
GET /t
--- response_body
lock: 0, nil
unlock: 1
lock: 0, nil
unlock: 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to test the case that safe_add actually returned the "no memory" error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the new test case . ^_^

On Sun, Dec 6, 2015 at 12:45 PM, Yichun Zhang [email protected]
wrote:

In t/sanity.t
#6 (comment):

  •            ngx.say("lock: ", elapsed, ", ", err)
    
  •            local ok, err = lock:unlock()
    
  •            if not ok then
    
  •                ngx.say("failed to unlock: ", err)
    
  •            end
    
  •            ngx.say("unlock: ", ok)
    
  •        end
    
  •    ';
    
  • }
    +--- request
    +GET /t
    +--- response_body
    +lock: 0, nil
    +unlock: 1
    +lock: 0, nil
    +unlock: 1

Need to test the case that safe_add actually returned the "no memory"
error.


Reply to this email directly or view it on GitHub
https://github.com/openresty/lua-resty-lock/pull/6/files#r46766560.

MembPhis
My github: https://github.com/membphis
Our Book: OpenResty Best Practices
https://www.gitbook.com/book/moonbingbing/openresty-best-practices


--- no_error_log
[error]