Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dmanjunath committed Jan 11, 2025
1 parent bfc111d commit edfd0fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
7 changes: 3 additions & 4 deletions src/service-worker/src/armada/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Adapter} from '../adapter';

export interface ContentAPIClient {
getContent(resource: string, host: string, retry?: string, cacheBust?: boolean, signal?: AbortSignal): Promise<Response>;
getContent(resource: string, host: string, retry?: string, cacheBust?: boolean): Promise<Response>;
}

export type TopologyAPIClient = {
Expand Down Expand Up @@ -31,8 +31,7 @@ export class ArmadaAPIClientImpl implements ArmadaAPIClient {
resource: string,
host: string,
retry?: string,
cacheBust?: boolean,
signal?: AbortSignal
cacheBust?: boolean
): Promise<Response> {
const url = new URL('/v1/content', `${this.protocol}//${host}`);
url.searchParams.append('project_id', this.projectId);
Expand All @@ -44,7 +43,7 @@ export class ArmadaAPIClientImpl implements ArmadaAPIClient {
url.searchParams.append('retry', retry);
}

const req = this.adapter.newRequest(url.toString(), { signal });
const req = this.adapter.newRequest(url.toString());
return this.fetcher.fetch(req);
}

Expand Down
29 changes: 20 additions & 9 deletions src/service-worker/src/armada/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ThrowingFetcher {

export class ArmadaLazyAssetGroup extends LazyAssetGroup {
static readonly MAX_ATTEMPTS = 5;
private static readonly TIMEOUT_MS = 100;
private static readonly TIMEOUT_MS = 250;

constructor(
adapter: Adapter, idle: IdleScheduler, config: AssetGroupConfig, hashes: Map<string, string>,
Expand All @@ -43,13 +43,22 @@ export class ArmadaLazyAssetGroup extends LazyAssetGroup {
let completedRequests = 0;
const controllers: AbortController[] = [];

// Helper to abort all controllers except the specified index
const abortOtherControllers = (exceptIndex?: number) => {
controllers.forEach((ctrl, i) => {
if (ctrl && (exceptIndex === undefined || i !== exceptIndex)) {
ctrl.abort();
}
});
};

// Helper to start a request to a node
const startNodeRequest = async (node: string, index: number) => {
const controller = new AbortController();
controllers[index] = controller;

try {
const response = await this.apiClient.getContent(url, node, undefined, false, controller.signal);
const response = await this.apiClient.getContent(url, node, undefined, false);

// Don't process if request was aborted
if (controller.signal.aborted) {
Expand All @@ -74,12 +83,7 @@ export class ArmadaLazyAssetGroup extends LazyAssetGroup {

// Store successful response and cancel other requests
successfulResponse = response;
// Cancel all other requests
controllers.forEach((ctrl, i) => {
if (i !== index && ctrl) {
ctrl.abort();
}
});
abortOtherControllers(index);
} catch (err) {
if (err.name !== 'AbortError') {
const msg = `Error fetching content: node=${node} resource=${url} error=${err}`;
Expand All @@ -96,9 +100,11 @@ export class ArmadaLazyAssetGroup extends LazyAssetGroup {
const checkStatus = () => {
if (successfulResponse) {
clearTimeout(timeoutId);
abortOtherControllers();
resolve(successfulResponse);
} else if (completedRequests === nodes.length) {
clearTimeout(timeoutId);
abortOtherControllers();
reject(new SwContentNodesFetchFailureError(
`Failed to fetch content: resource=${url} attempts=${nodes.length}`,
undefined,
Expand All @@ -123,7 +129,12 @@ export class ArmadaLazyAssetGroup extends LazyAssetGroup {
timeoutId = setTimeout(scheduleNext, ArmadaLazyAssetGroup.TIMEOUT_MS);
});

return resultPromise;
try {
return await resultPromise;
} finally {
// Ensure all controllers are aborted when the promise settles
abortOtherControllers();
}
}

/**
Expand Down

0 comments on commit edfd0fe

Please sign in to comment.