From b85305859703f21b259ed792d52eb52edb6fd03c Mon Sep 17 00:00:00 2001 From: Marcelo Pires Date: Wed, 12 Aug 2020 13:22:44 +0100 Subject: [PATCH 1/5] warm up tls connection --- src/client.js | 13 +++++++++++++ test/test.js | 38 +++++++++++++++++++++++++++++++++++++- test/utils.js | 7 +++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/client.js b/src/client.js index 4608143af..60ce57371 100644 --- a/src/client.js +++ b/src/client.js @@ -56,6 +56,7 @@ export class StreamChat { const defaultOptions = { timeout: 3000, withCredentials: false, // making sure cookies are not sent + warmUp: true, }; if (this.node) { @@ -491,6 +492,9 @@ export class StreamChat { case 'patch': response = await this.axiosInstance.patch(url, data, requestConfig); break; + case 'options': + response = await this.axiosInstance.options(url); + break; default: break; } @@ -788,8 +792,17 @@ export class StreamChat { logger: this.logger, }); + let warmUp; + if (this.options.warmUp) { + warmUp = this.doAxiosRequest('options', this.baseURL + '/connect'); + } + const handshake = await this.wsConnection.connect(); this.connectionID = this.wsConnection.connectionID; + if (this.options.warmUp) { + // wait for warm up connection to complete + await warmUp; + } return handshake; } diff --git a/test/test.js b/test/test.js index 485741d77..22ecfbf1d 100644 --- a/test/test.js +++ b/test/test.js @@ -5,7 +5,11 @@ 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, + getTestClientForUserWithoutWarmUp, + getTestClientWithoutWarmUp, +} from './utils'; import fs from 'fs'; import assertArrays from 'chai-arrays'; const mockServer = require('mockttp').getLocal(); @@ -3003,3 +3007,35 @@ describe('paginate order with id_gt{,e}', () => { expect(result.messages[1].id).to.be.equal(user + (4).toString()); }); }); + +describe.only('warm up', () => { + let channel; + let client; + let user = uuidv4(); + it('shouldReuseConnection', async () => { + const client = getTestClient(true); + client.setBaseURL('https://chat-us-east-1.stream-io-api.com'); + const health = await client.setUser({ id: user }, createUserToken(user)); + client.health = health; + channel = await client.channel('messaging', uuidv4()); + + // populate cache + await channel.query(); + + // create a channel + // first request with warmUp + const warmUpClient = await getTestClientForUser(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.'); + const noWarmUpClient = await getTestClientForUserWithoutWarmUp(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); + }); +}); diff --git a/test/utils.js b/test/utils.js index dd6088ff2..257ca3681 100644 --- a/test/utils.js +++ b/test/utils.js @@ -29,6 +29,13 @@ export async function getTestClientForUser(userID, status, options) { return client; } +export async function getTestClientForUserWithoutWarmUp(userID) { + const client = new StreamChat(apiKey, { warmUp: false }); + const health = await client.setUser({ id: userID }, createUserToken(userID)); + client.health = health; + return client; +} + export function createUserToken(userID, exp) { const c = new StreamChat(apiKey, apiSecret); return c.createToken(userID, exp); From c5864dd5ab46eb1089899245f7f3f441779e5a0d Mon Sep 17 00:00:00 2001 From: Marcelo Pires Date: Wed, 12 Aug 2020 13:25:16 +0100 Subject: [PATCH 2/5] fix comments --- test/test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 22ecfbf1d..eb79ef88e 100644 --- a/test/test.js +++ b/test/test.js @@ -3022,7 +3022,6 @@ describe.only('warm up', () => { // populate cache await channel.query(); - // create a channel // first request with warmUp const warmUpClient = await getTestClientForUser(user); let t0 = new Date().getTime(); @@ -3030,6 +3029,8 @@ describe.only('warm up', () => { let t1 = new Date().getTime(); const withWarmUpDur = t1 - t0; console.log('time taken with warm up ' + withWarmUpDur + ' milliseconds.'); + + // second request without warmUp const noWarmUpClient = await getTestClientForUserWithoutWarmUp(user); t0 = new Date().getTime(); await noWarmUpClient.channel(channel.type, channel.id).query(); From 84951a8a4e3641e1974594a8dccc96d2dcf4ec16 Mon Sep 17 00:00:00 2001 From: Marcelo Pires Date: Wed, 12 Aug 2020 13:28:08 +0100 Subject: [PATCH 3/5] run all the tests --- test/test.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/test.js b/test/test.js index eb79ef88e..da966a4cd 100644 --- a/test/test.js +++ b/test/test.js @@ -3008,13 +3008,14 @@ describe('paginate order with id_gt{,e}', () => { }); }); -describe.only('warm up', () => { +describe('warm up', () => { let channel; let client; - let user = uuidv4(); + const user = uuidv4(); it('shouldReuseConnection', async () => { + const baseUrl = 'https://chat-us-east-1.stream-io-api.com'; const client = getTestClient(true); - client.setBaseURL('https://chat-us-east-1.stream-io-api.com'); + client.setBaseURL(baseUrl); const health = await client.setUser({ id: user }, createUserToken(user)); client.health = health; channel = await client.channel('messaging', uuidv4()); @@ -3024,6 +3025,7 @@ describe.only('warm up', () => { // first request with warmUp const warmUpClient = await getTestClientForUser(user); + warmUpClient.setBaseURL(baseUrl); let t0 = new Date().getTime(); await warmUpClient.channel(channel.type, channel.id).query(); let t1 = new Date().getTime(); @@ -3032,6 +3034,7 @@ describe.only('warm up', () => { // second request without warmUp const noWarmUpClient = await getTestClientForUserWithoutWarmUp(user); + noWarmUpClient.setBaseURL(baseUrl); t0 = new Date().getTime(); await noWarmUpClient.channel(channel.type, channel.id).query(); t1 = new Date().getTime(); From 6477a840bdcdb384018a537ee65792448d3ed4a5 Mon Sep 17 00:00:00 2001 From: Marcelo Pires Date: Wed, 12 Aug 2020 14:26:43 +0100 Subject: [PATCH 4/5] do not wait for warmUp connection to be completed(99% is always faster since it ends on proxy) --- src/client.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/client.js b/src/client.js index 60ce57371..8f63d7ae6 100644 --- a/src/client.js +++ b/src/client.js @@ -799,10 +799,6 @@ export class StreamChat { const handshake = await this.wsConnection.connect(); this.connectionID = this.wsConnection.connectionID; - if (this.options.warmUp) { - // wait for warm up connection to complete - await warmUp; - } return handshake; } From d3e3e943319a084bddaceb03bcb18d57d40ae460 Mon Sep 17 00:00:00 2001 From: Marcelo Pires Date: Wed, 12 Aug 2020 15:41:12 +0100 Subject: [PATCH 5/5] disable warmUp by default --- src/client.js | 5 ++--- test/test.js | 23 ++++++++++++++--------- test/utils.js | 7 ++----- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/client.js b/src/client.js index 8f63d7ae6..56cfb36b0 100644 --- a/src/client.js +++ b/src/client.js @@ -56,7 +56,7 @@ export class StreamChat { const defaultOptions = { timeout: 3000, withCredentials: false, // making sure cookies are not sent - warmUp: true, + warmUp: false, }; if (this.node) { @@ -792,9 +792,8 @@ export class StreamChat { logger: this.logger, }); - let warmUp; if (this.options.warmUp) { - warmUp = this.doAxiosRequest('options', this.baseURL + '/connect'); + this.doAxiosRequest('options', this.baseURL + '/connect'); } const handshake = await this.wsConnection.connect(); diff --git a/test/test.js b/test/test.js index da966a4cd..b4ed55de0 100644 --- a/test/test.js +++ b/test/test.js @@ -5,11 +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, - getTestClientForUserWithoutWarmUp, - getTestClientWithoutWarmUp, -} from './utils'; +import { expectHTTPErrorCode, getTestClientWithWarmUp } from './utils'; import fs from 'fs'; import assertArrays from 'chai-arrays'; const mockServer = require('mockttp').getLocal(); @@ -3023,18 +3019,27 @@ describe('warm up', () => { // populate cache await channel.query(); - // first request with warmUp - const warmUpClient = await getTestClientForUser(user); + // 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 request without warmUp - const noWarmUpClient = await getTestClientForUserWithoutWarmUp(user); + // 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(); diff --git a/test/utils.js b/test/utils.js index 257ca3681..e89d5dd0a 100644 --- a/test/utils.js +++ b/test/utils.js @@ -29,11 +29,8 @@ export async function getTestClientForUser(userID, status, options) { return client; } -export async function getTestClientForUserWithoutWarmUp(userID) { - const client = new StreamChat(apiKey, { warmUp: false }); - const health = await client.setUser({ id: userID }, createUserToken(userID)); - client.health = health; - return client; +export function getTestClientWithWarmUp() { + return new StreamChat(apiKey, { warmUp: true }); } export function createUserToken(userID, exp) {