Skip to content

Commit 09fae65

Browse files
committed
Add error handling delay in load function
1 parent e829207 commit 09fae65

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/load.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { AzureAppConfigurationImpl } from "./AzureAppConfigurationImpl";
88
import { AzureAppConfigurationOptions, MaxRetries, MaxRetryDelayInMs } from "./AzureAppConfigurationOptions";
99
import * as RequestTracing from "./requestTracing/constants";
1010

11+
const MinDelayForUnhandedError: number = 5000; // 5 seconds
12+
1113
/**
1214
* Loads the data from Azure App Configuration service and returns an instance of AzureAppConfiguration.
1315
* @param connectionString The connection string for the App Configuration store.
@@ -25,6 +27,26 @@ export async function load(
2527
connectionStringOrEndpoint: string | URL,
2628
credentialOrOptions?: TokenCredential | AzureAppConfigurationOptions,
2729
appConfigOptions?: AzureAppConfigurationOptions
30+
): Promise<AzureAppConfiguration> {
31+
const startTimestamp = Date.now();
32+
try {
33+
return await loadPromise(connectionStringOrEndpoint, credentialOrOptions, appConfigOptions);
34+
} catch (error) {
35+
// load() method is called in the application's startup code path.
36+
// Unhandled exceptions cause application crash which can result in crash loops as orchestrators attempt to restart the application.
37+
// Knowing the intended usage of the provider in startup code path, we mitigate back-to-back crash loops from overloading the server with requests by waiting a minimum time to propogate fatal errors.
38+
const delay = MinDelayForUnhandedError - (Date.now() - startTimestamp);
39+
if (delay > 0) {
40+
await new Promise((resolve) => setTimeout(resolve, delay));
41+
}
42+
throw error;
43+
}
44+
}
45+
46+
async function loadPromise(
47+
connectionStringOrEndpoint: string | URL,
48+
credentialOrOptions?: TokenCredential | AzureAppConfigurationOptions,
49+
appConfigOptions?: AzureAppConfigurationOptions
2850
): Promise<AzureAppConfiguration> {
2951
let client: AppConfigurationClient;
3052
let options: AzureAppConfigurationOptions | undefined;

0 commit comments

Comments
 (0)