@@ -176,6 +176,8 @@ const LIST_STATS_CACHE_TTL = Duration.seconds(60);
176176const FILES_VIEWED_CACHE_TTL = Duration . seconds ( 15 ) ;
177177/** A diff can stay interactive while its next cached value is fetched off the critical path. */
178178const DIFF_STALE_WINDOW = Duration . minutes ( 10 ) ;
179+ /** How long an expired diff may stay held before it is dropped unread. */
180+ const DIFF_SWEEP_INTERVAL = Duration . minutes ( 1 ) ;
179181/** How long one host's signed-in login is believed without asking its CLI again. */
180182const VIEWER_CACHE_TTL = Duration . minutes ( 10 ) ;
181183const SEARCH_VISIBILITY_TTL = Duration . minutes ( 10 ) ;
@@ -1939,6 +1941,7 @@ export const make = Effect.gen(function* () {
19391941 ) ;
19401942
19411943 const context = yield * Effect . context < never > ( ) ;
1944+ const scope = yield * Effect . scope ;
19421945 /** Runs a refresh as its own fiber, for the reads that answer from a held value first. */
19431946 const runFork = Effect . runForkWith ( context ) ;
19441947
@@ -2659,8 +2662,9 @@ export const make = Effect.gen(function* () {
26592662 if ( oldest !== undefined ) held . delete ( oldest ) ;
26602663 }
26612664 held . set ( key , { at, value } ) ;
2665+ armDiffSweep ( ) ;
26622666 } ) ;
2663- return < E > ( key : string , read : Effect . Effect < PullRequestDiffResult , E > ) => {
2667+ const serve = < E > ( key : string , read : Effect . Effect < PullRequestDiffResult , E > ) => {
26642668 const recorded = read . pipe ( Effect . tap ( ( value ) => record ( key , value ) ) ) ;
26652669 return Effect . flatMap ( Clock . currentTimeMillis , ( now ) => {
26662670 const snapshot = held . get ( key ) ;
@@ -2671,6 +2675,14 @@ export const make = Effect.gen(function* () {
26712675 return revalidate ( recorded ) . pipe ( Effect . as ( snapshot . value ) ) ;
26722676 } ) ;
26732677 } ;
2678+ /** Drops what `serve` would already refuse, and says how many values are still held. */
2679+ const dropExpired = Effect . map ( Clock . currentTimeMillis , ( now ) => {
2680+ for ( const [ key , snapshot ] of held ) {
2681+ if ( now - snapshot . at > staleMs ) held . delete ( key ) ;
2682+ }
2683+ return held . size ;
2684+ } ) ;
2685+ return { serve, dropExpired } ;
26742686 } ) ( ) ;
26752687
26762688 const makeLastGoodRead = < A > ( capacity : number ) => {
@@ -3234,6 +3246,36 @@ export const make = Effect.gen(function* () {
32343246 } ,
32353247 } ,
32363248 ) ;
3249+ /**
3250+ * Both diff holders drop an expired value only when its own key is read again, and a diff's key
3251+ * moves with every push, so a closed page's patches would stay held until the server restarted.
3252+ * One pending sweep at a time, armed in the same step that leaves a value held and re-armed
3253+ * while anything still is, so an idle server keeps no timer.
3254+ */
3255+ let diffSweepPending = false ;
3256+ // Forked from the service's own context, in one step with the flag: a caller's interruption
3257+ // cannot strand the flag, and its request is not kept alive by the wait.
3258+ const armDiffSweep = ( ) => {
3259+ if ( diffSweepPending ) return ;
3260+ diffSweepPending = true ;
3261+ runFork ( Effect . forkIn ( sweepDiffs , scope ) ) ;
3262+ } ;
3263+ const sweepDiffs : Effect . Effect < void > = Effect . sleep ( DIFF_SWEEP_INTERVAL ) . pipe (
3264+ Effect . andThen (
3265+ Effect . suspend ( ( ) => {
3266+ // Cleared before the sweep, so a read that settles during it arms the next one itself.
3267+ diffSweepPending = false ;
3268+ return staleDiff . dropExpired ;
3269+ } ) ,
3270+ ) ,
3271+ // The cache drops its expired entries as its keys are walked.
3272+ Effect . flatMap ( ( stale ) =>
3273+ Effect . map ( Cache . keys ( diffCache ) , ( keys ) => stale + Array . from ( keys ) . length ) ,
3274+ ) ,
3275+ Effect . map ( ( held ) => {
3276+ if ( held > 0 ) armDiffSweep ( ) ;
3277+ } ) ,
3278+ ) ;
32373279 const diff : PullRequestService [ "Service" ] [ "diff" ] = ( input ) => {
32383280 const key = JSON . stringify ( [
32393281 refCacheKey ( input ) ,
@@ -3256,8 +3298,10 @@ export const make = Effect.gen(function* () {
32563298 Effect . uninterruptible ,
32573299 ) ,
32583300 ) ,
3301+ // However the read ends, it may have left a value behind in the cache.
3302+ Effect . ensuring ( Effect . sync ( armDiffSweep ) ) ,
32593303 ) ;
3260- return staleDiff ( key , read ) ;
3304+ return staleDiff . serve ( key , read ) ;
32613305 } ;
32623306
32633307 const filesViewedCache = yield * Cache . makeWith (
0 commit comments