pss: auto-reopen subscription on Bee WebSocket close
Bee closes the pssSubscribe WebSocket after ~8s of idleness (or on any transient hiccup). The previous handler only logged the close — so the gateway silently went deaf to every ack/deliver envelope that arrived after the first close, breaking every chat whose provider takes more than a few seconds to respond. Re-open the subscription on close with a short delay; cancel() now blocks further reopens. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
7f6703b41d
commit
2c8daee63b
1 file changed
+22
-2
+22
-2
@@ -149,7 +149,18 @@ export class PssTransport {
|
|||||||
|
|
||||||
subscribe(args: PssSubscribeArgs): PssSubscription {
|
subscribe(args: PssSubscribeArgs): PssSubscription {
|
||||||
const topic = Topic.fromString(args.topic)
|
const topic = Topic.fromString(args.topic)
|
||||||
return this.opts.bee.pssSubscribe(topic, {
|
// Bee's pssSubscribe is a WebSocket that Bee closes whenever it feels like
|
||||||
|
// (idle timeout, broker hiccup, transient WS reset). Without re-subscribe,
|
||||||
|
// the gateway misses every envelope after the first close — which silently
|
||||||
|
// breaks every job whose provider sends ack/deliver more than a few
|
||||||
|
// seconds after the chat request lands. Wrap the subscription in a
|
||||||
|
// self-healing loop: on close, log + reopen.
|
||||||
|
let cancelled = false
|
||||||
|
let current: PssSubscription | null = null
|
||||||
|
const RECONNECT_DELAY_MS = 500
|
||||||
|
const open = (): void => {
|
||||||
|
if (cancelled) return
|
||||||
|
current = this.opts.bee.pssSubscribe(topic, {
|
||||||
onMessage: async msg => {
|
onMessage: async msg => {
|
||||||
try {
|
try {
|
||||||
const env = decodeEnvelope(msg.toUtf8())
|
const env = decodeEnvelope(msg.toUtf8())
|
||||||
@@ -171,8 +182,17 @@ export class PssTransport {
|
|||||||
this.opts.logger.error({err}, 'pss subscription error')
|
this.opts.logger.error({err}, 'pss subscription error')
|
||||||
},
|
},
|
||||||
onClose: () => {
|
onClose: () => {
|
||||||
this.opts.logger.warn({topic: args.topic}, 'pss subscription closed')
|
this.opts.logger.warn({topic: args.topic}, 'pss subscription closed — reopening')
|
||||||
|
if (!cancelled) setTimeout(open, RECONNECT_DELAY_MS)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
open()
|
||||||
|
return {
|
||||||
|
cancel: () => {
|
||||||
|
cancelled = true
|
||||||
|
current?.cancel()
|
||||||
|
},
|
||||||
|
} as PssSubscription
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user