14
14
from functools import partial
15
15
from json import dumps , loads
16
16
17
- from re import match , findall
17
+ import re
18
+ import time
18
19
19
20
from models import User , session
20
21
from datetime import datetime
@@ -151,6 +152,8 @@ def connect(self, channel_id, bot_id, quiet=False):
151
152
def authenticate (self , * args ):
152
153
"""Authenticate session to a Beam chat through a websocket."""
153
154
155
+ backoff = 0
156
+
154
157
future = args [- 1 ]
155
158
if future .exception () is None :
156
159
self .websocket = future .result ()
@@ -164,7 +167,12 @@ def authenticate(self, *args):
164
167
165
168
self .read_chat (self .handle )
166
169
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
+
168
176
self .authenticate (* args )
169
177
170
178
def send_message (self , * args , method = "msg" ):
@@ -187,7 +195,7 @@ def send_message(self, *args, method="msg"):
187
195
188
196
if method == "msg" :
189
197
for message in args :
190
- for chunk in findall (r'.{1,250}' , message ):
198
+ for chunk in re . findall (r'.{1,250}' , message ):
191
199
message_packet = {
192
200
"type" : "method" ,
193
201
"method" : "msg" ,
@@ -334,7 +342,7 @@ def subscribe_to_interfaces(self, *interfaces):
334
342
def parse_liveloading_message (self , message ):
335
343
"""Parse a message received from the Beam liveloading websocket."""
336
344
337
- sections = match (r"(\d+)(.+)?$" , message ).groups ()
345
+ sections = re . match (r"(\d+)(.+)?$" , message ).groups ()
338
346
339
347
return {
340
348
"code" : sections [0 ],
0 commit comments