@@ -16,6 +16,7 @@ import {
1616 visibleThreadPullRequests ,
1717} from "@t3tools/shared/threadPullRequests" ;
1818import * as Cause from "effect/Cause" ;
19+ import * as Clock from "effect/Clock" ;
1920import * as Context from "effect/Context" ;
2021import * as Crypto from "effect/Crypto" ;
2122import * as DateTime from "effect/DateTime" ;
@@ -305,10 +306,15 @@ export const make = Effect.gen(function* () {
305306 } ) ) ,
306307 Effect . catchCauseIf (
307308 ( cause ) => ! Cause . hasInterruptsOnly ( cause ) ,
308- ( ) =>
309- Effect . logWarning ( "pull request stack lookup failed" , {
310- key,
311- } ) . pipe ( Effect . as ( null ) ) ,
309+ ( cause ) =>
310+ rateLimitRetryAt ( cause ) !== undefined
311+ ? // The sweep records the pause and holds the host's other reads until it ends.
312+ Effect . sync ( ( ) => retryStacks . add ( key ) ) . pipe (
313+ Effect . andThen ( Effect . failCause ( cause ) ) ,
314+ )
315+ : Effect . logWarning ( "pull request stack lookup failed" , {
316+ key,
317+ } ) . pipe ( Effect . as ( null ) ) ,
312318 ) ,
313319 ) ;
314320 if ( needsStack ) {
@@ -340,32 +346,39 @@ export const make = Effect.gen(function* () {
340346
341347 // Failed host reads by reason: how many, and the first key that failed that way.
342348 const skips = new Map < string , { count : number ; readonly key : string } > ( ) ;
343- yield * Effect . forEach (
344- groups ,
345- ( [ key , entries ] ) =>
346- Effect . suspend ( ( ) => {
347- if ( ! ( ( scope === "all" || requested . has ( key ) ) && isDue ( key , entries , nowMs ) ) ) {
348- return Effect . void ;
349+ const readGroup = ( key : string , entries : ReadonlyArray < LinkEntry > , pauseKey : string ) =>
350+ syncGroup ( key , entries ) . pipe (
351+ Effect . catchCause ( ( cause ) => {
352+ if ( Cause . hasInterruptsOnly ( cause ) ) return Effect . failCause ( cause ) ;
353+ const retryAt = rateLimitRetryAt ( cause ) ;
354+ if ( retryAt !== undefined ) {
355+ pausedUntil . set ( pauseKey , Math . max ( retryAt , pausedUntil . get ( pauseKey ) ?? 0 ) ) ;
349356 }
350- const first = entries [ 0 ] ! ;
351- const pauseKey = `${ first . thread . projectId } \0${ normalizeThreadPullRequestKey ( first . link ) . host } ` ;
352- // Checked as each read starts, so a pause found earlier in this sweep holds the rest.
353- if ( ( pausedUntil . get ( pauseKey ) ?? 0 ) > nowMs ) return Effect . void ;
354- return syncGroup ( key , entries ) . pipe (
355- Effect . catchCause ( ( cause ) => {
356- if ( Cause . hasInterruptsOnly ( cause ) ) return Effect . failCause ( cause ) ;
357- const retryAt = rateLimitRetryAt ( cause ) ;
358- if ( retryAt !== undefined ) {
359- pausedUntil . set ( pauseKey , Math . max ( retryAt , pausedUntil . get ( pauseKey ) ?? 0 ) ) ;
360- }
361- const reason = skipReason ( cause ) ;
362- const skip = skips . get ( reason ) ;
363- if ( skip === undefined ) skips . set ( reason , { count : 1 , key } ) ;
364- else skip . count += 1 ;
365- return Effect . void ;
366- } ) ,
367- ) ;
357+ const reason = skipReason ( cause ) ;
358+ const skip = skips . get ( reason ) ;
359+ if ( skip === undefined ) skips . set ( reason , { count : 1 , key } ) ;
360+ else skip . count += 1 ;
361+ return Effect . void ;
368362 } ) ,
363+ ) ;
364+ yield * Effect . forEach (
365+ groups ,
366+ ( [ key , entries ] ) => {
367+ if ( ! ( ( scope === "all" || requested . has ( key ) ) && isDue ( key , entries , nowMs ) ) ) {
368+ return Effect . void ;
369+ }
370+ const first = entries [ 0 ] ! ;
371+ const pauseKey = `${ first . thread . projectId } \0${ normalizeThreadPullRequestKey ( first . link ) . host } ` ;
372+ // Checked against the clock as each read starts, so a pause found earlier in this sweep
373+ // holds the rest, and one that ends during the sweep lets the rest through.
374+ return Clock . currentTimeMillis . pipe (
375+ Effect . flatMap ( ( startedAtMs ) =>
376+ ( pausedUntil . get ( pauseKey ) ?? 0 ) > startedAtMs
377+ ? Effect . void
378+ : readGroup ( key , entries , pauseKey ) ,
379+ ) ,
380+ ) ;
381+ } ,
369382 // As wide as one batched summary read, so the sweep's reads on a host arrive together and
370383 // GitHub answers them in one request rather than one `gh pr view` apiece.
371384 { concurrency : 25 , discard : true } ,
0 commit comments