Skip to content

Commit

Permalink
fix(perf): remove nanotimer for intervals as it uses 100% cpu
Browse files Browse the repository at this point in the history
  • Loading branch information
Balte de Wit committed Nov 26, 2018
1 parent b1cea03 commit d7bc543
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/lib/atemSocketChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AtemSocketChild extends EventEmitter {
private _connectionState = ConnectionState.Closed
private _debug = false
private _reconnectTimer: NodeJS.Timer | undefined
private _retransmitTimer = new NanoTimer()
private _retransmitTimer: NodeJS.Timer

private _localPacketId = 1
private _maxPacketID = (1 << 15) - 1 // Atem expects 15 not 16 bits before wrapping
Expand Down Expand Up @@ -54,7 +54,8 @@ export class AtemSocketChild extends EventEmitter {
}
}, this._reconnectInterval)
}
this._retransmitTimer.setInterval(() => this._checkForRetransmit(), [], '10ms')
// Check for retransmits every 10 milliseconds
this._retransmitTimer = setInterval(() => this._checkForRetransmit(), 10);

if (address) {
this._address = address
Expand All @@ -71,7 +72,7 @@ export class AtemSocketChild extends EventEmitter {
return new Promise((resolve) => {
if (this._connectionState === ConnectionState.Established) {
this._socket.close(() => {
this._retransmitTimer.clearInterval()
clearInterval(this._retransmitTimer)
clearInterval(this._reconnectTimer as NodeJS.Timer)
this._reconnectTimer = undefined

Expand Down Expand Up @@ -196,11 +197,12 @@ export class AtemSocketChild extends EventEmitter {
this._sendAck(this._lastReceivedPacketId)
} else if (!this._hasTimeout) {
this._hasTimeout = true
// timeout for 5 ms (syntax for nanotimer says m)
this._ackTimer.setTimeout(() => {
this._receivedWithoutAck = 0
this._hasTimeout = false
this._sendAck(this._lastReceivedPacketId)
}, [], '5ms')
}, [], '5m')
}
}

Expand Down

0 comments on commit d7bc543

Please sign in to comment.