@@ -31,34 +31,21 @@ def __init__(self,
3131 self .timeout = timeout
3232 self .verbose = verbose
3333
34+ self .socket = None
3435 self .pendings = None
3536 self .packer = msgpack .Packer ()
3637 self .lock = threading .Lock ()
38+
3739 try :
38- self .socket = self . _connect ()
40+ self ._reconnect ()
3941 except :
4042 # will be retried in emit()
41- self .socket = None
43+ self ._close ()
4244
4345 def emit (self , label , data ):
4446 bytes = self ._make_packet (label , data )
4547 self ._send (bytes )
4648
47- def _reconnect (self ):
48- if not self .socket :
49- self .socket = self ._connect ()
50-
51- def _connect (self ):
52- sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
53- sock .settimeout (self .timeout )
54- sock .connect ((self .host , self .port ))
55- return sock
56-
57- def _close (self ):
58- if self .socket :
59- self .socket .close ()
60- self .socket = None
61-
6249 def _make_packet (self , label , data ):
6350 if label :
6451 tag = '.' .join ((self .tag , label ))
@@ -97,6 +84,19 @@ def _send_internal(self, bytes):
9784 self ._close ()
9885 # clear buffer if it exceeds max bufer size
9986 if self .pendings and (len (self .pendings ) > self .bufmax ):
87+ # TODO: add callback handler here
10088 self .pendings = None
10189 else :
10290 self .pendings = bytes
91+
92+ def _reconnect (self ):
93+ if not self .socket :
94+ sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
95+ sock .settimeout (self .timeout )
96+ sock .connect ((self .host , self .port ))
97+ self .socket = sock
98+
99+ def _close (self ):
100+ if self .socket :
101+ self .socket .close ()
102+ self .socket = None
0 commit comments