Skip to content

Commit

Permalink
heartbeat vector clock (#15)
Browse files Browse the repository at this point in the history
* heartbeat vector clock

* rm dead const

* clock 5

* just in case
  • Loading branch information
davidmarkclements authored Oct 11, 2024
1 parent 13a0e24 commit 98813a4
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const methods = require('./methods')

const CONNECT_TIMEOUT = 20_000
const HEARTBEAT_INTERVAL = 1500
const HEARBEAT_MAX = HEARTBEAT_INTERVAL * 4
const HEARBEAT_CLOCK = 5
const ILLEGAL_METHODS = new Set(['id', 'userData', 'clients', 'hasClients', 'client', 'ref', 'unref', 'ready', 'opening', 'opened', 'close', 'closing', 'closed'])
const noop = Function.prototype

Expand All @@ -41,7 +41,7 @@ class PearIPC extends ReadyResource {
this._sc = null
this._rpc = null
this._clients = new Freelist()
this._lastActive = Date.now()
this._clock = HEARBEAT_CLOCK
this._internalHandlers = null

this._server = null
Expand Down Expand Up @@ -107,8 +107,7 @@ class PearIPC extends ReadyResource {
if (isServerSide) {
this._internalHandlers = {
_ping: (_, client) => {
const now = Date.now()
client._lastActive = now
client._clock = HEARBEAT_CLOCK
return { beat: 'pong' }
}
}
Expand Down Expand Up @@ -195,11 +194,8 @@ class PearIPC extends ReadyResource {
})
this._heartbeat = setInterval(() => {
for (const client of this.clients) {
const since = Date.now() - client._lastActive
const inactive = since > HEARBEAT_MAX
if (inactive) {
client.close()
}
client._clock--
if (client._clock <= 0) client.close()
}
}, HEARTBEAT_INTERVAL)
this._rpc = new RPC(noop)
Expand Down

0 comments on commit 98813a4

Please sign in to comment.