Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions packages/fxa-auth-server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,27 +372,12 @@ const convictConf = convict({
default: 'http://localhost:7000',
env: 'CUSTOMS_SERVER_URL',
},
customsHttpAgent: {
maxSockets: {
doc: 'The maximum number of sockets to be opened per host',
default: 10000,
env: 'CUSTOMS_MAX_SOCKETS',
},
maxFreeSockets: {
doc: 'The maximum number of free sockets to keep open for a host',
default: 10,
env: 'CUSTOMS_MAX_FREE_SOCKETS',
},
customsClient: {
timeoutMs: {
doc: 'The timeout in milliseconds for the sockets',
doc: 'Request timeout in milliseconds for calls to the customs server',
default: 30000,
env: 'CUSTOMS_TIMEOUT_MS',
},
freeSocketTimeoutMs: {
doc: 'The time in milliseconds for which a socket should remain open while unused',
default: 15000,
env: 'CUSTOMS_FREE_SOCKET_TIMEOUT_MS',
},
},
contentServer: {
url: {
Expand Down
75 changes: 17 additions & 58 deletions packages/fxa-auth-server/lib/customs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

'use strict';

const axios = require('axios');
const Sentry = require('@sentry/node');
const { config } = require('../config');
const { createHttpAgent, createHttpsAgent } = require('../lib/http-agent');
const { performance } = require('perf_hooks');
const { EmailNormalization } = require('fxa-shared/email/email-normalization');

Expand Down Expand Up @@ -56,41 +54,35 @@ class CustomsClient {
this.statsd = statsd;
this.rateLimit = rateLimit;

const customsHttpAgentConfig = config.get('customsHttpAgent');

if (url !== 'none') {
this.httpAgent = createHttpAgent(
customsHttpAgentConfig.maxSockets,
customsHttpAgentConfig.maxFreeSockets,
customsHttpAgentConfig.timeoutMs,
customsHttpAgentConfig.freeSocketTimeoutMs
);
this.httpsAgent = createHttpsAgent(
customsHttpAgentConfig.maxSockets,
customsHttpAgentConfig.maxFreeSockets,
customsHttpAgentConfig.timeoutMs,
customsHttpAgentConfig.freeSocketTimeoutMs
);
this.axiosInstance = axios.create({
baseURL: url,
httpAgent: this.httpAgent,
httpsAgent: this.httpsAgent,
});
this.url = url;
this.timeoutMs = config.get('customsClient').timeoutMs;
}
}

async makeRequest(endpoint, requestData) {
if (!this.axiosInstance) {
if (!this.url) {
return;
}

const method = endpoint.replaceAll('/', '');
const startTime = performance.now();

try {
this.logHttpAgentStatus();
const response = await fetch(`${this.url}${endpoint}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestData),
signal: AbortSignal.timeout(this.timeoutMs),
});

const response = await this.axiosInstance.post(endpoint, requestData);
// fetch does not reject on non-2xx; surface it so the catch below fails
// closed via backendServiceFailure rather than treating it as success.
if (!response.ok) {
throw new Error(`Customs server returned status ${response.status}`);
}

const result = await response.json();

if (this.statsd) {
this.statsd.timing(
Expand All @@ -99,7 +91,7 @@ class CustomsClient {
);
}

return response.data;
return result;
} catch (err) {
if (this.statsd) {
this.statsd.timing(
Expand Down Expand Up @@ -280,39 +272,6 @@ class CustomsClient {
}
}

logHttpAgentStatus() {
if (this.axiosInstance && this.statsd) {
this.logStatus(this.httpAgent, 'httpAgent');
this.logStatus(this.httpsAgent, 'httpsAgent');
}
}

logStatus(agent, name) {
if (agent) {
const status = agent.getCurrentStatus();
this.statsd.gauge(`${name}.createSocketCount`, status.createSocketCount);
this.statsd.gauge(
`${name}.createSocketErrorCount`,
status.createSocketErrorCount
);
this.statsd.gauge(`${name}.closeSocketCount`, status.closeSocketCount);
this.statsd.gauge(`${name}.errorSocketCount`, status.errorSocketCount);
this.statsd.gauge(
`${name}.timeoutSocketCount`,
status.timeoutSocketCount
);
this.statsd.gauge(`${name}.requestCount`, status.requestCount);

Object.keys(status.freeSockets).forEach((addr, value) => {
this.statsd.gauge(`${name}.freeSockets.${addr}`, value);
});

Object.keys(status.sockets).forEach((addr, value) => {
this.statsd.gauge(`${name}.sockets.${addr}`, value);
});
}
}

// #region Customs V2
v2Enabled() {
return this.rateLimit != null;
Expand Down
Loading
Loading