1
- -- Copyright (C) 2013-2014 Jiale Zhi (calio), Cloudflare Inc.
1
+ -- Copyright (C) 2013-2014 Jiale Zhi (calio), CloudFlare Inc.
2
2
-- require "luacov"
3
3
4
4
local concat = table.concat
@@ -32,10 +32,10 @@ if not ngx.config or not ngx.config.ngx_lua_version
32
32
33
33
is_exiting = function () return false end
34
34
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" )
39
39
else
40
40
is_exiting = ngx .worker .exiting
41
41
end
@@ -52,14 +52,16 @@ local port
52
52
local path
53
53
local max_buffer_reuse = 10000 -- reuse buffer for at most 10000
54
54
-- times
55
+ local periodic_flush = nil
56
+ local need_periodic_flush = nil
55
57
56
58
-- internal variables
57
59
local buffer_size = 0
58
60
-- 2nd level buffer, it stores logs ready to be sent out
59
61
local send_buffer = " "
60
62
-- 1st level buffer, it stores incoming logs
61
63
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
63
65
local log_buffer_index = 0
64
66
65
67
local last_error
@@ -77,7 +79,6 @@ local logger_initted
77
79
local counter = 0
78
80
79
81
80
-
81
82
local function _write_error (msg )
82
83
last_error = msg
83
84
end
@@ -95,7 +96,7 @@ local function _do_connect()
95
96
sock :settimeout (timeout )
96
97
end
97
98
98
- -- host/ port and path config have already been checked in init()
99
+ -- " host"/" port" and " path" have already been checked in init()
99
100
if host and port then
100
101
ok , err = sock :connect (host , port )
101
102
elseif path then
@@ -114,9 +115,9 @@ local function _connect()
114
115
115
116
if connecting then
116
117
if debug then
117
- ngx_log (DEBUG , " previous connect not finished" )
118
+ ngx_log (DEBUG , " previous connection not finished" )
118
119
end
119
- return nil , " previous connect not finished"
120
+ return nil , " previous connection not finished"
120
121
end
121
122
122
123
connected = false
@@ -133,10 +134,10 @@ local function _connect()
133
134
end
134
135
135
136
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 )
137
138
end
138
139
139
- -- ngx.sleep use seconds to count time
140
+ -- ngx.sleep time is in seconds
140
141
if not exiting then
141
142
ngx_sleep (retry_interval / 1000 )
142
143
end
@@ -163,8 +164,8 @@ local function _prepare_stream_buffer()
163
164
log_buffer_data = new_tab (20000 , 0 )
164
165
counter = 0
165
166
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\" " )
168
169
end
169
170
end
170
171
end
@@ -178,7 +179,7 @@ local function _do_flush()
178
179
179
180
local bytes , err = sock :send (packet )
180
181
if not bytes then
181
- -- sock:send always close current connection on error
182
+ -- " sock:send" always closes current connection on error
182
183
return nil , err
183
184
end
184
185
206
207
local function _flush_lock ()
207
208
if not flushing then
208
209
if debug then
209
- ngx_log (DEBUG , " flush lock accquired " )
210
+ ngx_log (DEBUG , " flush lock acquired " )
210
211
end
211
212
flushing = true
212
213
return true
@@ -235,7 +236,7 @@ local function _flush()
235
236
236
237
if not _need_flush () then
237
238
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 )
239
240
end
240
241
_flush_unlock ()
241
242
return true
@@ -260,10 +261,10 @@ local function _flush()
260
261
end
261
262
262
263
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 )
264
265
end
265
266
266
- -- ngx.sleep use seconds to count time
267
+ -- ngx.sleep time is in seconds
267
268
if not exiting then
268
269
ngx_sleep (retry_interval / 1000 )
269
270
end
@@ -274,11 +275,15 @@ local function _flush()
274
275
_flush_unlock ()
275
276
276
277
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 "
278
279
.. " failed after " .. max_retry_times .. " retries: "
279
280
.. err
280
281
_write_error (err_msg )
281
282
return nil , err_msg
283
+ else
284
+ if debug then
285
+ ngx_log (DEBUG , " send " .. bytes .. " bytes" )
286
+ end
282
287
end
283
288
284
289
buffer_size = buffer_size - # send_buffer
@@ -287,8 +292,29 @@ local function _flush()
287
292
return bytes
288
293
end
289
294
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
+
290
313
local function _flush_buffer ()
291
314
local ok , err = timer_at (0 , _flush )
315
+
316
+ need_periodic_flush = false
317
+
292
318
if not ok then
293
319
_write_error (err )
294
320
return nil , err
@@ -326,22 +352,25 @@ function _M.init(user_config)
326
352
elseif k == " max_retry_times" then
327
353
max_retry_times = v
328
354
elseif k == " retry_interval" then
329
- -- ngx.sleep uses seconds to count sleep time
355
+ -- ngx.sleep time is in seconds
330
356
retry_interval = v
331
357
elseif k == " pool_size" then
332
358
pool_size = v
333
359
elseif k == " max_buffer_reuse" then
334
360
max_buffer_reuse = v
361
+ elseif k == " periodic_flush" then
362
+ periodic_flush = v
335
363
end
336
364
end
337
365
338
366
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."
340
369
end
341
370
342
371
343
372
if (flush_limit >= drop_limit ) then
344
- return nil , " flush_limit should < drop_limit"
373
+ return nil , " \" flush_limit\" should be < \" drop_limit\" "
345
374
end
346
375
347
376
flushing = false
@@ -354,6 +383,15 @@ function _M.init(user_config)
354
383
355
384
logger_initted = true
356
385
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
+
357
395
return logger_initted
358
396
end
359
397
@@ -375,14 +413,14 @@ function _M.log(msg)
375
413
376
414
local msg_len = # msg
377
415
378
- -- return result of _flush_buffer is not checked, because it writes
416
+ -- response of " _flush_buffer" is not checked, because it writes
379
417
-- error buffer
380
418
if (is_exiting ()) then
381
419
exiting = true
382
420
_write_buffer (msg )
383
421
_flush_buffer ()
384
422
if (debug ) then
385
- ngx_log (DEBUG , " worker exiting" )
423
+ ngx_log (DEBUG , " Nginx worker is exiting" )
386
424
end
387
425
bytes = 0
388
426
elseif (msg_len + buffer_size < flush_limit ) then
@@ -395,10 +433,11 @@ function _M.log(msg)
395
433
else
396
434
_flush_buffer ()
397
435
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" )
399
438
end
400
439
bytes = 0
401
- --- this message does not fit in buffer, drop it
440
+ --- this log message doesn't fit in buffer, drop it
402
441
end
403
442
404
443
if last_error then
0 commit comments