@@ -3,7 +3,9 @@ import * as Context from "effect/Context";
33import * as Effect from "effect/Effect" ;
44import * as Layer from "effect/Layer" ;
55import * as Ref from "effect/Ref" ;
6- import * as Schema from "effect/Schema" ;
6+ import * as Predicate from "effect/Predicate" ;
7+
8+ import * as SourceControlRateLimit from "./SourceControlRateLimit.ts" ;
79
810const GRAPHQL_RESERVE_RATIO = 0.1 ;
911const RATE_LIMIT_SELECTION = "rateLimit { cost limit remaining resetAt }" ;
@@ -12,34 +14,17 @@ interface GraphQlBudgetSnapshot {
1214 readonly cost : number ;
1315 readonly limit : number ;
1416 readonly remaining : number ;
15- readonly resetAt : string ;
1617 readonly resetAtMs : number ;
1718}
1819
19- export class GitHubGraphQlBudgetPausedError extends Schema . TaggedErrorClass < GitHubGraphQlBudgetPausedError > ( ) (
20- "GitHubGraphQlBudgetPausedError" ,
21- {
22- host : Schema . String ,
23- resetAt : Schema . String ,
24- } ,
25- ) {
26- get detail ( ) : string {
27- return `GraphQL reads for ${ this . host } are paused until ${ this . resetAt } .` ;
28- }
29-
30- override get message ( ) : string {
31- return `GitHub ${ this . detail } ` ;
32- }
33- }
34-
3520export class GitHubGraphQlBudget extends Context . Service <
3621 GitHubGraphQlBudget ,
3722 {
3823 readonly query : (
3924 host : string ,
4025 document : string ,
4126 options ?: { readonly allowReserve : boolean } ,
42- ) => Effect . Effect < string , GitHubGraphQlBudgetPausedError > ;
27+ ) => Effect . Effect < string , SourceControlRateLimit . SourceControlRateLimitPausedError > ;
4328 readonly observe : ( host : string , raw : string ) => Effect . Effect < void > ;
4429 }
4530> ( ) ( "t3/sourceControl/githubGraphQlBudget" ) { }
@@ -48,14 +33,14 @@ function hostKey(host: string): string {
4833 return host . trim ( ) . toLowerCase ( ) ;
4934}
5035
51- function isRecord ( value : unknown ) : value is Record < string , unknown > {
52- return typeof value === "object" && value !== null ;
53- }
54-
5536function snapshotFrom ( raw : string ) : GraphQlBudgetSnapshot | null {
5637 try {
5738 const parsed : unknown = JSON . parse ( raw ) ;
58- if ( ! isRecord ( parsed ) || ! isRecord ( parsed . data ) || ! isRecord ( parsed . data . rateLimit ) ) {
39+ if (
40+ ! Predicate . isObject ( parsed ) ||
41+ ! Predicate . isObject ( parsed . data ) ||
42+ ! Predicate . isObject ( parsed . data . rateLimit )
43+ ) {
5944 return null ;
6045 }
6146 const { cost, limit, remaining, resetAt } = parsed . data . rateLimit ;
@@ -74,7 +59,7 @@ function snapshotFrom(raw: string): GraphQlBudgetSnapshot | null {
7459 return null ;
7560 }
7661 const resetAtMs = Date . parse ( resetAt ) ;
77- return Number . isFinite ( resetAtMs ) ? { cost, limit, remaining, resetAt , resetAtMs } : null ;
62+ return Number . isFinite ( resetAtMs ) ? { cost, limit, remaining, resetAtMs } : null ;
7863 } catch {
7964 return null ;
8065 }
@@ -99,7 +84,7 @@ export const make = Effect.gen(function* () {
9984 function * ( host , document , options ) {
10085 if ( ! isReadOperation ( document ) ) return document ;
10186 const now = yield * Clock . currentTimeMillis ;
102- const resetAt = yield * Ref . modify ( snapshots , ( current ) => {
87+ const retryAt = yield * Ref . modify ( snapshots , ( current ) => {
10388 const key = hostKey ( host ) ;
10489 const snapshot = current . get ( key ) ;
10590 if ( snapshot === undefined ) return [ null , current ] as const ;
@@ -110,14 +95,18 @@ export const make = Effect.gen(function* () {
11095 }
11196 const remaining = Math . max ( 0 , snapshot . remaining - Math . max ( 1 , snapshot . cost ) ) ;
11297 if ( options ?. allowReserve !== true && remaining < snapshot . limit * GRAPHQL_RESERVE_RATIO ) {
113- return [ snapshot . resetAt , current ] as const ;
98+ return [ snapshot . resetAtMs , current ] as const ;
11499 }
115100 const next = new Map ( current ) ;
116101 next . set ( key , { ...snapshot , remaining } ) ;
117102 return [ null , next ] as const ;
118103 } ) ;
119- if ( resetAt !== null ) {
120- return yield * new GitHubGraphQlBudgetPausedError ( { host, resetAt } ) ;
104+ if ( retryAt !== null ) {
105+ return yield * new SourceControlRateLimit . SourceControlRateLimitPausedError ( {
106+ provider : "github" ,
107+ host : hostKey ( host ) ,
108+ retryAt,
109+ } ) ;
121110 }
122111 return withRateLimit ( document ) ;
123112 } ,
0 commit comments