Skip to content

Commit 4a26323

Browse files
dndxagentzh
authored andcommitted
feature: shdict: enabled the FFI-based API for the stream subsystem and ported tests.
Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent 870ec95 commit 4a26323

File tree

3 files changed

+1563
-47
lines changed

3 files changed

+1563
-47
lines changed

lib/resty/core.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
local subsystem = ngx.config.subsystem
44

55

6+
require "resty.core.shdict"
7+
8+
69
if subsystem == 'http' then
710
require "resty.core.uri"
811
require "resty.core.hash"
912
require "resty.core.base64"
1013
require "resty.core.regex"
1114
require "resty.core.exit"
12-
require "resty.core.shdict"
1315
require "resty.core.var"
1416
require "resty.core.ctx"
1517
require "resty.core.misc"

lib/resty/core/shdict.lua

Lines changed: 123 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,121 @@ local ngx_shared = ngx.shared
1919
local getmetatable = getmetatable
2020
local FFI_ERROR = base.FFI_ERROR
2121
local FFI_DECLINED = base.FFI_DECLINED
22+
local subsystem = ngx.config.subsystem
2223

2324

24-
ffi.cdef[[
25-
int ngx_http_lua_ffi_shdict_get(void *zone, const unsigned char *key,
26-
size_t key_len, int *value_type, unsigned char **str_value_buf,
27-
size_t *str_value_len, double *num_value, int *user_flags,
28-
int get_stale, int *is_stale, char **errmsg);
25+
local ngx_lua_ffi_shdict_get
26+
local ngx_lua_ffi_shdict_incr
27+
local ngx_lua_ffi_shdict_store
28+
local ngx_lua_ffi_shdict_flush_all
29+
local ngx_lua_ffi_shdict_get_ttl
30+
local ngx_lua_ffi_shdict_set_expire
31+
local ngx_lua_ffi_shdict_capacity
32+
local ngx_lua_ffi_shdict_free_space
2933

30-
int ngx_http_lua_ffi_shdict_incr(void *zone, const unsigned char *key,
31-
size_t key_len, double *value, char **err, int has_init, double init,
32-
long init_ttl, int *forcible);
3334

34-
int ngx_http_lua_ffi_shdict_store(void *zone, int op,
35-
const unsigned char *key, size_t key_len, int value_type,
36-
const unsigned char *str_value_buf, size_t str_value_len,
37-
double num_value, long exptime, int user_flags, char **errmsg,
38-
int *forcible);
35+
if subsystem == 'http' then
36+
ffi.cdef[[
37+
int ngx_http_lua_ffi_shdict_get(void *zone, const unsigned char *key,
38+
size_t key_len, int *value_type, unsigned char **str_value_buf,
39+
size_t *str_value_len, double *num_value, int *user_flags,
40+
int get_stale, int *is_stale, char **errmsg);
41+
42+
int ngx_http_lua_ffi_shdict_incr(void *zone, const unsigned char *key,
43+
size_t key_len, double *value, char **err, int has_init,
44+
double init, long init_ttl, int *forcible);
45+
46+
int ngx_http_lua_ffi_shdict_store(void *zone, int op,
47+
const unsigned char *key, size_t key_len, int value_type,
48+
const unsigned char *str_value_buf, size_t str_value_len,
49+
double num_value, long exptime, int user_flags, char **errmsg,
50+
int *forcible);
51+
52+
int ngx_http_lua_ffi_shdict_flush_all(void *zone);
53+
54+
long ngx_http_lua_ffi_shdict_get_ttl(void *zone,
55+
const unsigned char *key, size_t key_len);
3956

40-
int ngx_http_lua_ffi_shdict_flush_all(void *zone);
57+
int ngx_http_lua_ffi_shdict_set_expire(void *zone,
58+
const unsigned char *key, size_t key_len, long exptime);
4159

42-
long ngx_http_lua_ffi_shdict_get_ttl(void *zone,
43-
const unsigned char *key, size_t key_len);
60+
size_t ngx_http_lua_ffi_shdict_capacity(void *zone);
61+
]]
62+
63+
ngx_lua_ffi_shdict_get = C.ngx_http_lua_ffi_shdict_get
64+
ngx_lua_ffi_shdict_incr = C.ngx_http_lua_ffi_shdict_incr
65+
ngx_lua_ffi_shdict_store = C.ngx_http_lua_ffi_shdict_store
66+
ngx_lua_ffi_shdict_flush_all = C.ngx_http_lua_ffi_shdict_flush_all
67+
ngx_lua_ffi_shdict_get_ttl = C.ngx_http_lua_ffi_shdict_get_ttl
68+
ngx_lua_ffi_shdict_set_expire = C.ngx_http_lua_ffi_shdict_set_expire
69+
ngx_lua_ffi_shdict_capacity = C.ngx_http_lua_ffi_shdict_capacity
4470

45-
int ngx_http_lua_ffi_shdict_set_expire(void *zone,
46-
const unsigned char *key, size_t key_len, long exptime);
71+
if not pcall(function ()
72+
return C.ngx_http_lua_ffi_shdict_free_space
73+
end)
74+
then
75+
ffi.cdef[[
76+
size_t ngx_http_lua_ffi_shdict_free_space(void *zone);
77+
]]
78+
end
4779

48-
size_t ngx_http_lua_ffi_shdict_capacity(void *zone);
49-
]]
80+
ngx_lua_ffi_shdict_free_space = C.ngx_http_lua_ffi_shdict_free_space
81+
82+
elseif subsystem == 'stream' then
5083

51-
if not pcall(function () return C.ngx_http_lua_ffi_shdict_free_space end) then
5284
ffi.cdef[[
53-
size_t ngx_http_lua_ffi_shdict_free_space(void *zone);
85+
int ngx_stream_lua_ffi_shdict_get(void *zone, const unsigned char *key,
86+
size_t key_len, int *value_type, unsigned char **str_value_buf,
87+
size_t *str_value_len, double *num_value, int *user_flags,
88+
int get_stale, int *is_stale, char **errmsg);
89+
90+
int ngx_stream_lua_ffi_shdict_incr(void *zone, const unsigned char *key,
91+
size_t key_len, double *value, char **err, int has_init,
92+
double init, long init_ttl, int *forcible);
93+
94+
int ngx_stream_lua_ffi_shdict_store(void *zone, int op,
95+
const unsigned char *key, size_t key_len, int value_type,
96+
const unsigned char *str_value_buf, size_t str_value_len,
97+
double num_value, long exptime, int user_flags, char **errmsg,
98+
int *forcible);
99+
100+
int ngx_stream_lua_ffi_shdict_flush_all(void *zone);
101+
102+
long ngx_stream_lua_ffi_shdict_get_ttl(void *zone,
103+
const unsigned char *key, size_t key_len);
104+
105+
int ngx_stream_lua_ffi_shdict_set_expire(void *zone,
106+
const unsigned char *key, size_t key_len, long exptime);
107+
108+
size_t ngx_stream_lua_ffi_shdict_capacity(void *zone);
54109
]]
110+
111+
ngx_lua_ffi_shdict_get = C.ngx_stream_lua_ffi_shdict_get
112+
ngx_lua_ffi_shdict_incr = C.ngx_stream_lua_ffi_shdict_incr
113+
ngx_lua_ffi_shdict_store = C.ngx_stream_lua_ffi_shdict_store
114+
ngx_lua_ffi_shdict_flush_all = C.ngx_stream_lua_ffi_shdict_flush_all
115+
ngx_lua_ffi_shdict_get_ttl = C.ngx_stream_lua_ffi_shdict_get_ttl
116+
ngx_lua_ffi_shdict_set_expire = C.ngx_stream_lua_ffi_shdict_set_expire
117+
ngx_lua_ffi_shdict_capacity = C.ngx_stream_lua_ffi_shdict_capacity
118+
119+
if not pcall(function ()
120+
return C.ngx_stream_lua_ffi_shdict_free_space
121+
end)
122+
then
123+
ffi.cdef[[
124+
size_t ngx_stream_lua_ffi_shdict_free_space(void *zone);
125+
]]
126+
end
127+
128+
ngx_lua_ffi_shdict_free_space = C.ngx_stream_lua_ffi_shdict_free_space
129+
130+
else
131+
error("unknown subsystem: " .. subsystem)
55132
end
56133

57134
if not pcall(function () return C.free end) then
58135
ffi.cdef[[
59-
void free(void *ptr);
136+
void free(void *ptr);
60137
]]
61138
end
62139

@@ -141,11 +218,11 @@ local function shdict_store(zone, op, key, value, exptime, flags)
141218
return nil, "bad value type"
142219
end
143220

144-
local rc = C.ngx_http_lua_ffi_shdict_store(zone, op, key, key_len,
145-
valtyp, str_val_buf,
146-
str_val_len, num_val,
147-
exptime * 1000, flags, errmsg,
148-
forcible)
221+
local rc = ngx_lua_ffi_shdict_store(zone, op, key, key_len,
222+
valtyp, str_val_buf,
223+
str_val_len, num_val,
224+
exptime * 1000, flags, errmsg,
225+
forcible)
149226

150227
-- print("rc == ", rc)
151228

@@ -213,10 +290,10 @@ local function shdict_get(zone, key)
213290
local value_len = get_size_ptr()
214291
value_len[0] = size
215292

216-
local rc = C.ngx_http_lua_ffi_shdict_get(zone, key, key_len, value_type,
217-
str_value_buf, value_len,
218-
num_value, user_flags, 0,
219-
is_stale, errmsg)
293+
local rc = ngx_lua_ffi_shdict_get(zone, key, key_len, value_type,
294+
str_value_buf, value_len,
295+
num_value, user_flags, 0,
296+
is_stale, errmsg)
220297
if rc ~= 0 then
221298
if errmsg[0] then
222299
return nil, ffi_str(errmsg[0])
@@ -288,10 +365,10 @@ local function shdict_get_stale(zone, key)
288365
local value_len = get_size_ptr()
289366
value_len[0] = size
290367

291-
local rc = C.ngx_http_lua_ffi_shdict_get(zone, key, key_len, value_type,
292-
str_value_buf, value_len,
293-
num_value, user_flags, 1,
294-
is_stale, errmsg)
368+
local rc = ngx_lua_ffi_shdict_get(zone, key, key_len, value_type,
369+
str_value_buf, value_len,
370+
num_value, user_flags, 1,
371+
is_stale, errmsg)
295372
if rc ~= 0 then
296373
if errmsg[0] then
297374
return nil, ffi_str(errmsg[0])
@@ -394,10 +471,10 @@ local function shdict_incr(zone, key, value, init, init_ttl)
394471
init_ttl = 0
395472
end
396473

397-
local rc = C.ngx_http_lua_ffi_shdict_incr(zone, key, key_len, num_value,
398-
errmsg, init and 1 or 0,
399-
init or 0, init_ttl * 1000,
400-
forcible)
474+
local rc = ngx_lua_ffi_shdict_incr(zone, key, key_len, num_value,
475+
errmsg, init and 1 or 0,
476+
init or 0, init_ttl * 1000,
477+
forcible)
401478
if rc ~= 0 then -- ~= NGX_OK
402479
return nil, ffi_str(errmsg[0])
403480
end
@@ -413,7 +490,7 @@ end
413490
local function shdict_flush_all(zone)
414491
zone = check_zone(zone)
415492

416-
C.ngx_http_lua_ffi_shdict_flush_all(zone)
493+
ngx_lua_ffi_shdict_flush_all(zone)
417494
end
418495

419496

@@ -437,7 +514,7 @@ local function shdict_ttl(zone, key)
437514
return nil, "key too long"
438515
end
439516

440-
local rc = C.ngx_http_lua_ffi_shdict_get_ttl(zone, key, key_len)
517+
local rc = ngx_lua_ffi_shdict_get_ttl(zone, key, key_len)
441518

442519
if rc == FFI_ERROR then
443520
return nil, "bad zone"
@@ -475,8 +552,8 @@ local function shdict_expire(zone, key, exptime)
475552
return nil, "key too long"
476553
end
477554

478-
local rc = C.ngx_http_lua_ffi_shdict_set_expire(zone, key, key_len,
479-
exptime * 1000)
555+
local rc = ngx_lua_ffi_shdict_set_expire(zone, key, key_len,
556+
exptime * 1000)
480557

481558
if rc == FFI_ERROR then
482559
return nil, "bad zone"
@@ -495,14 +572,14 @@ end
495572
local function shdict_capacity(zone)
496573
zone = check_zone(zone)
497574

498-
return tonumber(C.ngx_http_lua_ffi_shdict_capacity(zone))
575+
return tonumber(ngx_lua_ffi_shdict_capacity(zone))
499576
end
500577

501578

502579
local function shdict_free_space(zone)
503580
zone = check_zone(zone)
504581

505-
return tonumber(C.ngx_http_lua_ffi_shdict_free_space(zone))
582+
return tonumber(ngx_lua_ffi_shdict_free_space(zone))
506583
end
507584

508585

0 commit comments

Comments
 (0)