diff --git a/packages/api-redundancy/src/config.ts b/packages/api-redundancy/src/config.ts index c27962b..643fab2 100644 --- a/packages/api-redundancy/src/config.ts +++ b/packages/api-redundancy/src/config.ts @@ -1,24 +1,3 @@ -/** - * Callback for "timeout" configuration property. - * Returns number of milliseconds to wait before failing query, while there are pending resources. - */ -export interface TimeoutCallback { - ( - startTime: number // Start time - ): number; -} - -/** - * Callback for "rotate" configuration property. - * Returns number of milliseconds to wait before trying next resource. - */ -export interface RotationTimeoutCallback { - ( - queriesSent: number, // Number of queries sent, starts with 1 for timeout after first resource - startTime: number // Query start time - ): number; -} - /** * Resource to rotate (usually hostname or partial URL) */ @@ -30,8 +9,8 @@ export type RedundancyResource = string; export interface RedundancyConfig { resources: RedundancyResource[]; // Resources to rotate index: number; // Start index - timeout: number | TimeoutCallback; // Timeout for error (full timeout = timeout + resources.length * rotate) - rotate: number | RotationTimeoutCallback; // Timeout for one query + timeout: number; // Timeout for error (full timeout = timeout + resources.length * rotate) + rotate: number; // Timeout for one query random: boolean; // True if order should be randomised dataAfterTimeout: boolean; // True if data can be sent after timeout } diff --git a/packages/api-redundancy/src/index.ts b/packages/api-redundancy/src/index.ts index 1e3afee..3165e8b 100644 --- a/packages/api-redundancy/src/index.ts +++ b/packages/api-redundancy/src/index.ts @@ -117,10 +117,6 @@ export function initRedundancy(cfg: Partial): Redundancy { if (doneCallback) { doneCallback(data, error); } - }, - (newIndex) => { - // Update start index - config.index = newIndex; } ); queries.push(query); diff --git a/packages/api-redundancy/src/query.ts b/packages/api-redundancy/src/query.ts index 031ac4a..2cbbf15 100644 --- a/packages/api-redundancy/src/query.ts +++ b/packages/api-redundancy/src/query.ts @@ -76,8 +76,7 @@ export function sendQuery( config: RedundancyConfig, payload: unknown, query: QueryModuleCallback, - done?: QueryDoneCallback, - success?: QueryUpdateIndexCallback + done?: QueryDoneCallback ): GetQueryStatus { // Get number of resources const resourcesCount = config.resources.length; @@ -270,11 +269,11 @@ export function sendQuery( resetTimer(); clearQueue(); - // Update index in Redundancy - if (success && !config.random) { + // Update index in configuration + if (!config.random) { const index = config.resources.indexOf(item.resource); if (index !== -1 && index !== config.index) { - success(index); + config.index = index; } } @@ -302,22 +301,16 @@ export function sendQuery( if (resource === void 0) { // Nothing to execute: wait for final timeout before failing if (queue.length) { - const timeout: number = - typeof config.timeout === 'function' - ? config.timeout(startTime) - : config.timeout; - if (timeout) { - // Last timeout before failing to allow late response - timer = setTimeout(() => { - resetTimer(); - if (status === 'pending') { - // Clear queue - clearQueue(); - failQuery(); - } - }, timeout); - return; - } + // Last timeout before failing to allow late response + timer = setTimeout(() => { + resetTimer(); + if (status === 'pending') { + // Clear queue + clearQueue(); + failQuery(); + } + }, config.timeout); + return; } // Fail @@ -341,14 +334,8 @@ export function sendQuery( // Bump next index queriesSent++; - // Get timeout for next item - const timeout: number = - typeof config.rotate === 'function' - ? config.rotate(queriesSent, startTime) - : config.rotate; - // Create timer - timer = setTimeout(execNext, timeout); + timer = setTimeout(execNext, config.rotate); // Execute it query(resource, payload, item); diff --git a/packages/api-redundancy/tests/multiple-resources-test.ts b/packages/api-redundancy/tests/multiple-resources-test.ts index 291b140..9e9d18e 100644 --- a/packages/api-redundancy/tests/multiple-resources-test.ts +++ b/packages/api-redundancy/tests/multiple-resources-test.ts @@ -68,9 +68,6 @@ describe('Multiple resources', () => { expect(diff).toBeLessThan(50); done(); - }, - () => { - done('This should not have been called'); } ); @@ -101,7 +98,6 @@ describe('Multiple resources', () => { const startTime = Date.now(); let sentQuery = 0; let itemAborted = false; - let parentUpdated = false; // Send query const getStatus = sendQuery( @@ -174,20 +170,12 @@ describe('Multiple resources', () => { expect(status.queriesSent).toEqual(2); expect(status.queriesPending).toEqual(0); - // Parent should have been updated - expect(parentUpdated).toEqual(true); - // Delay between first and second queries const diff = Date.now() - startTime; expect(diff).toBeGreaterThan(50); expect(diff).toBeLessThan(150); done(); - }, - (newIndex) => { - // Start index should be updated to 1 - expect(newIndex).toEqual(1); - parentUpdated = true; } ); @@ -300,9 +288,6 @@ describe('Multiple resources', () => { expect(diff).toBeLessThan(120); done(); - }, - () => { - done('This should have never been called'); } ); @@ -385,9 +370,6 @@ describe('Multiple resources', () => { expect(diff).toBeLessThan(120); done(); - }, - () => { - done('This should have never been called'); } ); @@ -448,9 +430,6 @@ describe('Multiple resources', () => { expect(diff).toBeLessThan(170); done(); - }, - () => { - done('This should have never been called'); } );