Handle connection exceptions

This commit is contained in:
TheKysek 2016-10-15 15:53:26 +02:00
parent 5a665757ad
commit 00920856cc

View File

@ -82,10 +82,11 @@ class Connection(threading.Thread):
if self.status == 'fully_established': if self.status == 'fully_established':
self._request_objects() self._request_objects()
else: else:
raise logging.debug('Disconnecting from {}:{}. Reason: {}'.format(self.host, self.port, e))
data = None
except ConnectionResetError: except ConnectionResetError:
logging.debug('Disconnecting from {}:{}. Reason: ConnectionResetError'.format(self.host, self.port)) logging.debug('Disconnecting from {}:{}. Reason: ConnectionResetError'.format(self.host, self.port))
data = None self.status = 'disconnecting'
self._process_buffer_receive() self._process_buffer_receive()
self._process_queue() self._process_queue()
self._send_data() self._send_data()
@ -93,12 +94,12 @@ class Connection(threading.Thread):
logging.debug( logging.debug(
'Disconnecting from {}:{}. Reason: time.time() - self.last_message_received > shared.timeout'.format( 'Disconnecting from {}:{}. Reason: time.time() - self.last_message_received > shared.timeout'.format(
self.host, self.port)) self.host, self.port))
data = None self.status = 'disconnecting'
if time.time() - self.last_message_received > 30 and self.status != 'fully_established': if time.time() - self.last_message_received > 30 and self.status != 'fully_established':
logging.debug( logging.debug(
'Disconnecting from {}:{}. Reason: time.time() - self.last_message_received > 30 and self.status != \'fully_established\''.format( 'Disconnecting from {}:{}. Reason: time.time() - self.last_message_received > 30 and self.status != \'fully_established\''.format(
self.host, self.port)) self.host, self.port))
data = None self.status = 'disconnecting'
if time.time() - self.last_message_sent > 300 and self.status == 'fully_established': if time.time() - self.last_message_sent > 300 and self.status == 'fully_established':
self.send_queue.put(message.Message(b'pong', b'')) self.send_queue.put(message.Message(b'pong', b''))
if self.status == 'disconnecting': if self.status == 'disconnecting':
@ -111,25 +112,26 @@ class Connection(threading.Thread):
time.sleep(0.2) time.sleep(0.2)
def _connect(self): def _connect(self):
logging.info('Connecting to {}:{}'.format(self.host, self.port)) logging.debug('Connecting to {}:{}'.format(self.host, self.port))
try: try:
self.s = socket.create_connection((self.host, self.port)) self.s = socket.create_connection((self.host, self.port))
self.status = 'connected' self.status = 'connected'
logging.debug('Established TCP connection to {}:{}'.format(self.host, self.port)) logging.info('Established TCP connection to {}:{}'.format(self.host, self.port))
except Exception as e: except Exception as e:
logging.warning('Connection to {}:{} failed'.format(self.host, self.port)) logging.warning('Connection to {}:{} failed. Reason: {}'.format(self.host, self.port, e))
logging.warning(e)
self.status = 'failed' self.status = 'failed'
def _send_data(self): def _send_data(self):
if self.buffer_send: if self.buffer_send and self:
try: try:
amount = self.s.send(self.buffer_send) amount = self.s.send(self.buffer_send)
self.buffer_send = self.buffer_send[amount:] self.buffer_send = self.buffer_send[amount:]
except (BlockingIOError, ssl.SSLWantWriteError): except (BlockingIOError, ssl.SSLWantWriteError):
pass pass
except BrokenPipeError as e:
logging.debug('Disconnecting from {}:{}. Reason: {}'.format(self.host, self.port, e))
self.status = 'disconnecting'
def _do_tls_handshake(self): def _do_tls_handshake(self):
logging.debug('Initializing TLS connection with {}:{}'.format(self.host, self.port)) logging.debug('Initializing TLS connection with {}:{}'.format(self.host, self.port))
@ -148,7 +150,8 @@ class Connection(threading.Thread):
except ssl.SSLWantWriteError: except ssl.SSLWantWriteError:
select.select([], [self.s], []) select.select([], [self.s], [])
except Exception as e: except Exception as e:
logging.error(e) logging.debug('Disconnecting from {}:{}. Reason: {}'.format(self.host, self.port, e))
self.status = 'disconnecting'
break break
self.tls = True self.tls = True
logging.debug('Established TLS connection with {}:{}'.format(self.host, self.port)) logging.debug('Established TLS connection with {}:{}'.format(self.host, self.port))