Mercurial > cosocket
changeset 58:7a2ecb3e800f
conversations dict is now a weak value dict. Added a status page showing current resource usage.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 28 Apr 2009 21:37:00 -0700 |
parents | 08975d96ace2 |
children | b0f802c4fafc |
files | openwebchat.py |
diffstat | 1 files changed, 26 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/openwebchat.py Tue Apr 28 15:14:06 2009 -0700 +++ b/openwebchat.py Tue Apr 28 21:37:00 2009 -0700 @@ -5,6 +5,7 @@ import httplib import cStringIO import mimetools +import weakref from cosocket import * import channels @@ -16,7 +17,10 @@ class Conversations(object): def __init__(self): - self._convs = {} + self._convs = weakref.WeakValueDictionary() + + def __len__(self): + return len(self._convs) def get(self, name): if name not in self._convs: @@ -24,8 +28,11 @@ if not os.path.exists(filename): open(filename, 'w').close() - self._convs[name] = Conversation(open(filename, 'r+w')) - return self._convs[name] + conv = Conversation(open(filename, 'r+w')) + self._convs[name] = conv + else: + conv = self._convs[name] + return conv class Conversation(list): def __init__(self, fileobj): @@ -54,6 +61,7 @@ 'css' : 'text/css'} def __init__(self, addr, conversations): + self._num_connections = 0 self._convs = conversations self._server = CoroutineSocketServer(addr, self._server_coroutine) @@ -101,8 +109,12 @@ yield until_sent(block) def _server_coroutine(self, addr): - while 1: - yield self._until_one_request_processed(addr) + self._num_connections += 1 + try: + while 1: + yield self._until_one_request_processed(addr) + finally: + self._num_connections -= 1 def __multipart_boundary(self, boundary, mimetype = 'application/json'): # Here we actually declare the content type and start the @@ -183,8 +195,15 @@ else: conv_name = match.group(1) page = match.group(2) - yield self._until_conv_request_processed(addr, headers, method, - conv_name, page) + if conv_name == 'status': + # TODO: Return 404 if page is non-empty. + lines = ('open connections : %d' % self._num_connections, + 'open conversations: %d'% len(self._convs)) + yield self._until_http_response_sent('\r\n'.join(lines)) + else: + yield self._until_conv_request_processed(addr, headers, + method, conv_name, + page) if __name__ == '__main__': if len(sys.argv) > 1: