Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
7 changes: 5 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,8 @@ function _M.lock(self, key)
return nil, "locked"
end
local exptime = self.exptime
local ok, err = dict:add(key, true, exptime)
local dict_add = self.dict_add
local ok, err = dict_add(dict, key, true, exptime)
if ok then
cdata.key_id = ref_obj(key)
if not shdict_mt then
Expand All @@ -154,7 +157,7 @@ function _M.lock(self, key)
elapsed = elapsed + step
timeout = timeout - step

local ok, err = dict:add(key, true, exptime)
local ok, err = dict_add(dict, key, true, exptime)
if ok then
cdata.key_id = ref_obj(key)
if not shdict_mt then
Expand Down
70 changes: 70 additions & 0 deletions t/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,73 @@ 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]



=== TEST 15: use safe_add, dont have enough memory
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua '
local lock = require "resty.lock"
local cache = ngx.shared.cache_locks

cache:flush_all()
function full_dict( dict )
for i = 1, 10000 do
local ok, err = dict:safe_set(string.rep("2", 2) .. i, string.rep("2", 5) .. i)
if not ok then
return
end
end
end
full_dict(cache)

local lock1 = lock:new("cache_locks", {safe_add = true, timeout = 0})
local elapsed, err = lock1:lock("foo")
ngx.say("lock1: ", elapsed, ", ", err)

local lock2 = lock:new("cache_locks", {timeout = 0})
local elapsed, err = lock2:lock("foo")
ngx.say("lock2: ", elapsed, ", ", err)
';
}
--- request
GET /t
--- response_body
lock1: nil, no memory
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think we need a clone of this test case except safe_add = false to ensure there is a difference.

Copy link
Member

Choose a reason for hiding this comment

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

In addition, we need another clone of this test case without explicitly setting the safe_add option to ensure that the default behaviour is safe_add = false.

Copy link
Author

Choose a reason for hiding this comment

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

add the new test cases :)

lock2: 0, nil

--- no_error_log
[error]