Skip to content

Commit

Permalink
Merge pull request #65 from snyk/keep-alive-agent
Browse files Browse the repository at this point in the history
feat: axios agent keep alive to reuse connections
  • Loading branch information
Arvi3d authored Mar 18, 2021
2 parents bb9bfa1 + 52b5bd0 commit ed7963b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/axios.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
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,
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(
Expand Down

0 comments on commit ed7963b

Please sign in to comment.