(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
This commit is contained in:
Aaron Morris
2025-04-15 11:50:41 -04:00
committed by GitHub
parent 3994336c4d
commit 1e3c8df96d
3 changed files with 12 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@flags-sdk/statsig': patch
---
If using an Edge Config adapter, reduce minimum sync delay for config specs from 5000ms->1000ms
@@ -38,7 +38,9 @@ export async function createEdgeConfigDataAdapter(options: {
* Statsig syncs config specs outside of the request context,
* so we will support it in triggering config spec synchronization in this case.
*/
export const createSyncingHandler = (): null | (() => void) => {
export const createSyncingHandler = (
minSyncDelayMs: number,
): null | (() => void) => {
// Syncing both in Edge Runtime and Node.js for now, as the sync is otherwise
// not working during local development.
//
@@ -50,17 +52,15 @@ export const createSyncingHandler = (): null | (() => void) => {
// - the broken syncing due to issues in Date.now in Edge Runtime would be irrelevant
//
// if (typeof EdgeRuntime === 'undefined') return null;
const timerInterval = 5_000;
let isSyncingConfigSpecs = false;
let nextConfigSpecSyncTime = Date.now() + timerInterval;
let nextConfigSpecSyncTime = Date.now() + minSyncDelayMs;
return (): void => {
if (Date.now() >= nextConfigSpecSyncTime && !isSyncingConfigSpecs) {
try {
isSyncingConfigSpecs = true;
const sync = Statsig.syncConfigSpecs().finally(() => {
isSyncingConfigSpecs = false;
nextConfigSpecSyncTime = Date.now() + timerInterval;
nextConfigSpecSyncTime = Date.now() + minSyncDelayMs;
});
import('@vercel/functions').then(({ waitUntil }) => {
waitUntil(sync);
+2 -1
View File
@@ -100,7 +100,8 @@ export function createStatsigAdapter(options: {
return user != null && typeof user === 'object';
};
const syncHandler = createSyncingHandler();
const minSyncDelayMs = options.edgeConfig ? 1_000 : 5_000;
const syncHandler = createSyncingHandler(minSyncDelayMs);
async function predecide(user?: StatsigUser): Promise<StatsigUser> {
await initialize();