Skip to content

Commit

Permalink
optimize eventloop
Browse files Browse the repository at this point in the history
  • Loading branch information
clowwindy committed Aug 2, 2015
1 parent 111acf6 commit 02120e3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
6 changes: 3 additions & 3 deletions shadowsocks/asyncdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def handle_event(self, sock, fd, event):
return
if event & eventloop.POLL_ERR:
logging.error('dns socket err')
self._loop.remove(self._sock, self)
self._loop.remove(self._sock)
self._sock.close()
# TODO when dns server is IPv6
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
Expand All @@ -380,7 +380,7 @@ def remove_callback(self, callback):
del self._cb_to_hostname[callback]
arr = self._hostname_to_cb.get(hostname, None)
if arr:
arr.remove(callback, self)
arr.remove(callback)
if not arr:
del self._hostname_to_cb[hostname]
if hostname in self._hostname_status:
Expand Down Expand Up @@ -427,7 +427,7 @@ def close(self):
if self._sock:
if self._loop:
self._loop.remove_periodic(self.handle_periodic)
self._loop.remove(self._sock, self)
self._loop.remove(self._sock)
self._sock.close()
self._sock = None

Expand Down
20 changes: 8 additions & 12 deletions shadowsocks/eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,25 @@ def __init__(self):
else:
raise Exception('can not find any available functions in select '
'package')
self._fd_to_f = {}
self._fd_to_handler = {}
self._fdmap = {} # (f, handler)
self._last_time = time.time()
self._periodic_callbacks = []
self._stopping = False
logging.debug('using event model: %s', model)

def poll(self, timeout=None):
events = self._impl.poll(timeout)
return [(self._fd_to_f[fd], fd, event) for fd, event in events]
return [(self._fdmap[fd][0], fd, event) for fd, event in events]

def add(self, f, mode, handler):
fd = f.fileno()
self._fd_to_f[fd] = f
self._fdmap[fd] = (f, handler)
self._impl.register(fd, mode)
self._fd_to_handler[fd] = handler

def remove(self, f, handler):
def remove(self, f):
fd = f.fileno()
del self._fd_to_f[fd]
del self._fdmap[fd]
self._impl.unregister(fd)
if handler is not None:
del self._fd_to_handler[fd]

def add_periodic(self, callback):
self._periodic_callbacks.append(callback)
Expand All @@ -182,9 +178,8 @@ def remove_periodic(self, callback):

def modify(self, f, mode, handler):
fd = f.fileno()
self._fdmap[fd] = (f, handler)
self._impl.modify(fd, mode)
if handler is not None:
self._fd_to_handler[fd] = handler

def stop(self):
self._stopping = True
Expand All @@ -209,8 +204,9 @@ def run(self):
continue

for sock, fd, event in events:
handler = self._fd_to_handler.get(fd, None)
handler = self._fdmap.get(fd, None)
if handler is not None:
handler = handler[1]
try:
handler.handle_event(sock, fd, event)
except (OSError, IOError) as e:
Expand Down
8 changes: 4 additions & 4 deletions shadowsocks/tcprelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,13 @@ def destroy(self):
logging.debug('destroy')
if self._remote_sock:
logging.debug('destroying remote')
self._loop.remove(self._remote_sock, self._server)
self._loop.remove(self._remote_sock)
del self._fd_to_handlers[self._remote_sock.fileno()]
self._remote_sock.close()
self._remote_sock = None
if self._local_sock:
logging.debug('destroying local')
self._loop.remove(self._local_sock, self._server)
self._loop.remove(self._local_sock)
del self._fd_to_handlers[self._local_sock.fileno()]
self._local_sock.close()
self._local_sock = None
Expand Down Expand Up @@ -691,7 +691,7 @@ def handle_event(self, sock, fd, event):
def handle_periodic(self):
if self._closed:
if self._server_socket:
self._eventloop.remove(self._server_socket, self)
self._eventloop.remove(self._server_socket)
self._server_socket.close()
self._server_socket = None
logging.info('closed TCP port %d', self._listen_port)
Expand All @@ -706,5 +706,5 @@ def close(self, next_tick=False):
if not next_tick:
if self._eventloop:
self._eventloop.remove_periodic(self.handle_periodic)
self._eventloop.remove(self._server_socket, self)
self._eventloop.remove(self._server_socket)
self._server_socket.close()
4 changes: 2 additions & 2 deletions shadowsocks/udprelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _get_a_server(self):
def _close_client(self, client):
if hasattr(client, 'close'):
self._sockets.remove(client.fileno())
self._eventloop.remove(client, self)
self._eventloop.remove(client)
client.close()
else:
# just an address
Expand Down Expand Up @@ -288,5 +288,5 @@ def close(self, next_tick=False):
if not next_tick:
if self._eventloop:
self._eventloop.remove_periodic(self.handle_periodic)
self._eventloop.remove(self._server_socket, self)
self._eventloop.remove(self._server_socket)
self._server_socket.close()

0 comments on commit 02120e3

Please sign in to comment.