Skip to content
Snippets Groups Projects
Commit 3660fcf5 authored by Guido Trotter's avatar Guido Trotter
Browse files

AsyncUDPSocket.handle_error


By overriding the default asyncore handle_error (which closes the
socket) with our own version, which logs what happened but tries to
proceed, we can get rid of a couple of try/except blocks. The resulting
churn is deindentation of the internal code.

Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 48bf6352
No related branches found
No related tags found
No related merge requests found
...@@ -92,7 +92,8 @@ class AsyncUDPSocket(asyncore.dispatcher): ...@@ -92,7 +92,8 @@ class AsyncUDPSocket(asyncore.dispatcher):
# differ and treat all messages equally. # differ and treat all messages equally.
pass pass
def do_read(self): # this method is overriding an asyncore.dispatcher method
def handle_read(self):
try: try:
payload, address = self.recvfrom(constants.MAX_UDP_DATA_SIZE) payload, address = self.recvfrom(constants.MAX_UDP_DATA_SIZE)
except socket.error, err: except socket.error, err:
...@@ -105,15 +106,6 @@ class AsyncUDPSocket(asyncore.dispatcher): ...@@ -105,15 +106,6 @@ class AsyncUDPSocket(asyncore.dispatcher):
ip, port = address ip, port = address
self.handle_datagram(payload, ip, port) self.handle_datagram(payload, ip, port)
# this method is overriding an asyncore.dispatcher method
def handle_read(self):
try:
self.do_read()
except: # pylint: disable-msg=W0702
# we need to catch any exception here, log it, but proceed, because even
# if we failed handling a single request, we still want to continue.
logging.error("Unexpected exception", exc_info=True)
def handle_datagram(self, payload, ip, port): def handle_datagram(self, payload, ip, port):
"""Handle an already read udp datagram """Handle an already read udp datagram
...@@ -128,26 +120,28 @@ class AsyncUDPSocket(asyncore.dispatcher): ...@@ -128,26 +120,28 @@ class AsyncUDPSocket(asyncore.dispatcher):
# this method is overriding an asyncore.dispatcher method # this method is overriding an asyncore.dispatcher method
def handle_write(self): def handle_write(self):
if not self._out_queue:
logging.error("handle_write called with empty output queue")
return
(ip, port, payload) = self._out_queue[0]
try: try:
if not self._out_queue: self.sendto(payload, 0, (ip, port))
logging.error("handle_write called with empty output queue") except socket.error, err:
if err.errno == errno.EINTR:
# we got a signal while trying to write. no need to do anything,
# handle_write will be called again because we haven't emptied the
# _out_queue, and we'll try again
return return
(ip, port, payload) = self._out_queue[0] else:
try: raise
self.sendto(payload, 0, (ip, port)) self._out_queue.pop(0)
except socket.error, err:
if err.errno == errno.EINTR: # this method is overriding an asyncore.dispatcher method
# we got a signal while trying to write. no need to do anything, def handle_error(self):
# handle_write will be called again because we haven't emptied the """Log an error in handling any request, and proceed.
# _out_queue, and we'll try again
return """
else: logging.exception("Error while handling asyncore request")
raise
self._out_queue.pop(0)
except: # pylint: disable-msg=W0702
# we need to catch any exception here, log it, but proceed, because even
# if we failed sending a single datagram we still want to continue.
logging.error("Unexpected exception", exc_info=True)
def enqueue_send(self, ip, port, payload): def enqueue_send(self, ip, port, payload):
"""Enqueue a datagram to be sent when possible """Enqueue a datagram to be sent when possible
...@@ -169,7 +163,7 @@ class AsyncUDPSocket(asyncore.dispatcher): ...@@ -169,7 +163,7 @@ class AsyncUDPSocket(asyncore.dispatcher):
""" """
result = utils.WaitForFdCondition(self, select.POLLIN, timeout) result = utils.WaitForFdCondition(self, select.POLLIN, timeout)
if result is not None and result & select.POLLIN: if result is not None and result & select.POLLIN:
self.do_read() self.handle_read()
return True return True
else: else:
return False return False
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment