@@ -42,6 +42,8 @@ function withUsageLimits(
4242 return usageLimits ? { ...rest , usageLimits } : rest ;
4343}
4444
45+ const FAILED_CHECK_RECHECK_DELAY = "30 seconds" ;
46+
4547export const makeManagedServerProvider = Effect . fn ( "makeManagedServerProvider" ) ( function * <
4648 Settings ,
4749> ( input : {
@@ -83,6 +85,7 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
8385 checked : false ,
8486 } ) ;
8587 const settingsRef = yield * Ref . make ( initialSettings ) ;
88+ const recheckPendingRef = yield * Ref . make ( false ) ;
8689 const enrichmentFiberRef = yield * Ref . make < Fiber . Fiber < void , unknown > | null > ( null ) ;
8790 const scope = yield * Effect . scope ;
8891
@@ -143,7 +146,7 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
143146
144147 const applySnapshotBase = Effect . fn ( "applySnapshot" ) ( function * (
145148 nextSettings : Settings ,
146- options ?: { readonly forceRefresh ?: boolean } ,
149+ options ?: { readonly forceRefresh ?: boolean ; readonly deferFailure ?: boolean } ,
147150 ) {
148151 const previousSettings = yield * Ref . get ( settingsRef ) ;
149152 const settingsChanged =
@@ -176,6 +179,18 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
176179 // A probe without usage must not erase a report published while its I/O was in flight.
177180 const usageBeforeProbe = ( yield * Ref . get ( snapshotStateRef ) ) . snapshot . usageLimits ;
178181 const probedSnapshot = yield * input . checkProvider ;
182+ // One failed periodic check over a ready provider is often transient; recheck before showing it.
183+ if (
184+ options ?. deferFailure === true &&
185+ probedSnapshot . status === "error" &&
186+ probedSnapshot . auth . status !== "unauthenticated"
187+ ) {
188+ const { snapshot } = yield * Ref . get ( snapshotStateRef ) ;
189+ if ( snapshot . enabled && snapshot . status === "ready" ) {
190+ yield * Ref . set ( recheckPendingRef , true ) ;
191+ return snapshot ;
192+ }
193+ }
179194 const { snapshot : nextSnapshot , generation : nextGeneration } = yield * Ref . modify (
180195 snapshotStateRef ,
181196 ( state ) => {
@@ -319,12 +334,14 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
319334 input . refreshOnInterval !== false &&
320335 ( typeof input . refreshOnInterval !== "function" || input . refreshOnInterval ( snapshot ) ) ;
321336
322- const refreshOnInterval = Effect . fn ( "ManagedServerProvider.refreshOnInterval" ) ( function * ( ) {
337+ const refreshOnInterval = Effect . fn ( "ManagedServerProvider.refreshOnInterval" ) ( function * (
338+ deferFailure : boolean ,
339+ ) {
323340 const { snapshot } = yield * Ref . get ( snapshotStateRef ) ;
324341 if ( ! snapshot . enabled ) return ;
325342 if ( ! needsUsageRecovery ( snapshot ) || ! input . checkUsageLimits ) {
326343 if ( needsProviderRefresh ( snapshot ) ) {
327- yield * applySnapshotBase ( yield * input . getSettings , { forceRefresh : true } ) ;
344+ yield * applySnapshotBase ( yield * input . getSettings , { forceRefresh : true , deferFailure } ) ;
328345 }
329346 return ;
330347 }
@@ -391,10 +408,11 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
391408 ) . pipe ( Effect . forkScoped ) ;
392409
393410 yield * Effect . forever (
394- getRefreshInterval . pipe (
395- Effect . flatMap ( ( refreshInterval ) =>
411+ Effect . all ( [ getRefreshInterval , Ref . getAndSet ( recheckPendingRef , false ) ] ) . pipe (
412+ Effect . flatMap ( ( [ configuredInterval , recheckPending ] ) => {
413+ const refreshInterval = recheckPending ? FAILED_CHECK_RECHECK_DELAY : configuredInterval ;
396414 // @effect -diagnostics-next-line raceFirstWithSleepToTimeout:off - races the interval against a settings-change signal, not a timeout
397- Effect . raceFirst (
415+ return Effect . raceFirst (
398416 Effect . sleep (
399417 Duration . toMillis ( Duration . fromInputUnsafe ( refreshInterval ) ) <= 0
400418 ? "60 seconds"
@@ -407,14 +425,14 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
407425 ? shouldRefreshOnInterval . pipe (
408426 Effect . flatMap ( ( shouldRefresh ) =>
409427 shouldRefresh
410- ? refreshSemaphore . withPermits ( 1 ) ( refreshOnInterval ( ) )
428+ ? refreshSemaphore . withPermits ( 1 ) ( refreshOnInterval ( ! recheckPending ) )
411429 : Effect . void ,
412430 ) ,
413431 )
414432 : Effect . void ,
415433 ) ,
416- ) ,
417- ) ,
434+ ) ;
435+ } ) ,
418436 Effect . ignoreCause ( { log : true } ) ,
419437 ) ,
420438 ) . pipe ( Effect . forkScoped ) ;
0 commit comments