Skip to content

Commit

Permalink
feat(mercury): add mercuryTimeOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
lialang-cisco committed Dec 18, 2024
1 parent 8225a78 commit b7c2036
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/@webex/internal-plugin-mercury/src/mercury.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const Mercury = WebexPlugin.extend({
},
socket: 'object',
localClusterServiceUrls: 'object',
mercuryTimeOffset: {
default: undefined,
type: 'number',
},
},

derived: {
Expand Down Expand Up @@ -197,6 +201,7 @@ const Mercury = WebexPlugin.extend({

socket.on('close', (...args) => this._onclose(...args));
socket.on('message', (...args) => this._onmessage(...args));
socket.on('pong', (...args) => this._setTimeOffset(...args));
socket.on('sequence-mismatch', (...args) => this._emit('sequence-mismatch', ...args));
socket.on('ping-pong-latency', (...args) => this._emit('ping-pong-latency', ...args));

Expand Down Expand Up @@ -486,6 +491,7 @@ const Mercury = WebexPlugin.extend({
},

_onmessage(event) {
this._setTimeOffset(event);
const envelope = event.data;

if (process.env.ENABLE_MERCURY_LOGGING) {
Expand Down Expand Up @@ -529,6 +535,13 @@ const Mercury = WebexPlugin.extend({
});
},

_setTimeOffset(event) {
const {wsWriteTimestamp} = event;
if (typeof wsWriteTimestamp === 'number' && wsWriteTimestamp > 0) {
this.mercuryTimeOffset = Date.now() - wsWriteTimestamp;
}
},

_reconnect(webSocketUrl) {
this.logger.info(`${this.namespace}: reconnecting`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export default class Socket extends EventEmitter {
try {
const data = JSON.parse(event.data);
const sequenceNumber = parseInt(data.sequenceNumber, 10);
const wsWriteTimestamp = parseInt(data.wsWriteTimestamp, 10);

this.logger.debug(`socket,${this._domain}: sequence number: `, sequenceNumber);
if (this.expectedSequenceNumber && sequenceNumber !== this.expectedSequenceNumber) {
Expand All @@ -308,7 +309,7 @@ export default class Socket extends EventEmitter {
// Yes, it's a little weird looking; we want to emit message events that
// look like normal socket message events, but event.data cannot be
// modified and we don't actually care about anything but the data property
const processedEvent = {data};
const processedEvent = {data, wsWriteTimestamp};

this._acknowledge(processedEvent);
if (data.type === 'pong') {
Expand Down
14 changes: 14 additions & 0 deletions packages/@webex/internal-plugin-mercury/test/unit/spec/mercury.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,20 @@ describe('plugin-mercury', () => {
});
});

describe('#_setTimeOffset', () => {
it('sets mercuryTimeOffset based on the difference between wsWriteTimestamp and now', () => {
const event = {
data: {
wsWriteTimestamp: Date.now() - 1000,
}
};
assert.isUndefined(mercury.mercuryTimeOffset);
mercury._setTimeOffset(event);
assert.isDefined(mercury.mercuryTimeOffset);
assert.true(mercury.mercuryTimeOffset > 0);
});
});

describe('#_prepareUrl()', () => {
beforeEach(() => {
webex.internal.device.webSocketUrl = 'ws://example.com';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,13 +750,15 @@ describe('plugin-mercury', () => {
data: JSON.stringify({
sequenceNumber: 3,
id: 'mockid',
wsWriteTimestamp: 1735689600000,
}),
});

assert.calledWith(spy, {
data: {
sequenceNumber: 3,
id: 'mockid',
wsWriteTimestamp: 1735689600000,
},
});
});
Expand Down Expand Up @@ -790,13 +792,15 @@ describe('plugin-mercury', () => {
data: JSON.stringify({
sequenceNumber: 5,
id: 'mockid',
wsWriteTimestamp: 1735689600000,
}),
});
assert.called(socket._acknowledge);
assert.calledWith(socket._acknowledge, {
data: {
sequenceNumber: 5,
id: 'mockid',
wsWriteTimestamp: 1735689600000,
},
});
});
Expand Down

0 comments on commit b7c2036

Please sign in to comment.