Skip to content

Commit

Permalink
Make time duration since last server data available
Browse files Browse the repository at this point in the history
  • Loading branch information
ngbrown committed Aug 23, 2023
1 parent 0f10f88 commit f38cc02
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/amqp-base-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export abstract class AMQPBaseClient {
channelMax = 0
frameMax: number
heartbeat: number
lastDataReceived: number | undefined = undefined
onerror: (error: AMQPError) => void
logger: Logger | null | undefined = console
/** Used for string -> arraybuffer when publishing */
Expand Down Expand Up @@ -85,6 +86,7 @@ export abstract class AMQPBaseClient {
close(reason = "", code = 200): Promise<void> {
if (this.closed) return this.rejectClosed()
this.closed = true
this.lastDataReceived = undefined
let j = 0
const frame = new AMQPView(new ArrayBuffer(512))
frame.setUint8(j, 1); j += 1 // type: method
Expand Down Expand Up @@ -606,5 +608,18 @@ export abstract class AMQPBaseClient {
}
i += 1 // frame end
}
this.lastDataReceived = performance.now()
}

/**
* Get the time since since connection or last data received
* @returns milliseconds; Infinity if disconnected
*/
durationSinceLastData(): number {
if (typeof this.lastDataReceived === 'number') {
return performance.now() - this.lastDataReceived;
} else {
return Infinity
}
}
}
1 change: 1 addition & 0 deletions src/amqp-socket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class AMQPClient extends AMQPBaseClient {
socket.setTimeout((this.heartbeat || 60) * 1000)
// enable TCP keepalive if AMQP heartbeats are disabled
if (this.heartbeat === 0) socket.setKeepAlive(true, 60)
this.lastDataReceived = performance.now()
return new Promise((resolve, reject) => {
socket.on('timeout', () => reject(new AMQPError("timeout", this)))
socket.on('error', (err) => reject(new AMQPError(err.message, this)))
Expand Down
1 change: 1 addition & 0 deletions src/amqp-websocket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class AMQPWebSocketClient extends AMQPBaseClient {
this.socket = socket
socket.binaryType = "arraybuffer"
socket.onmessage = this.handleMessage.bind(this)
this.lastDataReceived = performance.now()
return new Promise((resolve, reject) => {
this.connectPromise = [resolve, reject]
socket.addEventListener('close', reject)
Expand Down

0 comments on commit f38cc02

Please sign in to comment.