From 7d87deb0a36b655992b2950e128d3cbb14601828 Mon Sep 17 00:00:00 2001 From: Antoine Arlaud Date: Mon, 5 Jun 2023 15:00:04 +0200 Subject: [PATCH] feat: capture latency upon ping --- lib/dispatcher.js | 16 +++++++++++++--- lib/server/socket.js | 4 ++-- test/functional/dispatcher-server-api.test.ts | 11 +++++++++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index b4229890a..dbcceb48c 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -46,11 +46,15 @@ class DispatcherClient { clientId, clientVersion, requestType = 'client-connected', + time = -1, ) { const hashedToken = hashToken(token); - const queryStringParams = clientId + let queryStringParams = clientId ? `?broker_client_id=${clientId}&request_type=${requestType}` : ''; + if (time != -1) { + queryStringParams = queryStringParams + `&latency=${Date.now() - time}`; + } await this.#makeRequest( { hashedToken, clientId, requestType: requestType }, `${this.#url}/internal/brokerservers/${ @@ -159,8 +163,14 @@ if (config.dispatcherUrl) { return kc.clientConnected(token, clientId, clientVersion); }; - clientPinged = async function (token, clientId, clientVersion) { - return kc.clientConnected(token, clientId, clientVersion, 'client-pinged'); + clientPinged = async function (token, clientId, clientVersion, time) { + return kc.clientConnected( + token, + clientId, + clientVersion, + 'client-pinged', + time, + ); }; clientDisconnected = async function (token, clientId) { diff --git a/lib/server/socket.js b/lib/server/socket.js index 15eebff01..5dbb686a6 100644 --- a/lib/server/socket.js +++ b/lib/server/socket.js @@ -132,10 +132,10 @@ module.exports = ({ server, filters, config }) => { socket.on('chunk', streamingResponse(token)); socket.on('request', response(token)); - socket.on('incoming::ping', () => { + socket.on('incoming::ping', (time) => { setImmediate( async () => - await dispatcher.clientPinged(token, clientId, clientVersion), + await dispatcher.clientPinged(token, clientId, clientVersion, time), ); }); diff --git a/test/functional/dispatcher-server-api.test.ts b/test/functional/dispatcher-server-api.test.ts index 129c01894..d272451f4 100644 --- a/test/functional/dispatcher-server-api.test.ts +++ b/test/functional/dispatcher-server-api.test.ts @@ -65,9 +65,11 @@ describe('Broker Server Dispatcher API interaction', () => { }); it('should fire off clientPinged call successfully with server response', async () => { + const time = Date.now(); + const fakeLatency = 1; nock(`${serverUrl}`) .post( - `/internal/brokerservers/0/connections/${hashedToken}?broker_client_id=${clientId}&request_type=client-pinged&version=${apiVersion}`, + `/internal/brokerservers/0/connections/${hashedToken}?broker_client_id=${clientId}&request_type=client-pinged&latency=${fakeLatency}&version=${apiVersion}`, ) .reply((uri, requestBody) => { spyFn(JSON.parse(requestBody)); @@ -79,7 +81,12 @@ describe('Broker Server Dispatcher API interaction', () => { process.env.hostname = '0'; const dispatcher = require('../../lib/dispatcher'); await expect( - dispatcher.clientPinged(token, clientId, clientVersion), + dispatcher.clientPinged( + token, + clientId, + clientVersion, + time - fakeLatency, + ), ).resolves.not.toThrowError(); expect(spyLogWarn).toHaveBeenCalledTimes(0); expect(spyFn).toBeCalledWith({