@@ -62,7 +62,10 @@ export const makeUsageHttpClient = Effect.fn("makeUsageHttpClient")(function (
6262 readonly expiresAt : number ;
6363 }
6464 > ( ) ;
65- const accounts = new Map < string , { readonly lock : Semaphore . Semaphore ; retryAt : number } > ( ) ;
65+ const accounts = new Map <
66+ string ,
67+ { readonly lock : Semaphore . Semaphore ; retryAt : number ; generation : number }
68+ > ( ) ;
6669 const cachedClient = HttpClient . transform ( client , ( read , request ) => {
6770 if ( ! isUsageUrl ( request . url ) ) return read ;
6871 return Effect . gen ( function * ( ) {
@@ -72,7 +75,7 @@ export const makeUsageHttpClient = Effect.fn("makeUsageHttpClient")(function (
7275 ] ) ;
7376 let account = accounts . get ( accountKey ) ;
7477 if ( ! account ) {
75- account = { lock : Semaphore . makeUnsafe ( 1 ) , retryAt : 0 } ;
78+ account = { lock : Semaphore . makeUnsafe ( 1 ) , retryAt : 0 , generation : 0 } ;
7679 accounts . set ( accountKey , account ) ;
7780 }
7881 const state = account ;
@@ -105,6 +108,7 @@ export const makeUsageHttpClient = Effect.fn("makeUsageHttpClient")(function (
105108 } ) ,
106109 ) ;
107110 }
111+ const generation = state . generation ;
108112 const result = yield * read . pipe (
109113 Effect . flatMap ( ( response ) =>
110114 Effect . gen ( function * ( ) {
@@ -130,11 +134,16 @@ export const makeUsageHttpClient = Effect.fn("makeUsageHttpClient")(function (
130134 Effect . exit ,
131135 ) ;
132136 const finishedAt = yield * Clock . currentTimeMillis ;
133- cached . set ( key , {
134- accountKey,
135- result,
136- expiresAt : Math . max ( finishedAt + TTL_MS , state . retryAt ) ,
137- } ) ;
137+ if (
138+ generation === state . generation ||
139+ Exit . isFailure ( result ) ||
140+ result . value . status >= 400
141+ )
142+ cached . set ( key , {
143+ accountKey,
144+ result,
145+ expiresAt : Math . max ( finishedAt + TTL_MS , state . retryAt ) ,
146+ } ) ;
138147 const saved = yield * result ;
139148 const response = HttpClientResponse . fromWeb (
140149 request ,
@@ -150,6 +159,8 @@ export const makeUsageHttpClient = Effect.fn("makeUsageHttpClient")(function (
150159 invalidateSuccessfulReads : ( origin : string , authorization : string ) =>
151160 Effect . sync ( ( ) => {
152161 const accountKey = encodeKey ( [ origin , authorization ] ) ;
162+ const account = accounts . get ( accountKey ) ;
163+ if ( account ) account . generation ++ ;
153164 for ( const [ key , saved ] of cached ) {
154165 if (
155166 saved . accountKey === accountKey &&
0 commit comments