Skip to content

Commit ca4ac7c

Browse files
committed
Add backoff to connections
1 parent ce1267f commit ca4ac7c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

beam.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from functools import partial
1515
from json import dumps, loads
1616

17-
from re import match, findall
17+
import re
18+
import time
1819

1920
from models import User, session
2021
from datetime import datetime
@@ -151,6 +152,8 @@ def connect(self, channel_id, bot_id, quiet=False):
151152
def authenticate(self, *args):
152153
"""Authenticate session to a Beam chat through a websocket."""
153154

155+
backoff = 0
156+
154157
future = args[-1]
155158
if future.exception() is None:
156159
self.websocket = future.result()
@@ -164,7 +167,12 @@ def authenticate(self, *args):
164167

165168
self.read_chat(self.handle)
166169
else:
167-
self.logger.error("There was an issue connecting. Trying again")
170+
self.logger.error("There was an issue connecting.")
171+
self.logger.error("Trying again in {} seconds.".format(backoff))
172+
173+
time.sleep(min(2**backoff, 60))
174+
backoff += 1
175+
168176
self.authenticate(*args)
169177

170178
def send_message(self, *args, method="msg"):
@@ -187,7 +195,7 @@ def send_message(self, *args, method="msg"):
187195

188196
if method == "msg":
189197
for message in args:
190-
for chunk in findall(r'.{1,250}', message):
198+
for chunk in re.findall(r'.{1,250}', message):
191199
message_packet = {
192200
"type": "method",
193201
"method": "msg",
@@ -334,7 +342,7 @@ def subscribe_to_interfaces(self, *interfaces):
334342
def parse_liveloading_message(self, message):
335343
"""Parse a message received from the Beam liveloading websocket."""
336344

337-
sections = match(r"(\d+)(.+)?$", message).groups()
345+
sections = re.match(r"(\d+)(.+)?$", message).groups()
338346

339347
return {
340348
"code": sections[0],

0 commit comments

Comments
 (0)