Skip to content

Commit

Permalink
feat: HTTP(S) agent uses Keep-Alive for API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jones-dev committed Jul 21, 2023
1 parent 3daaa71 commit 293b15f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0


## [Unreleased]
### Changed
* Keep-Alive is now used by HTTP(S) agent, to reduce latency for subsequent API requests.
### Fixed
* CI: silence npm audit warnings in non-production dependencies due to
currently-unresolvable [vulnerability in semver <7.5.2](https://github.com/npm/node-semver/pull/564).
Expand Down
9 changes: 8 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ import { URLSearchParams } from 'url';
import FormData from 'form-data';
import { IncomingMessage } from 'http';
import { ProxyConfig } from './types';
import * as https from 'https';
import * as http from 'http';

type HttpMethod = 'GET' | 'DELETE' | 'POST';

const axiosInstance = axios.create({
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
});

/**
* Options for sending HTTP requests.
* @private
Expand Down Expand Up @@ -219,7 +226,7 @@ export class HttpClient {
axiosRequestConfig: AxiosRequestConfig,
): Promise<{ statusCode: number; content: TContent }> {
try {
const response = await axios.request(axiosRequestConfig);
const response = await axiosInstance.request(axiosRequestConfig);

if (axiosRequestConfig.responseType === 'text') {
// Workaround for axios-bug: https://github.com/axios/axios/issues/907
Expand Down

0 comments on commit 293b15f

Please sign in to comment.