Handle socket error state better
This commit is contained in:
@@ -82,7 +82,7 @@ export class AuthState extends EventEmitter {
|
||||
}
|
||||
}),
|
||||
on(socket, SocketEvent.Status, (status: SocketStatus) => {
|
||||
if (status === SocketStatus.Closed) {
|
||||
if (status === SocketStatus.Closed || status === SocketStatus.Error) {
|
||||
this.challenge = undefined
|
||||
this.request = undefined
|
||||
this.details = undefined
|
||||
|
||||
@@ -95,8 +95,10 @@ export const socketPolicyConnectOnSend = (socket: Socket) => {
|
||||
}
|
||||
}),
|
||||
on(socket, SocketEvent.Sending, (message: ClientMessage) => {
|
||||
const isClosed = [SocketStatus.Closed, SocketStatus.Error].includes(socket.status)
|
||||
|
||||
// When a new message is sent, make sure the socket is open (unless there was a recent error)
|
||||
if (socket.status === SocketStatus.Closed && lastError < ago(30)) {
|
||||
if (isClosed && lastError < ago(30)) {
|
||||
socket.open()
|
||||
}
|
||||
}),
|
||||
@@ -146,13 +148,15 @@ export const socketPolicyReopenActive = (socket: Socket) => {
|
||||
|
||||
const unsubscribers = [
|
||||
on(socket, SocketEvent.Status, (newStatus: SocketStatus) => {
|
||||
const isClosed = [SocketStatus.Closed, SocketStatus.Error].includes(socket.status)
|
||||
|
||||
// Keep track of the most recent error
|
||||
if (newStatus === SocketStatus.Open) {
|
||||
lastOpen = Date.now()
|
||||
}
|
||||
|
||||
// If the socket closed and we have no error, reopen it but don't flap
|
||||
if (newStatus === SocketStatus.Closed && pending.size) {
|
||||
if (isClosed && pending.size) {
|
||||
sleep(Math.max(0, 10_000 - (Date.now() - lastOpen))).then(() => {
|
||||
for (const message of pending.values()) {
|
||||
socket.send(message)
|
||||
|
||||
@@ -89,7 +89,10 @@ export class Socket extends EventEmitter {
|
||||
this._ws.onclose = () => {
|
||||
this._ws = undefined
|
||||
this._sendQueue.stop()
|
||||
this.emit(SocketEvent.Status, SocketStatus.Closed, this.url)
|
||||
|
||||
if (this.status !== SocketStatus.Error) {
|
||||
this.emit(SocketEvent.Status, SocketStatus.Closed, this.url)
|
||||
}
|
||||
}
|
||||
|
||||
this._ws.onmessage = (event: any) => {
|
||||
|
||||
Reference in New Issue
Block a user