Skip to content

Add error handling delay in load function #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dev": "rollup --config --watch",
"lint": "eslint src/ test/",
"fix-lint": "eslint src/ test/ --fix",
"test": "mocha out/test/*.test.{js,cjs,mjs} --timeout 10000"
"test": "mocha out/test/*.test.{js,cjs,mjs} --timeout 15000"
},
"repository": {
"type": "git",
Expand Down
22 changes: 19 additions & 3 deletions src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { AzureAppConfigurationImpl } from "./AzureAppConfigurationImpl";
import { AzureAppConfigurationOptions, MaxRetries, MaxRetryDelayInMs } from "./AzureAppConfigurationOptions";
import * as RequestTracing from "./requestTracing/constants";

const MinDelayForUnhandedError: number = 5000; // 5 seconds

/**
* Loads the data from Azure App Configuration service and returns an instance of AzureAppConfiguration.
* @param connectionString The connection string for the App Configuration store.
Expand All @@ -26,8 +28,11 @@ export async function load(
credentialOrOptions?: TokenCredential | AzureAppConfigurationOptions,
appConfigOptions?: AzureAppConfigurationOptions
): Promise<AzureAppConfiguration> {
const startTimestamp = Date.now();
let client: AppConfigurationClient;
let options: AzureAppConfigurationOptions | undefined;

// input validation
if (typeof connectionStringOrEndpoint === "string" && !instanceOfTokenCredential(credentialOrOptions)) {
const connectionString = connectionStringOrEndpoint;
options = credentialOrOptions as AzureAppConfigurationOptions;
Expand Down Expand Up @@ -55,9 +60,20 @@ export async function load(
throw new Error("A connection string or an endpoint with credential must be specified to create a client.");
}

const appConfiguration = new AzureAppConfigurationImpl(client, options);
await appConfiguration.load();
return appConfiguration;
try {
const appConfiguration = new AzureAppConfigurationImpl(client, options);
await appConfiguration.load();
return appConfiguration;
} catch (error) {
// load() method is called in the application's startup code path.
// Unhandled exceptions cause application crash which can result in crash loops as orchestrators attempt to restart the application.
// 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 propagate fatal errors.
const delay = MinDelayForUnhandedError - (Date.now() - startTimestamp);
if (delay > 0) {
await new Promise((resolve) => setTimeout(resolve, delay));
}
throw error;
}
}

function instanceOfTokenCredential(obj: unknown) {
Expand Down