Skip to content

Commit

Permalink
Merge pull request #399 from GetStream/warmup-tls-conn
Browse files Browse the repository at this point in the history
warm up connection
  • Loading branch information
vishalnarkhede authored Sep 10, 2020
2 parents ba7daac + 6d4b65c commit 74a9121
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export class StreamChat<
const defaultOptions = {
timeout: 3000,
withCredentials: false, // making sure cookies are not sent
warmUp: false,
};

if (this.node) {
Expand Down Expand Up @@ -754,6 +755,10 @@ export class StreamChat<
case 'patch':
response = await this.axiosInstance.patch(url, data, requestConfig);
break;
case 'options':
// @ts-ignore
response = await this.axiosInstance.options(url, requestConfig);
break;
default:
throw new Error('Invalid request type');
}
Expand Down Expand Up @@ -1111,6 +1116,10 @@ export class StreamChat<
logger: this.logger,
});

if (this.options.warmUp) {
this.doAxiosRequest('options', this.baseURL + '/connect');
}

const handshake = await this.wsConnection.connect();
this.connectionID = this.wsConnection.connectionID;
return handshake;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ export type SearchOptions = {
export type StreamChatOptions = AxiosRequestConfig & {
browser?: boolean;
logger?: Logger;
warmUp?: boolean;
};

export type UnBanUserOptions = {
Expand Down
47 changes: 46 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import chaiAsPromised from 'chai-as-promised';
import chaiLike from 'chai-like';
import Immutable from 'seamless-immutable';
import { StreamChat, decodeBase64, encodeBase64 } from '../src';
import { expectHTTPErrorCode } from './utils';
import { expectHTTPErrorCode, getTestClientWithWarmUp } from './utils';
import fs from 'fs';
import assertArrays from 'chai-arrays';
const mockServer = require('mockttp').getLocal();
Expand Down Expand Up @@ -3003,3 +3003,48 @@ describe('paginate order with id_gt{,e}', () => {
expect(result.messages[1].id).to.be.equal(user + (4).toString());
});
});

describe('warm up', () => {
let channel;
let client;
const user = uuidv4();
it('shouldReuseConnection', async () => {
const baseUrl = 'https://chat-us-east-1.stream-io-api.com';
const client = getTestClient(true);
client.setBaseURL(baseUrl);
const health = await client.setUser({ id: user }, createUserToken(user));
client.health = health;
channel = await client.channel('messaging', uuidv4());

// populate cache
await channel.query();

// first client uses warmUp
const warmUpClient = getTestClientWithWarmUp();
warmUpClient.setBaseURL(baseUrl);
warmUpClient.health = await warmUpClient.setUser(
{ id: user },
createUserToken(user),
);

let t0 = new Date().getTime();
await warmUpClient.channel(channel.type, channel.id).query();
let t1 = new Date().getTime();
const withWarmUpDur = t1 - t0;
console.log('time taken with warm up ' + withWarmUpDur + ' milliseconds.');

// second client without warmUp
const noWarmUpClient = await getTestClient(false);
noWarmUpClient.setBaseURL(baseUrl);
noWarmUpClient.health = await noWarmUpClient.setUser(
{ id: user },
createUserToken(user),
);
t0 = new Date().getTime();
await noWarmUpClient.channel(channel.type, channel.id).query();
t1 = new Date().getTime();
const withoutWarmUpDur = t1 - t0;
console.log('time taken without warm up ' + withoutWarmUpDur + ' milliseconds.');
expect(withWarmUpDur).to.be.lessThan(withoutWarmUpDur);
});
});
4 changes: 4 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export async function getTestClientForUser(userID, status, options) {
return client;
}

export function getTestClientWithWarmUp() {
return new StreamChat(apiKey, { warmUp: true });
}

export function createUserToken(userID, exp) {
const c = new StreamChat(apiKey, apiSecret);
return c.createToken(userID, exp);
Expand Down

0 comments on commit 74a9121

Please sign in to comment.