Permalink
Browse files

catch i/o exceptions and remove socket

  • Loading branch information...
1 parent 9fb9c31 commit 6e9f853dc1723b6ff34dc956021e8fe412138b60 @zhemao committed Jan 30, 2012
Showing with 15 additions and 4 deletions.
  1. +15 −4 server/notify_server.py
View
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import redisd
+import socket
import settings
class Notifier:
@@ -9,7 +10,7 @@ def __init__(self):
self.clients = {}
self.commands = {'subscribe': self.subscribe,
'publish': self.publish,
- 'close': self.close}
+ 'close': self.on_close}
def subscribe(self, sock, *channames):
for i, name in enumerate(channames):
@@ -35,17 +36,27 @@ def publish(self, sock, channame, message):
sock.rep_integer(0)
else:
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))
- def close(self, sock):
- fileno = sock.fileno()
+ def remove_client(self, fileno):
chan_list = self.clients.get(fileno)
if chan_list is not None:
for channame in chan_list:
del self.channels[channame][fileno]
del self.clients[fileno]
+ def on_close(self, sock):
+ fileno = sock.fileno()
+ self.remove_client(fileno)
+
if __name__ == '__main__':
notifier = Notifier()

0 comments on commit 6e9f853

Please sign in to comment.