From bc6bc9538edbbc97246e09376f3686abe75ae35e Mon Sep 17 00:00:00 2001 From: Arvyd Paeglit Date: Thu, 18 Mar 2021 07:39:22 +0000 Subject: [PATCH 1/2] feat: axios agent keep alive to reuse connections --- src/axios.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/axios.ts b/src/axios.ts index be49d6ca..13b40a4f 100644 --- a/src/axios.ts +++ b/src/axios.ts @@ -1,11 +1,25 @@ import axios, { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'; +import http from 'http'; +import https from 'https'; import emitter from './emitter'; +const agentOptions = { + keepAlive: true, + maxSockets: 100, // Maximum number of sockets to allow per host. Defaults to Infinity. + maxFreeSockets: 10, + // timeout: 60000, // active socket keepalive for 60 seconds + freeSocketTimeout: 60000, // // Maximum number of sockets to leave open for 60 seconds in a free state. Only relevant if keepAlive is set to true. Defaults to 256. + socketActiveTTL: 1000 * 60 * 10, +}; + const axios_ = axios.create({ responseType: 'json', headers: { 'Content-Type': 'application/json;charset=utf-8', }, + // keepAlive pools and reuses TCP connections, so it's faster + httpAgent: new http.Agent(agentOptions), + httpsAgent: new https.Agent(agentOptions), }); axios_.interceptors.request.use( From 52b5bd004c7600891f50a71bef50dbabdd5791d5 Mon Sep 17 00:00:00 2001 From: Arvyd Paeglit Date: Thu, 18 Mar 2021 13:11:40 +0000 Subject: [PATCH 2/2] chore: cleaned comments --- src/axios.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/axios.ts b/src/axios.ts index 13b40a4f..8c938259 100644 --- a/src/axios.ts +++ b/src/axios.ts @@ -7,7 +7,6 @@ const agentOptions = { keepAlive: true, maxSockets: 100, // Maximum number of sockets to allow per host. Defaults to Infinity. maxFreeSockets: 10, - // timeout: 60000, // active socket keepalive for 60 seconds freeSocketTimeout: 60000, // // Maximum number of sockets to leave open for 60 seconds in a free state. Only relevant if keepAlive is set to true. Defaults to 256. socketActiveTTL: 1000 * 60 * 10, };