Skip to content

Commit b204d2c

Browse files
add lock for refresh operation
1 parent 0027254 commit b204d2c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/AzureAppConfigurationImpl.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IKeyValueAdapter } from "./IKeyValueAdapter.js";
99
import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter.js";
1010
import { DEFAULT_REFRESH_INTERVAL_IN_MS, MIN_REFRESH_INTERVAL_IN_MS } from "./RefreshOptions.js";
1111
import { Disposable } from "./common/disposable.js";
12+
import { Lock } from "./common/lock.js"
1213
import { FEATURE_FLAGS_KEY_NAME, FEATURE_MANAGEMENT_KEY_NAME } from "./featureManagement/constants.js";
1314
import { AzureKeyVaultKeyValueAdapter } from "./keyvault/AzureKeyVaultKeyValueAdapter.js";
1415
import { RefreshTimer } from "./refresh/RefreshTimer.js";
@@ -40,6 +41,8 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
4041
#isInitialLoadCompleted: boolean = false;
4142

4243
// Refresh
44+
#refreshLock: Lock = new Lock(); // lock to prevent "concurrent" async refresh
45+
4346
#refreshInterval: number = DEFAULT_REFRESH_INTERVAL_IN_MS;
4447
#onRefreshListeners: Array<() => any> = [];
4548
/**
@@ -350,6 +353,10 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
350353
throw new Error("Refresh is not enabled for key-values or feature flags.");
351354
}
352355

356+
this.#refreshLock.execute(this.#refreshTasks);
357+
}
358+
359+
async #refreshTasks(): Promise<void> {
353360
const refreshTasks: Promise<boolean>[] = [];
354361
if (this.#refreshEnabled) {
355362
refreshTasks.push(this.#refreshKeyValues());

src/common/lock.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
export class Lock {
5+
#locked = false;
6+
7+
async execute(fn) {
8+
if (this.#locked) {
9+
return; // do nothing
10+
}
11+
this.#locked = true;
12+
try {
13+
await fn();
14+
} finally {
15+
this.#locked = false;
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)