Skip to content

Commit 847c061

Browse files
Merge pull request #1158 from DataDog/benoit/cache-proxy-agents
⚡️ [RUM-2721] cache proxy agents
2 parents 4effb36 + 571ee4e commit 847c061

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/helpers/__tests__/utils.test.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,25 @@ describe('utils', () => {
102102

103103
describe('proxy configuration', () => {
104104
test('should have a ProxyAgent by default', async () => {
105-
jest.spyOn(axios, 'create').mockImplementation((() => (args: AxiosRequestConfig) => args.httpsAgent) as any)
106-
const requestOptions = {
107-
apiKey: 'apiKey',
108-
appKey: 'applicationKey',
109-
baseUrl: 'http://fake-base.url/',
110-
}
111-
const request = ciUtils.getRequestBuilder(requestOptions)
112-
const fakeEndpoint = fakeEndpointBuilder(request)
113-
const httpsAgent = await fakeEndpoint()
105+
const httpsAgent = await getHttpAgentForProxyOptions()
114106
expect(httpsAgent).toBeDefined()
115107
expect(httpsAgent).toBeInstanceOf(ProxyAgent)
116108
})
117109

118110
test('should add proxy configuration when explicitly defined', async () => {
111+
const httpsAgent = await getHttpAgentForProxyOptions({protocol: 'http', host: '1.2.3.4', port: 1234})
112+
expect(httpsAgent).toBeDefined()
113+
expect((httpsAgent as any).getProxyForUrl()).toBe('http://1.2.3.4:1234')
114+
})
115+
116+
test('should re-use the same proxy agent for the same proxy options', async () => {
117+
const httpsAgent1 = await getHttpAgentForProxyOptions({protocol: 'http', host: '1.2.3.4', port: 1234})
118+
const httpsAgent2 = await getHttpAgentForProxyOptions({protocol: 'http', host: '1.2.3.4', port: 1234})
119+
expect(httpsAgent1).toBe(httpsAgent2)
120+
})
121+
122+
const getHttpAgentForProxyOptions = async (proxyOpts?: ciUtils.ProxyConfiguration) => {
119123
jest.spyOn(axios, 'create').mockImplementation((() => (args: AxiosRequestConfig) => args.httpsAgent) as any)
120-
const proxyOpts: ciUtils.ProxyConfiguration = {protocol: 'http', host: '1.2.3.4', port: 1234}
121124
const requestOptions = {
122125
apiKey: 'apiKey',
123126
appKey: 'applicationKey',
@@ -126,10 +129,9 @@ describe('utils', () => {
126129
}
127130
const request = ciUtils.getRequestBuilder(requestOptions)
128131
const fakeEndpoint = fakeEndpointBuilder(request)
129-
const httpsAgent = await fakeEndpoint()
130-
expect(httpsAgent).toBeDefined()
131-
expect((httpsAgent as any).getProxyForUrl()).toBe('http://1.2.3.4:1234')
132-
})
132+
133+
return fakeEndpoint()
134+
}
133135
})
134136

135137
test('should accept overrideUrl', async () => {

src/helpers/utils.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,22 @@ export const getRequestBuilder = (options: RequestOptions) => {
209209
return (args: AxiosRequestConfig) => axios.create(baseConfiguration)(overrideArgs(args))
210210
}
211211

212+
const proxyAgentCache = new Map<string, ProxyAgent>()
213+
212214
export const getProxyAgent = (proxyOpts?: ProxyConfiguration): ProxyAgent => {
213215
const proxyUrlFromConfiguration = getProxyUrl(proxyOpts)
214-
if (!proxyOpts || proxyUrlFromConfiguration === '') {
216+
217+
let proxyAgent = proxyAgentCache.get(proxyUrlFromConfiguration)
218+
if (!proxyAgent) {
219+
proxyAgent = createProxyAgentForUrl(proxyUrlFromConfiguration)
220+
proxyAgentCache.set(proxyUrlFromConfiguration, proxyAgent)
221+
}
222+
223+
return proxyAgent
224+
}
225+
226+
const createProxyAgentForUrl = (proxyUrl: string) => {
227+
if (!proxyUrl) {
215228
// Let the default proxy agent discover environment variables.
216229
return new ProxyAgent()
217230
}
@@ -223,7 +236,7 @@ export const getProxyAgent = (proxyOpts?: ProxyConfiguration): ProxyAgent => {
223236
return ''
224237
}
225238

226-
return proxyUrlFromConfiguration
239+
return proxyUrl
227240
},
228241
})
229242
}

0 commit comments

Comments
 (0)