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

feat(internal-plugin-mercury): add mercuryTimeOffset #4035

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
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
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
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
Loading