Skip to content

Commit 0951b67

Browse files
committed
AppManagingAccount.getContacts: parallel loading with 10s timeout
- Load all collectors in parallel via Promise.allSettled instead of sequential - 10s timeout per collector to prevent hanging on slow/unreachable patient accounts - Error-tolerant: failed collectors are skipped with console.error
1 parent 010f265 commit 0951b67

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

ts/appTemplates/AppManagingAccount.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,36 @@ export class AppManagingAccount extends Application {
7272
const collectors = await this.getCollectors(forceRefresh);
7373
const sources: ContactSource[] = [];
7474

75-
// Collect all invites from all collectors (with error tolerance)
75+
// Collect all invites from all collectors in parallel (with error tolerance + timeout)
7676
const allInvitePairs: Array<{ collector: any; invite: any }> = [];
77-
for (const collector of collectors) {
78-
try {
79-
await collector.init(forceRefresh);
80-
const invites = await collector.getInvites(forceRefresh);
77+
const TIMEOUT_MS = 10000;
78+
79+
const loadCollector = async (collector: any) => {
80+
await collector.init(forceRefresh);
81+
const invites = await collector.getInvites(forceRefresh);
82+
return invites;
83+
};
84+
85+
const results = await Promise.allSettled(
86+
collectors.map(async (collector) => {
87+
const race = Promise.race([
88+
loadCollector(collector),
89+
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), TIMEOUT_MS))
90+
]);
91+
const invites = await race as any[];
92+
return { collector, invites };
93+
})
94+
);
95+
96+
for (const result of results) {
97+
if (result.status === 'fulfilled') {
98+
const { collector, invites } = result.value;
8199
for (const invite of invites) {
82100
sources.push(invite.toContactSource());
83101
allInvitePairs.push({ collector, invite });
84102
}
85-
} catch (e) {
86-
// Skip collectors that fail to load (e.g. network issues)
87-
console.error('Contact: failed loading collector', collector.name, e);
103+
} else {
104+
console.error('Contact: failed loading collector', result.reason);
88105
}
89106
}
90107

0 commit comments

Comments
 (0)