1
1
# -*- coding: utf-8 -*-
2
2
3
+ import socket
3
4
import threading
4
5
from queue import Queue , Full , Empty
5
6
@@ -50,9 +51,11 @@ def __init__(self,
50
51
queue_maxsize = DEFAULT_QUEUE_MAXSIZE ,
51
52
queue_circular = DEFAULT_QUEUE_CIRCULAR ,
52
53
queue_overflow_handler = None ,
54
+ discard_logs_on_reconnect_error = False ,
53
55
** kwargs ):
54
56
"""
55
57
:param kwargs: This kwargs argument is not used in __init__. This will be removed in the next major version.
58
+ :discard_logs_on_reconnect_error: When set to true, will discard logs when reconnect error is reported.
56
59
"""
57
60
super (FluentSender , self ).__init__ (tag = tag , host = host , port = port , bufmax = bufmax , timeout = timeout ,
58
61
verbose = verbose , buffer_overflow_handler = buffer_overflow_handler ,
@@ -66,6 +69,8 @@ def __init__(self,
66
69
else :
67
70
self ._queue_overflow_handler = self ._queue_overflow_handler_default
68
71
72
+ self ._discard_logs_on_reconnect_error = discard_logs_on_reconnect_error
73
+
69
74
self ._thread_guard = threading .Event () # This ensures visibility across all variables
70
75
self ._closed = False
71
76
@@ -81,11 +86,7 @@ def close(self, flush=True):
81
86
return
82
87
self ._closed = True
83
88
if not flush :
84
- while True :
85
- try :
86
- self ._queue .get (block = False )
87
- except Empty :
88
- break
89
+ self ._clear_queue ()
89
90
self ._queue .put (_TOMBSTONE )
90
91
self ._send_thread .join ()
91
92
@@ -101,6 +102,13 @@ def queue_blocking(self):
101
102
def queue_circular (self ):
102
103
return self ._queue_circular
103
104
105
+ def _clear_queue (self ):
106
+ while True :
107
+ try :
108
+ self ._queue .get (block = False )
109
+ except Empty :
110
+ break
111
+
104
112
def _send (self , bytes_ ):
105
113
with self .lock :
106
114
if self ._closed :
@@ -120,8 +128,20 @@ def _send(self, bytes_):
120
128
121
129
return True
122
130
131
+ def _send_internal (self , bytes_ ):
132
+ send_internal_result = super (FluentSender , self )._send_internal (bytes_ )
133
+ if send_internal_result is False :
134
+ # when send_result is False, super() caught socket.error
135
+ # and assigned the error to self.last_error
136
+ if self ._discard_logs_on_reconnect_error is True and isinstance (self .last_error , socket .gaierror ):
137
+ # clear the queue to avoid blocking and print the log
138
+ self ._clear_queue ()
139
+ print ("%s. Please check address: (%s, %s)" % (str (self .last_error ), self .host , self .port ))
140
+
141
+ return send_internal_result
142
+
123
143
def _send_loop (self ):
124
- send_internal = super ( FluentSender , self ) ._send_internal
144
+ send_internal = self ._send_internal
125
145
126
146
try :
127
147
while True :
0 commit comments