Skip to content

Commit ea03d89

Browse files
fix: release buffer when send log messages after max retry times
2 parents 4ef6b70 + fe375b3 commit ea03d89

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

lib/resty/logger/socket.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ local function _flush(premature, self)
279279
.. "failed after " .. self.max_retry_times .. " retries: "
280280
.. err
281281
_write_error(self, err_msg)
282-
return nil, err_msg
283282
else
284283
if debug then
285284
ngx_log(DEBUG, "send " .. bytes .. " bytes")

t/udp_limit.t

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# vim:set ft= ts=4 sw=4 et:
2+
3+
use Test::Nginx::Socket::Lua;
4+
use Cwd qw(cwd);
5+
6+
repeat_each(2);
7+
8+
use Test::Nginx::Socket "no_plan";
9+
our $HtmlDir = html_dir;
10+
11+
our $pwd = cwd();
12+
13+
our $HttpConfig = qq{
14+
lua_package_path "$pwd/lib/?.lua;;";
15+
lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;";
16+
};
17+
18+
$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';
19+
$ENV{TEST_NGINX_HTML_DIR} = $HtmlDir;
20+
21+
no_long_string();
22+
23+
log_level('info');
24+
25+
run_tests();
26+
27+
__DATA__
28+
29+
=== TEST 1: UDP oversized log dropped, subsequent logs work
30+
--- http_config eval
31+
"$::HttpConfig"
32+
--- config
33+
location = /t {
34+
content_by_lua_block {
35+
collectgarbage()
36+
37+
package.loaded["resty.logger.socket"] = nil
38+
39+
-- we try to create a new udp socket and override the send method
40+
local old_udp = ngx.socket.udp
41+
ngx.socket.udp = function()
42+
local sock, err = old_udp()
43+
if not sock then return nil, err end
44+
45+
local proxy = {}
46+
local mt = {
47+
__index = function(t, k)
48+
return function(self, ...)
49+
return sock[k](sock, ...)
50+
end
51+
end
52+
}
53+
setmetatable(proxy, mt)
54+
55+
proxy.send = function(self, data)
56+
if #data > 65000 then
57+
return nil, "Message too long"
58+
end
59+
return sock:send(data)
60+
end
61+
62+
return proxy
63+
end
64+
65+
local logger_socket = require "resty.logger.socket"
66+
local logger, err = logger_socket:new({
67+
host = "127.0.0.1",
68+
port = 29999,
69+
flush_limit = 100000,
70+
sock_type = "udp",
71+
max_retry_times = 0,
72+
})
73+
if not logger then
74+
ngx.say("failed to init logger: ", err)
75+
return
76+
end
77+
78+
-- exceeed the max payload size of UDP
79+
local big_msg = string.rep("a", 70000)
80+
81+
local bytes, err = logger:log(big_msg)
82+
ngx.say("log big bytes: ", bytes)
83+
84+
-- this should fail
85+
local bytes, err = logger:flush(logger)
86+
ngx.say("flush big ret: ", bytes, " err: ", err)
87+
88+
-- after flush send another message and then flushing should succeed
89+
local small_msg = "hello world"
90+
local bytes, err = logger:log(small_msg)
91+
ngx.say("log small bytes: ", bytes, " err: ", err)
92+
93+
local bytes, err = logger:flush(logger)
94+
ngx.say("flush small ret: ", bytes, " err: ", err)
95+
96+
ngx.say("done")
97+
98+
}
99+
}
100+
--- request
101+
GET /t
102+
--- wait: 0.1
103+
--- udp_listen: 29999
104+
--- udp_reply:
105+
--- udp_query: hello world
106+
--- response_body_like
107+
log big bytes: \d+
108+
flush big ret: nil err: nil
109+
log small bytes: 11 err: try to send log messages to the log server failed after 0 retries: Message too long
110+
flush small ret: \w+ err: nil
111+
done

0 commit comments

Comments
 (0)