Skip to content

Commit

Permalink
catch i/o exceptions and remove socket
Browse files Browse the repository at this point in the history
  • Loading branch information
zhemao committed Jan 30, 2012
1 parent 9fb9c31 commit 6e9f853
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions server/notify_server.py
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python


import redisd import redisd
import socket
import settings import settings


class Notifier: class Notifier:
Expand All @@ -9,7 +10,7 @@ def __init__(self):
self.clients = {} self.clients = {}
self.commands = {'subscribe': self.subscribe, self.commands = {'subscribe': self.subscribe,
'publish': self.publish, 'publish': self.publish,
'close': self.close} 'close': self.on_close}


def subscribe(self, sock, *channames): def subscribe(self, sock, *channames):
for i, name in enumerate(channames): for i, name in enumerate(channames):
Expand All @@ -35,17 +36,27 @@ def publish(self, sock, channame, message):
sock.rep_integer(0) sock.rep_integer(0)
else: else:
for fileno in chan: for fileno in chan:
chan[fileno].rep_multibulk(['message', channame, message]) try:
chan[fileno].rep_multibulk(['message', channame, message])
except socket.error, e:
self.remove_client(fileno)
raise e
except IOError, e:
self.remove_client(fileno)
raise e
sock.rep_integer(len(chan)) sock.rep_integer(len(chan))


def close(self, sock): def remove_client(self, fileno):
fileno = sock.fileno()
chan_list = self.clients.get(fileno) chan_list = self.clients.get(fileno)
if chan_list is not None: if chan_list is not None:
for channame in chan_list: for channame in chan_list:
del self.channels[channame][fileno] del self.channels[channame][fileno]
del self.clients[fileno] del self.clients[fileno]


def on_close(self, sock):
fileno = sock.fileno()
self.remove_client(fileno)

if __name__ == '__main__': if __name__ == '__main__':
notifier = Notifier() notifier = Notifier()


Expand Down

0 comments on commit 6e9f853

Please sign in to comment.