Matrix invite: honor 429 retry_after_ms (3 attempts)

This commit is contained in:
inference-bot committed 2026-10-02 12:29:41 -06:00
1 parent 07a1f2ad4e
commit f86ead8653
1 file changed
+14 -3
+14 -3
View File
@@ -846,15 +846,26 @@ async def sync_matrix_space() -> dict:
return {"skipped": True} return {"skipped": True}
H = {"Authorization": f"Bearer {MATRIX_BOT_TOKEN}", "Content-Type": "application/json"} H = {"Authorization": f"Bearer {MATRIX_BOT_TOKEN}", "Content-Type": "application/json"}
async def invite(mxid: str, room: str) -> None: async def invite(mxid: str, room: str) -> bool:
import asyncio as _aio
for attempt in range(3):
r = await client.post( r = await client.post(
f"{MATRIX_HOMESERVER}/_matrix/client/v3/rooms/{room}/invite", f"{MATRIX_HOMESERVER}/_matrix/client/v3/rooms/{room}/invite",
headers=H, json={"user_id": mxid}, headers=H, json={"user_id": mxid},
) )
# 200 = invited; 403 M_FORBIDDEN with 'already in room' is fine too if r.status_code == 200:
if r.status_code not in (200, 403) and "already" not in r.text: return True
if r.status_code == 429:
wait = (r.json().get("retry_after_ms", 1000) / 1000) + 0.2
await _aio.sleep(wait)
continue
# 403 with 'already in room' is fine
if "already" in r.text:
return True
logger.warning("Matrix invite %s -> %s failed: %s %s", logger.warning("Matrix invite %s -> %s failed: %s %s",
mxid, room, r.status_code, r.text[:120]) mxid, room, r.status_code, r.text[:120])
return False
return False
async with httpx.AsyncClient(timeout=30) as client: async with httpx.AsyncClient(timeout=30) as client:
# current members of the space (to avoid duplicate invites) # current members of the space (to avoid duplicate invites)