Skip to content

Commit 656904f

Browse files
committed
Make bridge state filling more customizable
1 parent 8f80f03 commit 656904f

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

mautrix/bridge/state.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
@dataclass(kw_only=True)
1717
class BridgeState(SerializableAttrs):
1818
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
1922

2023
user_id: UserID = None
2124
remote_id: str = None
@@ -31,20 +34,21 @@ def fill(self) -> 'BridgeState':
3134
if not self.timestamp:
3235
self.timestamp = int(time.time())
3336
if not self.ok:
34-
self.error_source = "bridge"
37+
if not self.error_source:
38+
self.error_source = self.default_error_source
3539
try:
3640
msg = self.human_readable_errors[self.error]
3741
except KeyError:
3842
pass
3943
else:
4044
self.message = msg.format(message=self.message) if self.message else msg
4145
if not self.ttl:
42-
self.ttl = 60
46+
self.ttl = self.default_error_ttl
4347
else:
4448
self.error = None
4549
self.error_source = None
4650
if not self.ttl:
47-
self.ttl = 240
51+
self.ttl = self.default_ok_ttl
4852
return self
4953

5054
def should_deduplicate(self, prev_state: 'BridgeState') -> bool:
@@ -54,7 +58,7 @@ def should_deduplicate(self, prev_state: 'BridgeState') -> bool:
5458
# If there's more than ⅘ of the previous pong's time-to-live left, drop this one
5559
return prev_state.timestamp + (prev_state.ttl / 5) > self.timestamp
5660

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:
5862
if not url:
5963
return
6064
headers = {"Authorization": f"Bearer {token}"}
@@ -66,7 +70,7 @@ async def send(self, url: str, token: str, log: logging.Logger) -> None:
6670
text = text.replace("\n", "\\n")
6771
log.warning(f"Unexpected status code {resp.status} "
6872
f"sending bridge state update: {text}")
69-
else:
73+
elif log_sent:
7074
log.debug(f"Sent new bridge state {self}")
7175
except Exception as e:
7276
log.warning(f"Failed to send updated bridge state: {e}")

0 commit comments

Comments
 (0)