Skip to content

Commit 1e3c8df

Browse files
authored
(flags-sdk/statsig) Reduce sync delay when using Edge Config (#118)
If using an Edge Config adapter, reduce minimum sync delay for config specs from 5000ms->1000ms
1 parent 3994336 commit 1e3c8df

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

.changeset/small-panthers-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@flags-sdk/statsig': patch
3+
---
4+
5+
If using an Edge Config adapter, reduce minimum sync delay for config specs from 5000ms->1000ms

packages/adapter-statsig/src/edge-runtime-hooks.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export async function createEdgeConfigDataAdapter(options: {
3838
* Statsig syncs config specs outside of the request context,
3939
* so we will support it in triggering config spec synchronization in this case.
4040
*/
41-
export const createSyncingHandler = (): null | (() => void) => {
41+
export const createSyncingHandler = (
42+
minSyncDelayMs: number,
43+
): null | (() => void) => {
4244
// Syncing both in Edge Runtime and Node.js for now, as the sync is otherwise
4345
// not working during local development.
4446
//
@@ -50,17 +52,15 @@ export const createSyncingHandler = (): null | (() => void) => {
5052
// - the broken syncing due to issues in Date.now in Edge Runtime would be irrelevant
5153
//
5254
// if (typeof EdgeRuntime === 'undefined') return null;
53-
54-
const timerInterval = 5_000;
5555
let isSyncingConfigSpecs = false;
56-
let nextConfigSpecSyncTime = Date.now() + timerInterval;
56+
let nextConfigSpecSyncTime = Date.now() + minSyncDelayMs;
5757
return (): void => {
5858
if (Date.now() >= nextConfigSpecSyncTime && !isSyncingConfigSpecs) {
5959
try {
6060
isSyncingConfigSpecs = true;
6161
const sync = Statsig.syncConfigSpecs().finally(() => {
6262
isSyncingConfigSpecs = false;
63-
nextConfigSpecSyncTime = Date.now() + timerInterval;
63+
nextConfigSpecSyncTime = Date.now() + minSyncDelayMs;
6464
});
6565
import('@vercel/functions').then(({ waitUntil }) => {
6666
waitUntil(sync);

packages/adapter-statsig/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ export function createStatsigAdapter(options: {
100100
return user != null && typeof user === 'object';
101101
};
102102

103-
const syncHandler = createSyncingHandler();
103+
const minSyncDelayMs = options.edgeConfig ? 1_000 : 5_000;
104+
const syncHandler = createSyncingHandler(minSyncDelayMs);
104105

105106
async function predecide(user?: StatsigUser): Promise<StatsigUser> {
106107
await initialize();

0 commit comments

Comments
 (0)