@@ -62,7 +62,7 @@ def get(self):
62
62
63
63
64
64
class MessageMixin (object ):
65
- waiters = []
65
+ waiters = set ()
66
66
cache = []
67
67
cache_size = 200
68
68
@@ -77,7 +77,11 @@ def wait_for_messages(self, callback, cursor=None):
77
77
if recent :
78
78
callback (recent )
79
79
return
80
- cls .waiters .append (callback )
80
+ cls .waiters .add (callback )
81
+
82
+ def cancel_wait (self , callback ):
83
+ cls = MessageMixin
84
+ cls .waiters .remove (callback )
81
85
82
86
def new_messages (self , messages ):
83
87
cls = MessageMixin
@@ -87,7 +91,7 @@ def new_messages(self, messages):
87
91
callback (messages )
88
92
except :
89
93
logging .error ("Error in waiter callback" , exc_info = True )
90
- cls .waiters = []
94
+ cls .waiters = set ()
91
95
cls .cache .extend (messages )
92
96
if len (cls .cache ) > self .cache_size :
93
97
cls .cache = cls .cache [- self .cache_size :]
@@ -114,7 +118,7 @@ class MessageUpdatesHandler(BaseHandler, MessageMixin):
114
118
@tornado .web .asynchronous
115
119
def post (self ):
116
120
cursor = self .get_argument ("cursor" , None )
117
- self .wait_for_messages (self .async_callback ( self . on_new_messages ) ,
121
+ self .wait_for_messages (self .on_new_messages ,
118
122
cursor = cursor )
119
123
120
124
def on_new_messages (self , messages ):
@@ -123,6 +127,9 @@ def on_new_messages(self, messages):
123
127
return
124
128
self .finish (dict (messages = messages ))
125
129
130
+ def on_connection_close (self ):
131
+ self .cancel_wait (self .on_new_messages )
132
+
126
133
127
134
class AuthLoginHandler (BaseHandler , tornado .auth .GoogleMixin ):
128
135
@tornado .web .asynchronous
0 commit comments