@@ -26,6 +26,8 @@ import { detachStackFrame } from "../git/detachStackFrame.ts";
2626import { forkParked } from "../serverActivation.ts" ;
2727
2828const CONCURRENT_READS = 512 ;
29+ /** Invalidation excludes only reads whose scopes share its stripe, not every PR read. */
30+ const SCOPE_STRIPES = 64 ;
2931// Entry filenames are the URI-encoded `<storeId>:<key>`; the scope revisions file has
3032// no store id. Effect before 4.1 wrote `<storeId><key>`, still pruned here.
3133const STORE_ID = "pr-v2" ;
@@ -87,6 +89,11 @@ export const make = Effect.gen(function* () {
8789 const clock = yield * Clock . Clock ;
8890 let enabled = true ;
8991 const lock = yield * Semaphore . make ( CONCURRENT_READS ) ;
92+ const stripes = yield * Effect . forEach ( Array . from ( { length : SCOPE_STRIPES } ) , ( ) =>
93+ Semaphore . make ( CONCURRENT_READS ) ,
94+ ) ;
95+ const stripeOf = ( scope : string ) => ( Hash . string ( scope ) >>> 0 ) % SCOPE_STRIPES ;
96+ const revisionWrites = yield * Semaphore . make ( 1 ) ;
9097 const digest = ( key : string ) =>
9198 digester . digest ( Digest . sha256 , new TextEncoder ( ) . encode ( key ) ) . pipe ( Effect . map ( Hex . encode ) ) ;
9299 const revisions = yield * Cache . makeWith (
@@ -162,6 +169,12 @@ export const make = Effect.gen(function* () {
162169 PersistenceError : ( ) => read ,
163170 SchemaError : ( ) => read ,
164171 } ) ,
172+ // A read stores its result under the revisions it started with, so an invalidation of
173+ // one of its scopes must wait for it; reads of other scopes run on.
174+ ( effect ) =>
175+ [ ...new Set ( scopes . map ( stripeOf ) ) ]
176+ . toSorted ( ( left , right ) => left - right )
177+ . reduceRight ( ( inner , stripe ) => stripes [ stripe ] ! . withPermits ( 1 ) ( inner ) , effect ) ,
165178 lock . withPermits ( 1 ) ,
166179 ) ;
167180 } , detachStackFrame ) ,
@@ -182,7 +195,8 @@ export const make = Effect.gen(function* () {
182195 return Effect . logWarning ( "PR cache disabled after clearing failed" ) ;
183196 } ) ,
184197 Effect . uninterruptible ,
185- lock . withPermits ( CONCURRENT_READS ) ,
198+ revisionWrites . withPermits ( 1 ) ,
199+ ( effect , scope ) => stripes [ stripeOf ( scope ) ] ! . withPermits ( CONCURRENT_READS ) ( effect ) ,
186200 detachStackFrame ,
187201 ) ,
188202 } ) ;
0 commit comments