From bf5d4fe4aaba8988859c29351a8c101cfe09bf80 Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Mon, 8 Jan 2024 07:03:07 -0500 Subject: [PATCH] Added comments to retry and backoff logic --- examples/retry-backoff.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/examples/retry-backoff.ts b/examples/retry-backoff.ts index c6f31090..e8b3fc3a 100644 --- a/examples/retry-backoff.ts +++ b/examples/retry-backoff.ts @@ -20,13 +20,26 @@ export function* retryBackoffExample( { attempts = 5, startDelay = 5, maxDelay = 200 } = {}, ): Operation { try { + // This is the initial operation, + // if it succeeds then the operation will be complete return yield* op(); } catch (e) { + // if we encounter error that we're not expecting then throw + // and do not proceed any further if (!isKnownError(e)) { throw e; } } + // we're here because we encountered a known issue, + // we want to run the retry logic attempting up to 5 attempts + // pause between each attempt and double the delay with each attempt + // trigger a timeout if we reach maxDelay + // we do this by setting up a race between retry logic and + // the timeout. If retry logic succeeds, timeout will be halted + // automatically. If timeout reaches the throw, it'll interrupt the + // retry logic automatically. + let lastError: Error; function* retry() { let delay = startDelay;