export async function recursivelyGetIdList()

in lambda/rest-endpoints/src/helpers.ts [41:82]


export async function recursivelyGetIdList(
	uidList: string[],
	prevResults: RecipeIndexEntry[],
	attempt?: number,
): Promise<RecipeIndexEntry[]> {
	const batchSize = 50; //ok so this is finger-in-the-air
	const maxAttempts = 10;

	const toLookUp = uidList.slice(0, batchSize);
	try {
		const results = await multipleRecipesByUid(toLookUp);
		if (toLookUp.length == uidList.length) {
			//we got everything
			return prevResults.concat(results);
		} else {
			return recursivelyGetIdList(
				uidList.slice(batchSize),
				prevResults.concat(results),
				0,
			);
		}
	} catch (err) {
		console.warn(err);
		if (err instanceof ProvisionedThroughputExceededException) {
			const nextAttempt = attempt ? attempt + 1 : 1;
			if (nextAttempt > maxAttempts) {
				console.error(
					`Unable to make request after ${maxAttempts} attempts, giving up`,
				);
				throw err;
			}

			console.warn(
				`Attempt ${nextAttempt} - caught ProvisionedThroughputException`,
			);
			await asyncTimeout(100 + 20 ** nextAttempt);
			return recursivelyGetIdList(uidList, prevResults, nextAttempt);
		} else {
			throw err;
		}
	}
}