Skip to content

Commit

Permalink
Merge pull request #1158 from DataDog/benoit/cache-proxy-agents
Browse files Browse the repository at this point in the history
⚡️ [RUM-2721] cache proxy agents
  • Loading branch information
BenoitZugmeyer authored Jan 17, 2024
2 parents 4effb36 + 571ee4e commit 847c061
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
30 changes: 16 additions & 14 deletions src/helpers/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,25 @@ describe('utils', () => {

describe('proxy configuration', () => {
test('should have a ProxyAgent by default', async () => {
jest.spyOn(axios, 'create').mockImplementation((() => (args: AxiosRequestConfig) => args.httpsAgent) as any)
const requestOptions = {
apiKey: 'apiKey',
appKey: 'applicationKey',
baseUrl: 'http://fake-base.url/',
}
const request = ciUtils.getRequestBuilder(requestOptions)
const fakeEndpoint = fakeEndpointBuilder(request)
const httpsAgent = await fakeEndpoint()
const httpsAgent = await getHttpAgentForProxyOptions()
expect(httpsAgent).toBeDefined()
expect(httpsAgent).toBeInstanceOf(ProxyAgent)
})

test('should add proxy configuration when explicitly defined', async () => {
const httpsAgent = await getHttpAgentForProxyOptions({protocol: 'http', host: '1.2.3.4', port: 1234})
expect(httpsAgent).toBeDefined()
expect((httpsAgent as any).getProxyForUrl()).toBe('http://1.2.3.4:1234')
})

test('should re-use the same proxy agent for the same proxy options', async () => {
const httpsAgent1 = await getHttpAgentForProxyOptions({protocol: 'http', host: '1.2.3.4', port: 1234})
const httpsAgent2 = await getHttpAgentForProxyOptions({protocol: 'http', host: '1.2.3.4', port: 1234})
expect(httpsAgent1).toBe(httpsAgent2)
})

const getHttpAgentForProxyOptions = async (proxyOpts?: ciUtils.ProxyConfiguration) => {
jest.spyOn(axios, 'create').mockImplementation((() => (args: AxiosRequestConfig) => args.httpsAgent) as any)
const proxyOpts: ciUtils.ProxyConfiguration = {protocol: 'http', host: '1.2.3.4', port: 1234}
const requestOptions = {
apiKey: 'apiKey',
appKey: 'applicationKey',
Expand All @@ -126,10 +129,9 @@ describe('utils', () => {
}
const request = ciUtils.getRequestBuilder(requestOptions)
const fakeEndpoint = fakeEndpointBuilder(request)
const httpsAgent = await fakeEndpoint()
expect(httpsAgent).toBeDefined()
expect((httpsAgent as any).getProxyForUrl()).toBe('http://1.2.3.4:1234')
})

return fakeEndpoint()
}
})

test('should accept overrideUrl', async () => {
Expand Down
17 changes: 15 additions & 2 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,22 @@ export const getRequestBuilder = (options: RequestOptions) => {
return (args: AxiosRequestConfig) => axios.create(baseConfiguration)(overrideArgs(args))
}

const proxyAgentCache = new Map<string, ProxyAgent>()

export const getProxyAgent = (proxyOpts?: ProxyConfiguration): ProxyAgent => {
const proxyUrlFromConfiguration = getProxyUrl(proxyOpts)
if (!proxyOpts || proxyUrlFromConfiguration === '') {

let proxyAgent = proxyAgentCache.get(proxyUrlFromConfiguration)
if (!proxyAgent) {
proxyAgent = createProxyAgentForUrl(proxyUrlFromConfiguration)
proxyAgentCache.set(proxyUrlFromConfiguration, proxyAgent)
}

return proxyAgent
}

const createProxyAgentForUrl = (proxyUrl: string) => {
if (!proxyUrl) {
// Let the default proxy agent discover environment variables.
return new ProxyAgent()
}
Expand All @@ -223,7 +236,7 @@ export const getProxyAgent = (proxyOpts?: ProxyConfiguration): ProxyAgent => {
return ''
}

return proxyUrlFromConfiguration
return proxyUrl
},
})
}
Expand Down

0 comments on commit 847c061

Please sign in to comment.