Skip to content

Commit

Permalink
Merge pull request #560 from snyk/feat/capture-latency-on-ping
Browse files Browse the repository at this point in the history
feat: capture latency upon ping
  • Loading branch information
aarlaud authored Jun 5, 2023
2 parents 2a94dbb + 7d87deb commit a9f89b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
16 changes: 13 additions & 3 deletions lib/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/${
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/server/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
});

Expand Down
11 changes: 9 additions & 2 deletions test/functional/dispatcher-server-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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({
Expand Down

0 comments on commit a9f89b4

Please # to comment.