Skip to content

Commit

Permalink
Merge pull request #7 from Kong/main
Browse files Browse the repository at this point in the history
[pull] main from Kong:main
  • Loading branch information
hutchic authored Dec 19, 2022
2 parents 322bdb2 + bcb547e commit c2fae19
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
12 changes: 12 additions & 0 deletions lib/resty/router/cdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,16 @@ end
return {
clib = clib,
ERR_BUF_MAX_LEN = ERR_BUF_MAX_LEN,

context_free = function(c)
clib.context_free(c)
end,

schema_free = function(s)
clib.schema_free(s)
end,

router_free = function(r)
clib.router_free(r)
end,
}
5 changes: 2 additions & 3 deletions lib/resty/router/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ local CACHED_VALUE = ffi_new("CValue[1]")
local UUID_BUF = ffi_new("uint8_t[?]", UUID_LEN)
local ERR_BUF_MAX_LEN = cdefs.ERR_BUF_MAX_LEN
local clib = cdefs.clib
local context_free = cdefs.context_free


function _M.new(schema)
local context = clib.context_new(schema.schema)
local c = setmetatable({
context = ffi_gc(context, function(c)
clib.context_free(c)
end),
context = ffi_gc(context, context_free),
schema = schema,
}, _MT)

Expand Down
10 changes: 5 additions & 5 deletions lib/resty/router/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local _MT = { __index = _M, }
local ffi = require("ffi")
local base = require("resty.core.base")
local cdefs = require("resty.router.cdefs")
local tb_new = require("table.new")
local get_string_buf = base.get_string_buf
local get_size_ptr = base.get_size_ptr
local ffi_string = ffi.string
Expand All @@ -17,9 +18,10 @@ local setmetatable = setmetatable

local ERR_BUF_MAX_LEN = cdefs.ERR_BUF_MAX_LEN
local clib = cdefs.clib
local router_free = cdefs.router_free


function _M.new(schema)
function _M.new(schema, routes_n)
local router = clib.router_new(schema.schema)
-- Note on this weird looking finalizer:
--
Expand All @@ -29,11 +31,9 @@ function _M.new(schema)
-- causing instruction fetch faults because the `router` finalizer will
-- attempt to execute from unmapped memory region
local r = setmetatable({
router = ffi_gc(router, function(r)
clib.router_free(r)
end),
router = ffi_gc(router, router_free),
schema = schema,
priorities = {},
priorities = tb_new(0, routes_n or 10),
}, _MT)

return r
Expand Down
8 changes: 4 additions & 4 deletions lib/resty/router/schema.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local _M = {}
local clib = require("resty.router.cdefs").clib
local cdefs = require("resty.router.cdefs")
local ffi = require("ffi")


Expand All @@ -8,14 +8,14 @@ local _MT = { __index = _M, }

local setmetatable = setmetatable
local ffi_gc = ffi.gc
local clib = cdefs.clib
local schema_free = cdefs.schema_free


function _M.new()
local schema = clib.schema_new()
local s = setmetatable({
schema = ffi_gc(schema, function(s)
clib.schema_free(s)
end),
schema = ffi_gc(schema, schema_free),
field_types = {},
field_ctypes = {},
clib = clib,
Expand Down
62 changes: 62 additions & 0 deletions t/02-gc.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# vim:set ft= ts=4 sw=4 et:

use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

repeat_each(1);

plan tests => repeat_each() * blocks() * 5;

my $pwd = cwd();

our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_package_cpath "$pwd/target/debug/?.so;;";
};

no_long_string();
no_diff();

run_tests();

__DATA__
=== TEST 1: gc schema, router
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local schema = require("resty.router.schema")
local router = require("resty.router.router")
local s = schema.new()
local r = router.new(s)
schema = nil
router = nil
rawset(package.loaded, "resty.router.schema", nil)
rawset(package.loaded, "resty.router.router", nil)
rawset(package.loaded, "resty.router.cdefs", nil)
collectgarbage()
s = nil
r = nil
collectgarbage()
ngx.say("ok")
}
}
--- request
GET /t
--- response_body
ok
--- no_error_log
[error]
[warn]
[crit]

0 comments on commit c2fae19

Please sign in to comment.