Skip to content
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

Avoid multiple refresh #136

Merged
merged 7 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions src/AzureAppConfigurationImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IKeyValueAdapter } from "./IKeyValueAdapter.js";
import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter.js";
import { DEFAULT_REFRESH_INTERVAL_IN_MS, MIN_REFRESH_INTERVAL_IN_MS } from "./RefreshOptions.js";
import { Disposable } from "./common/disposable.js";
import { Lock } from "./common/lock.js";
import { FEATURE_FLAGS_KEY_NAME, FEATURE_MANAGEMENT_KEY_NAME } from "./featureManagement/constants.js";
import { AzureKeyVaultKeyValueAdapter } from "./keyvault/AzureKeyVaultKeyValueAdapter.js";
import { RefreshTimer } from "./refresh/RefreshTimer.js";
Expand Down Expand Up @@ -40,6 +41,8 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
#isInitialLoadCompleted: boolean = false;

// Refresh
#refreshLock: Lock = new Lock(); // lock to prevent "concurrent" async refresh

#refreshInterval: number = DEFAULT_REFRESH_INTERVAL_IN_MS;
#onRefreshListeners: Array<() => any> = [];
/**
Expand Down Expand Up @@ -350,6 +353,10 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
throw new Error("Refresh is not enabled for key-values or feature flags.");
}

await this.#refreshLock.execute(this.#refreshTasks.bind(this));
}

async #refreshTasks(): Promise<void> {
const refreshTasks: Promise<boolean>[] = [];
if (this.#refreshEnabled) {
refreshTasks.push(this.#refreshKeyValues());
Expand Down
18 changes: 18 additions & 0 deletions src/common/lock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export class Lock {
zhiyuanliang-ms marked this conversation as resolved.
Show resolved Hide resolved
#locked = false;
zhiyuanliang-ms marked this conversation as resolved.
Show resolved Hide resolved

async execute(fn) {
if (this.#locked) {
return; // do nothing
}
this.#locked = true;
try {
await fn();
} finally {
this.#locked = false;
}
}
}