Skip to content

Commit 239adb6

Browse files
committed
Better timing
1 parent c35dd78 commit 239adb6

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

packages/runtime/src/createGatewayRuntime.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ export function createGatewayRuntime<
628628
'GraphOS Managed Federation <br>' + opts.graphRef || '';
629629
let lastSeenId: string;
630630
let lastSupergraphSdl: string;
631-
let minDelaySeconds = 0;
631+
let nextFetchTime: number;
632632
const uplinksParam =
633633
opts.upLink || process.env['APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT'];
634634
const uplinks =
@@ -644,15 +644,13 @@ export function createGatewayRuntime<
644644
const uplinksToUse: string[] = [];
645645
let retries = opts.maxRetries || Math.max(3, uplinks.length);
646646
const fetchSupergraphWithDelay = (): MaybePromise<string> => {
647-
if (minDelaySeconds) {
648-
graphosLogger.info(
649-
`Fetching supergraph with delay: ${minDelaySeconds}s`,
647+
const currentTime = Date.now();
648+
if (nextFetchTime >= currentTime) {
649+
const delay = nextFetchTime - currentTime;
650+
graphosLogger.info(`Fetching supergraph with delay: ${delay}ms`);
651+
return new Promise((resolve) =>
652+
setTimeout(() => resolve(fetchSupergraph()), delay),
650653
);
651-
return new Promise((resolve) => {
652-
setTimeout(() => {
653-
resolve(fetchSupergraph());
654-
}, minDelaySeconds * 1000);
655-
});
656654
}
657655
return fetchSupergraph();
658656
};
@@ -688,21 +686,11 @@ export function createGatewayRuntime<
688686
},
689687
}),
690688
(result) => {
691-
if (
692-
result.minDelaySeconds &&
693-
result.minDelaySeconds > minDelaySeconds
694-
) {
695-
minDelaySeconds = result.minDelaySeconds;
696-
if (config.pollingInterval) {
697-
if (minDelaySeconds <= config.pollingInterval) {
698-
minDelaySeconds = 0;
699-
} else if (minDelaySeconds > config.pollingInterval) {
700-
minDelaySeconds -= config.pollingInterval;
701-
}
702-
}
689+
if (result.minDelaySeconds) {
703690
attemptLogger.debug(
704-
`Setting min delay to ${minDelaySeconds}s`,
691+
`Setting min delay to ${result.minDelaySeconds}s`,
705692
);
693+
nextFetchTime = Date.now() + result.minDelaySeconds * 1000;
706694
}
707695
if ('error' in result) {
708696
attemptLogger.error(

0 commit comments

Comments
 (0)