fix(sms,apps): login code no longer waits ~15s; wage quotes stop re-requesting
SMS: with SMS_RELAY_HOST unset inside Docker, every send first looked up the server's public IP from ipify / ifconfig.me / icanhazip, one after another with a 5s timeout each. They are unreachable from the server, and a failed lookup was not remembered, so EVERY SMS, the login code included, waited ~15s. The three are now tried in parallel (3s), concurrent sends share one lookup, and a failure is not retried for 10 minutes. Apps: the server answers /customer/me/wage-quote only for variants that exist. The store re-asked for every unanswered id from a computed, and each answer replaced `quotes`, re-running every card, so one missing variant kept the app posting wage-quote in a loop. Unanswered ids are now remembered, an empty answer does not touch `quotes`, and a failed call waits 30s. Tests: relay-host.spec (failed lookup not repeated, one shared lookup). اپ: کد ورود بدون معطلی ارسال میشود. اپ: صفحهها بعد از ورود دیگر چند بار پشت سر هم بارگذاری نمیشوند. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -35,8 +35,9 @@ describe('relay host', () => {
|
||||
fetchMock.mockResolvedValue({ ok: true, status: 200, text: async () => '81.12.34.56\n' });
|
||||
expect(await resolveRelayHost()).toBe('81.12.34.56');
|
||||
expect(relayConfig().host).toBe('81.12.34.56');
|
||||
const calls = fetchMock.mock.calls.length;
|
||||
await resolveRelayHost();
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
expect(fetchMock).toHaveBeenCalledTimes(calls);
|
||||
});
|
||||
|
||||
it('SMS_RELAY_HOST always wins, with no lookup', async () => {
|
||||
@@ -57,4 +58,20 @@ describe('relay host', () => {
|
||||
fetchMock.mockRejectedValue(new Error('offline'));
|
||||
expect(await resolveRelayHost()).toBe('172.18.0.5');
|
||||
});
|
||||
|
||||
it('a failed lookup is not repeated on the next SMS, so sending does not wait on it again', async () => {
|
||||
networkInterfaces.mockReturnValue({ eth0: [iface('172.18.0.5')] });
|
||||
fetchMock.mockRejectedValue(new Error('offline'));
|
||||
await resolveRelayHost();
|
||||
const calls = fetchMock.mock.calls.length;
|
||||
expect(await resolveRelayHost()).toBe('172.18.0.5');
|
||||
expect(fetchMock).toHaveBeenCalledTimes(calls);
|
||||
});
|
||||
|
||||
it('concurrent sends share one lookup', async () => {
|
||||
networkInterfaces.mockReturnValue({ eth0: [iface('172.18.0.5')] });
|
||||
fetchMock.mockResolvedValue({ ok: true, status: 200, text: async () => '81.12.34.56' });
|
||||
await Promise.all([resolveRelayHost(), resolveRelayHost(), resolveRelayHost()]);
|
||||
expect(fetchMock).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -52,29 +52,45 @@ export function isPrivateIpv4(ip: string | null): boolean {
|
||||
*/
|
||||
let publicIpv4: string | null = null;
|
||||
const PUBLIC_IP_SERVICES = ['https://api.ipify.org', 'https://ifconfig.me/ip', 'https://icanhazip.com'];
|
||||
const LOOKUP_TIMEOUT_MS = 3_000;
|
||||
// A failed lookup is not repeated before this: the services are often
|
||||
// unreachable from the server, and every SMS (the login code too) waited
|
||||
// 3 × 5s on them before it was sent.
|
||||
const FAILED_LOOKUP_RETRY_MS = 10 * 60 * 1000;
|
||||
let lookup: Promise<void> | null = null;
|
||||
let lookupFailedAt = 0;
|
||||
|
||||
async function lookupPublicIpv4(): Promise<void> {
|
||||
const attempts = PUBLIC_IP_SERVICES.map(async (url) => {
|
||||
const reply = await postWithTimeout(url, {}, null, LOOKUP_TIMEOUT_MS, 'GET');
|
||||
const ip = reply.body.trim();
|
||||
if (reply.ok && /^\d{1,3}(\.\d{1,3}){3}$/.test(ip) && !isPrivateIpv4(ip)) return ip;
|
||||
throw new Error('not a public IPv4');
|
||||
});
|
||||
try {
|
||||
publicIpv4 = await Promise.any(attempts);
|
||||
} catch {
|
||||
lookupFailedAt = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
export async function resolveRelayHost(): Promise<string | null> {
|
||||
if (envOrNull('SMS_RELAY_HOST') || publicIpv4) return relayConfig().host;
|
||||
const own = serverIpv4();
|
||||
if (!isPrivateIpv4(own)) return own;
|
||||
for (const url of PUBLIC_IP_SERVICES) {
|
||||
try {
|
||||
const reply = await postWithTimeout(url, {}, null, 5_000, 'GET');
|
||||
const ip = reply.body.trim();
|
||||
if (reply.ok && /^\d{1,3}(\.\d{1,3}){3}$/.test(ip) && !isPrivateIpv4(ip)) {
|
||||
publicIpv4 = ip;
|
||||
break;
|
||||
}
|
||||
} catch {
|
||||
/* try the next service */
|
||||
}
|
||||
}
|
||||
if (Date.now() - lookupFailedAt < FAILED_LOOKUP_RETRY_MS) return relayConfig().host;
|
||||
lookup ??= lookupPublicIpv4().finally(() => {
|
||||
lookup = null;
|
||||
});
|
||||
await lookup;
|
||||
return relayConfig().host;
|
||||
}
|
||||
|
||||
/** Tests only. */
|
||||
export function resetRelayHostCache() {
|
||||
publicIpv4 = null;
|
||||
lookup = null;
|
||||
lookupFailedAt = 0;
|
||||
}
|
||||
|
||||
export function relayConfig() {
|
||||
|
||||
Reference in New Issue
Block a user