Skip to content

Commit 8bf7380

Browse files
authored
Remove retry for default interface and cache the used one (#104)
* Remove retry for default interface and cache the used one * new try
1 parent f326ec6 commit 8bf7380

1 file changed

Lines changed: 15 additions & 20 deletions

File tree

lib/tc.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
import shell from './shell.js';
22
import sudo from './sudo.js';
33

4-
const delay = (ms) => new Promise((result) => setTimeout(result, ms));
5-
64
async function getDefaultInterface() {
7-
const command =
8-
"sudo ip route | awk '/default/ {print $5; exit}' | tr -d '\n'";
9-
10-
// Retry a few times since GitHub Actions sometimes temporarily loses the default route
11-
for (let attempt = 0; attempt < 3; attempt++) {
12-
const result = await shell(command);
13-
if (result.stdout.length > 0) {
14-
return result.stdout;
15-
}
16-
if (result.stderr.length > 0) {
17-
throw new Error(
18-
'There was an error getting the default interface:\n\n' + result.stderr
19-
);
20-
}
21-
await delay(1000);
5+
// Try the default route first
6+
const routeResult = await shell(
7+
"sudo ip route | awk '/default/ {print $5; exit}' | tr -d '\n'"
8+
);
9+
if (routeResult.stdout.length > 0) {
10+
return routeResult.stdout;
2211
}
2312

24-
const result = await shell('sudo ip route show');
25-
throw new Error(
26-
`There was an error getting the default interface ${result.stdout}`
13+
// Fall back to finding the interface with a global IP address,
14+
// since the default route may be gone while throttling is active
15+
const addrResult = await shell(
16+
"ip -o -4 addr show scope global | awk '{print $2; exit}'"
2717
);
18+
if (addrResult.stdout.trim().length > 0) {
19+
return addrResult.stdout.trim();
20+
}
21+
22+
throw new Error('Could not find the default network interface');
2823
}
2924

3025
async function moduleProbe() {

0 commit comments

Comments
 (0)