mirror of
https://github.com/vercel/flags.git
synced 2026-09-19 03:49:23 +08:00
(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:
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user