-
Notifications
You must be signed in to change notification settings - Fork 82
keep the key never overrides #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
e178edd
61760d6
19d48da
f988311
2665d2a
3a39083
96640db
3589314
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| end | ||
|
|
||
| if not exptime then | ||
|
|
@@ -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, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code readability is not good enough. |
||
| timeout = timeout or 5, | ||
| exptime = exptime, | ||
| step = step or 0.001, | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to test the case that
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]
MembPhis |
||
|
|
||
| --- 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I think we need a clone of this test case except
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the new test cases :) |
||
| lock2: 0, nil | ||
|
|
||
| --- no_error_log | ||
| [error] | ||
|
|
||
There was a problem hiding this comment.
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