aboutsummaryrefslogtreecommitdiffstats
path: root/wee_slack.py
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2019-05-03 15:30:33 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2019-05-03 15:50:02 +0200
commitf3d880e3f3a761c65f47d3f39de9c03bc5370e96 (patch)
tree8ed9ac02bf94ccc3c716c3115926f1aac316582c /wee_slack.py
parente89ab6822327f6b20c86fca9a7b97e9d56a7d4d0 (diff)
downloadwee-slack-f3d880e3f3a761c65f47d3f39de9c03bc5370e96.tar.gz
Process the entire websocket queue in receive_ws_callback
When we receive many lines over the websocket, receive_ws_callback seems to be called periodically (10 times per second or so) until all lines have been read. Since we previously only read one line per invocation of receive_ws_callback, it would take a long time to read all the lines. In addition to making events processed slower than necessary, this caused a problem for the pong watcher. Since the lines was processed so slow, it might take a long time until we process the pong message when the queue was large. This caused the script to disconnect the websocket since it didn't see the pong messages in time. To fix this, we run websocket receive in receive_ws_callback until there are no more messages. The messages are just added to the eventrouter queue, not processed right away, so this shouldn't cause performance issues.
Diffstat (limited to 'wee_slack.py')
-rw-r--r--wee_slack.py49
1 files changed, 25 insertions, 24 deletions
diff --git a/wee_slack.py b/wee_slack.py
index 1bef836..4c6269d 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -419,30 +419,31 @@ class EventRouter(object):
on the queue for processing as JSON.
"""
team = self.teams[team_hash]
- try:
- # Read the data from the websocket associated with this team.
- opcode, data = team.ws.recv_data(control_frame=True)
- except ssl.SSLWantReadError:
- # Expected to happen occasionally on SSL websockets.
- return w.WEECHAT_RC_OK
- except (WebSocketConnectionClosedException, socket.error) as e:
- handle_socket_error(e, team, 'receive')
- return w.WEECHAT_RC_OK
-
- if opcode == ABNF.OPCODE_PONG:
- team.last_pong_time = time.time()
- return w.WEECHAT_RC_OK
- elif opcode != ABNF.OPCODE_TEXT:
- return w.WEECHAT_RC_OK
-
- message_json = json.loads(data.decode('utf-8'))
- metadata = WeeSlackMetadata({
- "team": team_hash,
- }).jsonify()
- message_json["wee_slack_metadata"] = metadata
- if self.recording:
- self.record_event(message_json, 'type', 'websocket')
- self.receive(message_json)
+ while True:
+ try:
+ # Read the data from the websocket associated with this team.
+ opcode, data = team.ws.recv_data(control_frame=True)
+ except ssl.SSLWantReadError:
+ # No more data to read at this time.
+ return w.WEECHAT_RC_OK
+ except (WebSocketConnectionClosedException, socket.error) as e:
+ handle_socket_error(e, team, 'receive')
+ return w.WEECHAT_RC_OK
+
+ if opcode == ABNF.OPCODE_PONG:
+ team.last_pong_time = time.time()
+ return w.WEECHAT_RC_OK
+ elif opcode != ABNF.OPCODE_TEXT:
+ return w.WEECHAT_RC_OK
+
+ message_json = json.loads(data.decode('utf-8'))
+ metadata = WeeSlackMetadata({
+ "team": team_hash,
+ }).jsonify()
+ message_json["wee_slack_metadata"] = metadata
+ if self.recording:
+ self.record_event(message_json, 'type', 'websocket')
+ self.receive(message_json)
def receive_httprequest_callback(self, data, command, return_code, out, err):
"""