Skip to content

Add support for shdict_get flag comparison #265

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ install:
- git clone https://github.com/openresty/mockeagain.git
- git clone https://github.com/openresty/test-nginx.git
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git
- git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
- git clone https://github.com/webcore-no/lua-nginx-module.git ../lua-nginx-module
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
- git clone https://github.com/openresty/memc-nginx-module.git ../memc-nginx-module
- git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module
- git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
- git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
- git clone https://github.com/webcore-no/lua-resty-core.git ../lua-resty-core

script:
- sudo iptables -I OUTPUT 1 -p udp --dport 10086 -j REJECT
Expand Down
10 changes: 9 additions & 1 deletion src/ngx_stream_lua_shdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ int
ngx_stream_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key,
size_t key_len, int *value_type, u_char **str_value_buf,
size_t *str_value_len, double *num_value, int *user_flags,
int get_stale, int *is_stale, char **err)
int *user_flags_neq, int get_stale, int *is_stale, char **err)
{
ngx_str_t name;
uint32_t hash;
Expand Down Expand Up @@ -1537,6 +1537,14 @@ ngx_stream_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key,
return NGX_OK;
}

if (*user_flags_neq && *user_flags == (int) sd->user_flags) {
*is_stale = (rc == NGX_DONE);
*user_flags_neq = 0;
ngx_shmtx_unlock(&ctx->shpool->mutex);
*value_type = LUA_TNIL;
return NGX_OK;
}

/* rc == NGX_OK || (rc == NGX_DONE && get_stale) */

*value_type = sd->value_type;
Expand Down
259 changes: 259 additions & 0 deletions t/167-shdict-neq-flags.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua::Stream 'no_plan';

run_tests();

__DATA__

=== TEST 1: flag eq
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0, 1)
local val = dogs:get("Bernese", 1)
ngx.say(val, " ", type(val))
}
--- stream_response
nil nil
--- no_error_log
[error]



=== TEST 2: fleq neq
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0, 1)
local val = dogs:get("Bernese", 2)
ngx.say(val, " " , type(val))
}
--- stream_response
42 number
--- no_error_log
[error]



=== TEST 3: set with no flag
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0)
local val = dogs:get("Bernese", 2)
ngx.say(val, " " , type(val))
}
--- stream_response
42 number
--- no_error_log
[error]



=== TEST 4: get with no flag
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0, 1)
local val = dogs:get("Bernese")
ngx.say(val, " " , type(val))
}
--- stream_response
42 number
--- no_error_log
[error]



=== TEST 5: set and get with no flag
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0)
local val = dogs:get("Bernese")
ngx.say(val, " " , type(val))
}
--- stream_response
42 number
--- no_error_log
[error]



=== TEST 6: set no flag, and read with 0 flag
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42)
local val = dogs:get("Bernese", 0)
ngx.say(val, " " , type(val))
}
--- stream_response
nil nil
--- no_error_log
[error]


=== TEST 7: flags_match is true
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0, 1)
local val, err, flags_match = dogs:get("Bernese", 1)

ngx.say(val, " ", type(val), " : ",
err, " ", type(err), " : ",
flags_match, " ", type(flags_match))
}
--- stream_response
nil nil : nil nil : true boolean
--- no_error_log
[error]



=== TEST 8: flags_match is nil
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
local val, err, flags_match = dogs:get("Bernese", 3)
ngx.say(flags_match, " ", type(flags_match))
}
--- stream_response
nil nil
--- no_error_log
[error]



=== TEST 9: get when flag is not number
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0, 1)
local val = dogs:get("Bernese", {})
}
--- error_log
cannot convert 'table' to 'int'


=== TEST 10: flag eq stale
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0.01, 1)
ngx.sleep(0.02)
local val = dogs:get_stale("Bernese", 1)
ngx.say(val, " ", type(val))
}
--- stream_response
nil nil
--- no_error_log
[error]



=== TEST 11: fleq neq stale
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0.01, 1)
ngx.sleep(0.02)
local val = dogs:get_stale("Bernese", 2)
ngx.say(val, " " , type(val))
}
--- stream_response
42 number
--- no_error_log
[error]



=== TEST 12: get_stale with no flag
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0.01, 1)
ngx.sleep(0.02)
local val = dogs:get_stale("Bernese")
ngx.say(val, " " , type(val))
}
--- stream_response
42 number
--- no_error_log
[error]



=== TEST 13: flags_match is true
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0.01, 1)
ngx.sleep(0.02)
local val, err, stale, flags_match = dogs:get_stale("Bernese", 1)

ngx.say(val, " ", type(val), " : ",
err, " ", type(err), " : ",
flags_match, " ", type(flags_match))
}
--- stream_response
nil nil : nil nil : true boolean
--- no_error_log
[error]



=== TEST 14: flags_match is nil
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
local val, err, stale, flags_match = dogs:get_stale("Bernese", 3)
ngx.say(flags_match, " ", type(flags_match))
}
--- stream_response
nil nil
--- no_error_log
[error]



=== TEST 15: get when flag is not number
--- stream_config
lua_shared_dict dogs 1m;
--- stream_server_config
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:set("Bernese", 42, 0, 1)
local val = dogs:get_stale("Bernese", {})
}
--- error_log
cannot convert 'table' to 'int'