Improve connection management, re-send stuff after a connection gets closed

This commit is contained in:
Jon Staab
2024-12-16 16:09:58 -08:00
parent 887fbfc25d
commit d8005b7c10
5 changed files with 52 additions and 29 deletions
+10 -11
View File
@@ -11,23 +11,23 @@ export class ConnectionSender {
constructor(readonly cxn: Connection) {
this.worker = new Worker({
shouldDefer: ([verb, ...extra]: Message) => {
// Always send CLOSE to clean up pending requests, even if the connection is closed
if (verb === 'CLOSE') return false
// If we're not connected, nothing we can do
if (this.cxn.socket.status !== SocketStatus.Open) return true
if (cxn.socket.status !== SocketStatus.Open) return true
// Always allow sending AUTH
if (verb === 'AUTH') return false
// Only close reqs that have been sent
if (verb === 'CLOSE') return !this.cxn.state.pendingRequests.has(extra[0])
// Always allow sending join requests
if (verb === 'EVENT' && extra[0].kind === AUTH_JOIN) return false
// Wait for auth
if (![AuthStatus.None, AuthStatus.Ok].includes(this.cxn.auth.status)) return true
if (![AuthStatus.None, AuthStatus.Ok].includes(cxn.auth.status)) return true
// Limit concurrent requests
if (verb === 'REQ') return this.cxn.state.pendingRequests.size >= 8
if (verb === 'REQ') return cxn.state.pendingRequests.size >= 8
return false
},
@@ -39,15 +39,14 @@ export class ConnectionSender {
this.worker.buffer = this.worker.buffer.filter(m => !(m[0] === 'REQ' && m[1] === extra[0]))
}
this.cxn.socket.send([verb, ...extra])
// Re-check socket status since we let CLOSE through
if (cxn.socket.status === SocketStatus.Open) {
cxn.socket.send([verb, ...extra])
}
})
}
push = (message: Message) => {
this.worker.push(message)
}
close = async () => {
this.worker.pause()
}
}