Various homeserver state FST and queue fixes #101

Open
l1s5 wants to merge 6 commits from l1s5/out-of-your-element:homeserver-online-check-fixes into main
2 changed files with 16 additions and 11 deletions
Showing only changes of commit 2c650742f0 - Show all commits

matrix: refactor d2m packet enqueuing

Merge `setErrorWithPacket` into `queuePacket`.
Also set a more accurate type.
l1s5 2026-10-28 07:48:04 +09:00
Signed by untrusted user: l1s5
GPG key ID: D0A90CC905B58D9B

View file

@ -130,7 +130,7 @@ async function sendError(roomID, source, type, e, payload) {
|| (e instanceof mreq.MatrixServerError && [502, 503].includes(e.httpStatus) && e.errcode === "CX_SERVER_ERROR")
) {
// Matrix homeserver is down (reverse proxy indicated failure)
homeserverStatus.homeserverStatus.setErrorWithPacket([source, payload])
homeserverStatus.homeserverStatus.queuePacket([source, payload])
}
}
}

View file

@ -1,8 +1,8 @@
// @ts-check
const assert = require("assert").strict
const Denque = require("denque")
const StateMachine = require("snowtransfer").StateMachine
const Ty = require("../types")
const passthrough = require("../passthrough")
const {sync} = passthrough
@ -140,19 +140,24 @@ const homeserverStatus = sync.remember(() => new class HomeserverStatus {
}
/**
* When not real time, call this for incoming packets to queue them to be sent in order later.
* Queue a packet to be sent in order later, particularly when the homeserver is not realtime.
*
* Queueing while realtime implies the homeserver went down, so this also transitions the state to offline.
*
* @param {["Discord", import("cloudstorm").IGatewayMessage]
* | ["Matrix", Ty.Event.Outer<any>]} sourceAndPacket
*/
queuePacket(sourceAndPacket) {
assert(!this.isRealTime())
this.queue.push(sourceAndPacket)
}
setErrorWithPacket(sourceAndPacket) {
const wasRecovering = this.sm.currentStateName === "recovering"
this.sm.doTransition("error")
if (!wasRecovering) { // if was recovering then sourceAndPacket is already in the right place in queue
this.queuePacket(sourceAndPacket)
if (this.sm.currentStateName === "recovering" && this.queue.peekFront()?.[1] === sourceAndPacket[1]) {
this.sm.doTransition("error")
return
}
if (this.isRealTime()) {
this.sm.doTransition("error")
}
this.queue.push(sourceAndPacket)
}
})