-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connection: use ping-pong instead of heartbeat #303
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
|
||
const { EventEmitter } = require('events'); | ||
const semver = require('semver'); | ||
const timers = require('timers'); | ||
|
||
const common = require('./common'); | ||
const serde = require('./serde'); | ||
|
@@ -53,18 +52,15 @@ class Connection extends EventEmitter { | |
this.application = null; | ||
this.remoteProxies = {}; | ||
|
||
this._heartbeatCallbackInstance = null; | ||
this._heartbeatInterval = null; | ||
this._closed = false; | ||
|
||
// Defined in constructor to be used as default callback in callMethod | ||
// without binding it. | ||
this._emitError = (error) => { | ||
if (error) this.emit('error', error); | ||
}; | ||
|
||
// Defined in constructor to be used as heartbeat message | ||
// in debug mode events | ||
this._heartbeatMessage = {}; | ||
|
||
transport.on('message', this._processMessage.bind(this)); | ||
transport.on('close', this._onSocketClose.bind(this)); | ||
transport.on('error', this._onSocketError.bind(this)); | ||
|
@@ -241,33 +237,36 @@ class Connection extends EventEmitter { | |
// Send a pong message | ||
// | ||
pong(messageId) { | ||
if (this._closed) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that it's a public method, I'd say that the caller shouldn't invoke There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aqrln mark There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return; | ||
} | ||
const message = { pong: [messageId] }; | ||
this._send(this._prepareMessage(message), message); | ||
} | ||
|
||
// Start sending heartbeat messages | ||
// Start sending ping messages | ||
// interval - heartbeat interval in milliseconds | ||
// | ||
startHeartbeat(interval) { | ||
const callback = () => { | ||
this.transport.send('{}'); | ||
this.setTimeout(interval, this._heartbeatCallbackInstance); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
this.emit('heartbeat', this._heartbeatMessage); | ||
const heartbeat = () => { | ||
if (this._closed) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified to if (!this._closed) {
this.ping();
} |
||
return; | ||
} | ||
const pingId = this._nextMessageId; | ||
this.ping(() => { | ||
this.session.unbufferUpTo(pingId); | ||
}); | ||
}; | ||
|
||
this._heartbeatCallbackInstance = callback; | ||
callback(); | ||
this._heartbeatInterval = setInterval(heartbeat, interval); | ||
} | ||
|
||
// Stop sending heartbeat messages | ||
// Stop sending ping messages | ||
// | ||
stopHeartbeat() { | ||
if (this._heartbeatCallbackInstance) { | ||
this.clearTimeout(this._heartbeatCallbackInstance); | ||
this._heartbeatCallbackInstance = null; | ||
if (this._heartbeatInterval) { | ||
clearTimeout(this._heartbeatInterval); | ||
this._heartbeatInterval = null; | ||
} | ||
} | ||
|
||
|
@@ -320,40 +319,17 @@ class Connection extends EventEmitter { | |
// Close the connection | ||
// | ||
close() { | ||
this._closed = true; | ||
this.stopHeartbeat(); | ||
this.transport.end(); | ||
} | ||
|
||
// Set a timeout using timers.enroll() | ||
// milliseconds - amount of milliseconds | ||
// callback - callback function | ||
// | ||
setTimeout(milliseconds, callback) { | ||
timers.enroll(this, milliseconds); | ||
timers._unrefActive(this); | ||
this.once('_timeout', callback); | ||
} | ||
|
||
// Clear a timeout set with Connection#setTimeout | ||
// handler - timer callback to remove | ||
// | ||
clearTimeout(handler) { | ||
timers.unenroll(this); | ||
this.removeListener('_timeout', handler); | ||
} | ||
|
||
// Returns underlying transport | ||
// | ||
getTransport() { | ||
return this.transport.getRawTransport(); | ||
} | ||
|
||
// timers.enroll() timeout handler | ||
// | ||
_onTimeout() { | ||
this.emit('_timeout'); | ||
} | ||
|
||
// Prepare a JSTP message to be sent over this connection | ||
// message - a message to prepare | ||
// | ||
|
@@ -383,6 +359,7 @@ class Connection extends EventEmitter { | |
// message - a message to send (optional) | ||
// | ||
_end(message) { | ||
this._closed = true; | ||
this.stopHeartbeat(); | ||
|
||
if (message) { | ||
|
@@ -400,6 +377,7 @@ class Connection extends EventEmitter { | |
// Closed socket event handler | ||
// | ||
_onSocketClose() { | ||
this.closed = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there is a typo here, it should be |
||
this.stopHeartbeat(); | ||
this.emit('close', this); | ||
if (this.server) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it will be more appropriate to call this field
_heartbeatTimer
or_heartbeatTimeout
because it is somewhat confusing to useinterval
here, due to the same word being used as a parameter name instartHeartbeat
method and having the meaning of the delay between heartbeats.