Skip to content

Commit a7bb2cd

Browse files
committed
Add a 1s timeout to each part of the interceptor query
The main target here is the ADB interceptor, which can be slow on some systems. Now, it will just return false/undefined until it can get a clear result and everything else can keep working in the meantime.
1 parent 93f9c2f commit a7bb2cd

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/api-server.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,19 @@ const typeDefs = `
5454
scalar Json
5555
scalar Error
5656
scalar Void
57-
`
57+
`;
58+
59+
// Wait for a promise, falling back to defaultValue on error or timeout
60+
const withFallback = <R>(p: Promise<R>, timeoutMs: number, defaultValue: R) =>
61+
Promise.race([
62+
p.catch((error) => {
63+
reportError(error);
64+
return defaultValue;
65+
}),
66+
delay(timeoutMs).then(() => defaultValue)
67+
]);
68+
69+
const INTERCEPTOR_TIMEOUT = 1000;
5870

5971
const buildResolvers = (
6072
config: HtkConfig,
@@ -118,10 +130,11 @@ const buildResolvers = (
118130

119131
Interceptor: {
120132
isActivable: (interceptor: Interceptor) => {
121-
return interceptor.isActivable().catch((e) => {
122-
reportError(e);
123-
return false;
124-
});
133+
return withFallback(
134+
interceptor.isActivable(),
135+
INTERCEPTOR_TIMEOUT,
136+
false
137+
);
125138
},
126139
isActive: (interceptor: Interceptor, args: _.Dictionary<any>) => {
127140
try {
@@ -132,10 +145,14 @@ const buildResolvers = (
132145
}
133146
},
134147
metadata: async (interceptor: Interceptor) => {
148+
if (!interceptor.getMetadata) return undefined;
149+
135150
try {
136-
return interceptor.getMetadata
137-
? await interceptor.getMetadata()
138-
: undefined;
151+
return await withFallback(
152+
interceptor.getMetadata(),
153+
INTERCEPTOR_TIMEOUT,
154+
undefined
155+
);
139156
} catch (e) {
140157
reportError(e);
141158
return undefined;

0 commit comments

Comments
 (0)