Skip to content
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

✨ Add ping/pong #576

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ Agent.prototype._handleMessage = function(request, callback) {
return this._subscribePresence(request.ch, request.seq, callback);
case ACTIONS.presenceUnsubscribe:
return this._unsubscribePresence(request.ch, request.seq, callback);
case ACTIONS.pingPong:
return this._pingPong(callback);
default:
callback(new ShareDBError(ERROR_CODE.ERR_MESSAGE_BADLY_FORMED, 'Invalid or unknown message'));
}
Expand Down Expand Up @@ -540,6 +542,14 @@ Agent.prototype._querySubscribe = function(queryId, collection, query, options,
});
};

Agent.prototype._pingPong = function(callback) {
var error = null;
var message = {
a: ACTIONS.pingPong
};
callback(error, message);
};

function getResultsData(results) {
var items = [];
for (var i = 0; i < results.length; i++) {
Expand Down
20 changes: 20 additions & 0 deletions lib/client/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ Connection.prototype.handleMessage = function(message) {
return this._handlePresenceUnsubscribe(err, message);
case ACTIONS.presenceRequest:
return this._handlePresenceRequest(err, message);
case ACTIONS.pingPong:
return this._handlePingPong(err);

default:
logger.warn('Ignoring unrecognized message', message);
Expand Down Expand Up @@ -476,6 +478,19 @@ Connection.prototype.send = function(message) {
this.socket.send(JSON.stringify(message));
};

Connection.prototype.ping = function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From discussion today - let's throw an error if the connection state is not connected.

This is to guard against a bunch of ping requests queueing up at the socket layer if the socket is currently disconnected or connecting, and having them suddenly flood the socket when eventually connected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — added ERR_CANNOT_PING_OFFLINE

if (!this.canSend) {
throw new ShareDBError(
ERROR_CODE.ERR_CANNOT_PING_OFFLINE,
'Socket must be CONNECTED to ping'
);
}

var message = {
a: ACTIONS.pingPong
};
this.send(message);
};

/**
* Closes the socket and emits 'closed'
Expand Down Expand Up @@ -725,6 +740,11 @@ Connection.prototype._handleHandshake = function(error, message) {
this._initialize(message);
};

Connection.prototype._handlePingPong = function(error) {
if (error) return this.emit('error', error);
this.emit('pong');
};

Connection.prototype._initialize = function(message) {
if (this.state !== 'connecting') return;

Expand Down
1 change: 1 addition & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ShareDBError.CODES = {
ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT: 'ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT',
ERR_APPLY_SNAPSHOT_NOT_PROVIDED: 'ERR_APPLY_SNAPSHOT_NOT_PROVIDED',
ERR_CLIENT_ID_BADLY_FORMED: 'ERR_CLIENT_ID_BADLY_FORMED',
ERR_CANNOT_PING_OFFLINE: 'ERR_CANNOT_PING_OFFLINE',
ERR_CONNECTION_SEQ_INTEGER_OVERFLOW: 'ERR_CONNECTION_SEQ_INTEGER_OVERFLOW',
ERR_CONNECTION_STATE_TRANSITION_INVALID: 'ERR_CONNECTION_STATE_TRANSITION_INVALID',
ERR_DATABASE_ADAPTER_NOT_FOUND: 'ERR_DATABASE_ADAPTER_NOT_FOUND',
Expand Down
1 change: 1 addition & 0 deletions lib/message-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exports.ACTIONS = {
op: 'op',
snapshotFetch: 'nf',
snapshotFetchByTimestamp: 'nt',
pingPong: 'pp',
presence: 'p',
presenceSubscribe: 'ps',
presenceUnsubscribe: 'pu',
Expand Down
39 changes: 39 additions & 0 deletions test/client/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,45 @@ describe('client connection', function() {
});
});

describe('ping/pong', function() {
it('pings the backend', function(done) {
var connection = this.backend.connect();

connection.on('pong', function() {
done();
});

connection.on('connected', function() {
connection.ping();
});
});

it('handles errors', function(done) {
this.backend.use('receive', function(request, next) {
var error = request.data.a === 'pp' && new Error('bad');
next(error);
});

var connection = this.backend.connect();

connection.on('error', function(error) {
expect(error.message).to.equal('bad');
done();
});

connection.on('connected', function() {
connection.ping();
});
});

it('throws if pinging offline', function() {
var connection = this.backend.connect();
expect(function() {
connection.ping();
}).to.throw('Socket must be CONNECTED to ping');
});
});

describe('backend.agentsCount', function() {
it('updates after connect and connection.close()', function(done) {
var backend = this.backend;
Expand Down