Skip to content

Commit 9d27e86

Browse files
committed
Fix typos and correct grammar errors
1 parent fd7962d commit 9d27e86

File tree

5 files changed

+138
-44
lines changed

5 files changed

+138
-44
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ Available user configurations are listed as follows:
127127

128128
Keepalive pool size used by sock:keepalive. Default to 10.
129129

130+
* `periodic_flush`
131+
132+
Periodic flush interval (in seconds). Set to `nil` to turn off this feature.
133+
130134
[Back to TOC](#table-of-contents)
131135

132136
initted

lib/resty/logger/socket.lua

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Copyright (C) 2013-2014 Jiale Zhi (calio), Cloudflare Inc.
1+
-- Copyright (C) 2013-2014 Jiale Zhi (calio), CloudFlare Inc.
22
--require "luacov"
33

44
local concat = table.concat
@@ -32,10 +32,10 @@ if not ngx.config or not ngx.config.ngx_lua_version
3232

3333
is_exiting = function() return false end
3434

35-
ngx_log(CRIT, "lua-resty-logger-socket working with ngx_lua module < 0.9.3"
36-
.. " has a serious issue that some log messages may be lost when"
37-
.. " nginx reloads. We strongly recommend you update your ngx_lua"
38-
.. " module to at least 0.9.3")
35+
ngx_log(CRIT, "We strongly recommend you to update your ngx_lua module to "
36+
.. "0.9.3 or above. lua-resty-logger-socket will lose some log "
37+
.. "messages when Nginx reloads if it works with ngx_lua module "
38+
.. "below 0.9.3")
3939
else
4040
is_exiting = ngx.worker.exiting
4141
end
@@ -52,14 +52,16 @@ local port
5252
local path
5353
local max_buffer_reuse = 10000 -- reuse buffer for at most 10000
5454
-- times
55+
local periodic_flush = nil
56+
local need_periodic_flush = nil
5557

5658
-- internal variables
5759
local buffer_size = 0
5860
-- 2nd level buffer, it stores logs ready to be sent out
5961
local send_buffer = ""
6062
-- 1st level buffer, it stores incoming logs
6163
local log_buffer_data = new_tab(20000, 0)
62-
-- number of log lines in current buffer, starts from 0
64+
-- number of log lines in current 1st level buffer, starts from 0
6365
local log_buffer_index = 0
6466

6567
local last_error
@@ -77,7 +79,6 @@ local logger_initted
7779
local counter = 0
7880

7981

80-
8182
local function _write_error(msg)
8283
last_error = msg
8384
end
@@ -95,7 +96,7 @@ local function _do_connect()
9596
sock:settimeout(timeout)
9697
end
9798

98-
-- host/port and path config have already been checked in init()
99+
-- "host"/"port" and "path" have already been checked in init()
99100
if host and port then
100101
ok, err = sock:connect(host, port)
101102
elseif path then
@@ -114,9 +115,9 @@ local function _connect()
114115

115116
if connecting then
116117
if debug then
117-
ngx_log(DEBUG, "previous connect not finished")
118+
ngx_log(DEBUG, "previous connection not finished")
118119
end
119-
return nil, "previous connect not finished"
120+
return nil, "previous connection not finished"
120121
end
121122

122123
connected = false
@@ -133,10 +134,10 @@ local function _connect()
133134
end
134135

135136
if debug then
136-
ngx_log(DEBUG, "retry to connect to the log server: ", err)
137+
ngx_log(DEBUG, "reconnect to the log server: ", err)
137138
end
138139

139-
-- ngx.sleep use seconds to count time
140+
-- ngx.sleep time is in seconds
140141
if not exiting then
141142
ngx_sleep(retry_interval / 1000)
142143
end
@@ -163,8 +164,8 @@ local function _prepare_stream_buffer()
163164
log_buffer_data = new_tab(20000, 0)
164165
counter = 0
165166
if debug then
166-
ngx_log(DEBUG, "log buffer max reuse(" .. max_buffer_reuse
167-
.. ") reached, create new log_buffer_data")
167+
ngx_log(DEBUG, "log buffer reuse limit (" .. max_buffer_reuse
168+
.. ") reached, create a new \"log_buffer_data\"")
168169
end
169170
end
170171
end
@@ -178,7 +179,7 @@ local function _do_flush()
178179

179180
local bytes, err = sock:send(packet)
180181
if not bytes then
181-
-- sock:send always close current connection on error
182+
-- "sock:send" always closes current connection on error
182183
return nil, err
183184
end
184185

@@ -206,7 +207,7 @@ end
206207
local function _flush_lock()
207208
if not flushing then
208209
if debug then
209-
ngx_log(DEBUG, "flush lock accquired")
210+
ngx_log(DEBUG, "flush lock acquired")
210211
end
211212
flushing = true
212213
return true
@@ -235,7 +236,7 @@ local function _flush()
235236

236237
if not _need_flush() then
237238
if debug then
238-
ngx_log(DEBUG, "do not need to flush:", log_buffer_index)
239+
ngx_log(DEBUG, "no need to flush:", log_buffer_index)
239240
end
240241
_flush_unlock()
241242
return true
@@ -260,10 +261,10 @@ local function _flush()
260261
end
261262

262263
if debug then
263-
ngx_log(DEBUG, "retry to send log message to the log server: ", err)
264+
ngx_log(DEBUG, "resend log messages to the log server: ", err)
264265
end
265266

266-
-- ngx.sleep use seconds to count time
267+
-- ngx.sleep time is in seconds
267268
if not exiting then
268269
ngx_sleep(retry_interval / 1000)
269270
end
@@ -274,11 +275,15 @@ local function _flush()
274275
_flush_unlock()
275276

276277
if not bytes then
277-
local err_msg = "try to send log message to the log server "
278+
local err_msg = "try to send log messages to the log server "
278279
.. "failed after " .. max_retry_times .. " retries: "
279280
.. err
280281
_write_error(err_msg)
281282
return nil, err_msg
283+
else
284+
if debug then
285+
ngx_log(DEBUG, "send " .. bytes .. " bytes")
286+
end
282287
end
283288

284289
buffer_size = buffer_size - #send_buffer
@@ -287,8 +292,29 @@ local function _flush()
287292
return bytes
288293
end
289294

295+
local function _periodic_flush()
296+
if need_periodic_flush then
297+
-- no regular flush happened after periodic flush timer had been set
298+
if debug then
299+
ngx_log(DEBUG, "performing periodic flush")
300+
end
301+
_flush()
302+
else
303+
if debug then
304+
ngx_log(DEBUG, "no need to perform periodic flush: regular flush "
305+
.. "happened before")
306+
end
307+
need_periodic_flush = true
308+
end
309+
310+
timer_at(periodic_flush, _periodic_flush)
311+
end
312+
290313
local function _flush_buffer()
291314
local ok, err = timer_at(0, _flush)
315+
316+
need_periodic_flush = false
317+
292318
if not ok then
293319
_write_error(err)
294320
return nil, err
@@ -326,22 +352,25 @@ function _M.init(user_config)
326352
elseif k == "max_retry_times" then
327353
max_retry_times = v
328354
elseif k == "retry_interval" then
329-
-- ngx.sleep uses seconds to count sleep time
355+
-- ngx.sleep time is in seconds
330356
retry_interval = v
331357
elseif k == "pool_size" then
332358
pool_size = v
333359
elseif k == "max_buffer_reuse" then
334360
max_buffer_reuse = v
361+
elseif k == "periodic_flush" then
362+
periodic_flush = v
335363
end
336364
end
337365

338366
if not (host and port) and not path then
339-
return nil, "no logging server configured. Need host/port or path."
367+
return nil, "no logging server configured. \"host\"/\"port\" or "
368+
.. "\"path\" is required."
340369
end
341370

342371

343372
if (flush_limit >= drop_limit) then
344-
return nil, "flush_limit should < drop_limit"
373+
return nil, "\"flush_limit\" should be < \"drop_limit\""
345374
end
346375

347376
flushing = false
@@ -354,6 +383,15 @@ function _M.init(user_config)
354383

355384
logger_initted = true
356385

386+
if periodic_flush then
387+
if debug then
388+
ngx_log(DEBUG, "periodic flush enabled for every "
389+
.. periodic_flush .. " milliseconds")
390+
end
391+
need_periodic_flush = true
392+
timer_at(periodic_flush, _periodic_flush)
393+
end
394+
357395
return logger_initted
358396
end
359397

@@ -375,14 +413,14 @@ function _M.log(msg)
375413

376414
local msg_len = #msg
377415

378-
-- return result of _flush_buffer is not checked, because it writes
416+
-- response of "_flush_buffer" is not checked, because it writes
379417
-- error buffer
380418
if (is_exiting()) then
381419
exiting = true
382420
_write_buffer(msg)
383421
_flush_buffer()
384422
if (debug) then
385-
ngx_log(DEBUG, "worker exiting")
423+
ngx_log(DEBUG, "Nginx worker is exiting")
386424
end
387425
bytes = 0
388426
elseif (msg_len + buffer_size < flush_limit) then
@@ -395,10 +433,11 @@ function _M.log(msg)
395433
else
396434
_flush_buffer()
397435
if (debug) then
398-
ngx_log(DEBUG, "logger buffer is full, this log would be dropped")
436+
ngx_log(DEBUG, "logger buffer is full, this log message will be "
437+
.. "dropped")
399438
end
400439
bytes = 0
401-
--- this message does not fit in buffer, drop it
440+
--- this log message doesn't fit in buffer, drop it
402441
end
403442

404443
if last_error then

t/bug.t

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@ GET /t?a=1&b=2
8989
--- tcp_reply:
9090
--- tcp_no_close
9191
--- grep_error_log chop
92-
retry to send log message to the log server: timeout
92+
resend log messages to the log server: timeout
9393
--- response_body
9494
foo
9595
foo
9696
foo
9797
--- grep_error_log_out
98-
retry to send log message to the log server: timeout
99-
retry to send log message to the log server: timeout
100-
retry to send log message to the log server: timeout
98+
resend log messages to the log server: timeout
99+
resend log messages to the log server: timeout
100+
resend log messages to the log server: timeout
101101

102102

103103

104-
=== TEST 2: insert new log message to buffer in the middle of last send (it's difficult to control the time sequence here, so this is skipped now)
104+
=== TEST 2: insert new log messages to buffer in the middle of last send (it's difficult to control the time sequence here, so this is skipped now)
105105
--- http_config eval: $::HttpConfig
106106
--- config
107107
log_subrequest on;

0 commit comments

Comments
 (0)