16
16
@dataclass (kw_only = True )
17
17
class BridgeState (SerializableAttrs ):
18
18
human_readable_errors : ClassVar [Dict [str , str ]] = {}
19
+ default_error_source : ClassVar [str ] = "bridge"
20
+ default_error_ttl : ClassVar [int ] = 60
21
+ default_ok_ttl : ClassVar [int ] = 240
19
22
20
23
user_id : UserID = None
21
24
remote_id : str = None
@@ -31,20 +34,21 @@ def fill(self) -> 'BridgeState':
31
34
if not self .timestamp :
32
35
self .timestamp = int (time .time ())
33
36
if not self .ok :
34
- self .error_source = "bridge"
37
+ if not self .error_source :
38
+ self .error_source = self .default_error_source
35
39
try :
36
40
msg = self .human_readable_errors [self .error ]
37
41
except KeyError :
38
42
pass
39
43
else :
40
44
self .message = msg .format (message = self .message ) if self .message else msg
41
45
if not self .ttl :
42
- self .ttl = 60
46
+ self .ttl = self . default_error_ttl
43
47
else :
44
48
self .error = None
45
49
self .error_source = None
46
50
if not self .ttl :
47
- self .ttl = 240
51
+ self .ttl = self . default_ok_ttl
48
52
return self
49
53
50
54
def should_deduplicate (self , prev_state : 'BridgeState' ) -> bool :
@@ -54,7 +58,7 @@ def should_deduplicate(self, prev_state: 'BridgeState') -> bool:
54
58
# If there's more than ⅘ of the previous pong's time-to-live left, drop this one
55
59
return prev_state .timestamp + (prev_state .ttl / 5 ) > self .timestamp
56
60
57
- async def send (self , url : str , token : str , log : logging .Logger ) -> None :
61
+ async def send (self , url : str , token : str , log : logging .Logger , log_sent : bool = True ) -> None :
58
62
if not url :
59
63
return
60
64
headers = {"Authorization" : f"Bearer { token } " }
@@ -66,7 +70,7 @@ async def send(self, url: str, token: str, log: logging.Logger) -> None:
66
70
text = text .replace ("\n " , "\\ n" )
67
71
log .warning (f"Unexpected status code { resp .status } "
68
72
f"sending bridge state update: { text } " )
69
- else :
73
+ elif log_sent :
70
74
log .debug (f"Sent new bridge state { self } " )
71
75
except Exception as e :
72
76
log .warning (f"Failed to send updated bridge state: { e } " )
0 commit comments