Skip to content

Commit 3683d5e

Browse files
sunnylqmclaude
andcommitted
feat(upload): rank a far primary with the backups by address family
The server may now make an edge domain the primary upload address. Its TCP probe takes whichever family the system prefers, and from Singapore the domain's IPv6 answer pointed at Beijing (180 ms) while its IPv4 edge was 2 ms away, so the primary went to the end of the list unprobed. When the primary is far and the server sends backupUrls, the primary is now timed by TLS handshake over IPv4 and IPv6 like the backups and ranked among them with the family that won; ties keep the server's order. A server that sends only backupUrl still gets the backup first, the primary last, without a second probe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4c4d190 commit 3683d5e

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

‎src/api.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,13 @@ export interface UploadTarget {
522522
* accepts the same form. `backupUrls` (newer servers) lists all alternatives
523523
* to `url`, `backupUrl` (older ones) names a single one.
524524
*
525-
* The primary stays first while it is near: the alternatives are accelerated
526-
* endpoints, worth a detour only when the direct path is poor.
527-
* When it is far or unreachable, the alternatives are timed by TLS handshake,
528-
* over IPv4 and IPv6 apart, and tried fastest first with the family that won;
529-
* the primary goes last.
525+
* The primary stays first while its TCP connect takes at most 150 ms, unprobed
526+
* further. When it is far or unreachable, every address, the primary
527+
* included, is timed by TLS handshake over IPv4 and IPv6 apart and tried
528+
* fastest first with the family that won; ties keep the server's order. The
529+
* family matters: an edge network may answer AAAA queries with an address on
530+
* another continent while its A records are near, and the TCP probe above
531+
* takes whichever family the system prefers.
530532
*/
531533
export async function chooseUploadUrls(instruction: {
532534
url: string;
@@ -560,11 +562,12 @@ export async function chooseUploadUrls(instruction: {
560562
if (Number.isFinite(latency) && latency <= UPLOAD_PRIMARY_MAX_LATENCY_MS) {
561563
return urls([url, ...backups]);
562564
}
563-
if (backups.length === 1) {
564-
return urls([backups[0], url]);
565+
if (!Array.isArray(instruction.backupUrls)) {
566+
// an older server: one backup, chosen on the primary's probe alone
567+
return urls([...backups, url]);
565568
}
566569
const timed = await Promise.all(
567-
backups.map(async (item) => ({
570+
[url, ...backups].map(async (item) => ({
568571
url: item,
569572
...(await measureTlsLatencyByFamily(item)),
570573
})),
@@ -573,13 +576,10 @@ export async function chooseUploadUrls(instruction: {
573576
timed.sort((a, b) =>
574577
a.latency === b.latency ? 0 : a.latency < b.latency ? -1 : 1,
575578
);
576-
return [
577-
...timed.map(({ url: item, family }) => ({
578-
url: item,
579-
...(family ? { family } : {}),
580-
})),
581-
{ url },
582-
];
579+
return timed.map(({ url: item, family }) => ({
580+
url: item,
581+
...(family ? { family } : {}),
582+
}));
583583
}
584584

585585
/**

‎tests/upload-backup.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ describe('upload address selection', () => {
132132
]);
133133
});
134134

135+
test('a primary slow over its preferred family competes with its fastest one', async () => {
136+
// the default upload address on an edge network whose AAAA answer is far
137+
const esa = 'https://up.example';
138+
setup({}, 180, { [esa]: { v4: 8, v6: 360 }, [acc]: 11, [oss]: 560 });
139+
expect(
140+
await chooseUploadUrls({
141+
url: esa,
142+
backupUrl: acc,
143+
backupUrls: [acc, oss],
144+
}),
145+
).toEqual([
146+
{ url: esa, family: 4 },
147+
{ url: acc, family: 4 },
148+
{ url: oss, family: 4 },
149+
]);
150+
});
151+
135152
test('a tie between the families goes to IPv4', async () => {
136153
setup({}, 320, { [acc]: 180, [node]: { v4: 20, v6: 20 } });
137154
expect(
@@ -145,11 +162,12 @@ describe('upload address selection', () => {
145162

146163
test('alternatives that do not answer go behind those that do', async () => {
147164
setup({}, Number.POSITIVE_INFINITY, { [acc]: 200 });
165+
// the primary keeps its place among those that do not answer
148166
expect(await order({ url: oss, backupUrls: [node, acc, entry] })).toEqual([
149167
acc,
168+
oss,
150169
node,
151170
entry,
152-
oss,
153171
]);
154172
});
155173

0 commit comments

Comments
 (0)