@@ -7,6 +7,7 @@ type Limits = {
77 waiters : number ;
88 waitMs : number ;
99 pressureMs ?: number ;
10+ retryMs ?: number ;
1011} ;
1112type PressureSource = { stalledSince : ( ) => number | undefined ; disconnect : ( ) => void } ;
1213type Active = { bytes : number ; since : number ; source : PressureSource } ;
@@ -37,8 +38,10 @@ export class ReplayBudget {
3738 private readonly heldBySubject = new Map < string , number > ( ) ;
3839 private readonly queues = new Map < string , Waiter [ ] > ( ) ;
3940 private readonly active = new Set < Active > ( ) ;
41+ private readonly recentStalls = new Map < string , number > ( ) ;
4042 private waiting = 0 ;
41- private pressureTimer ?: NodeJS . Timeout ;
43+ private wakeTimer ?: NodeJS . Timeout ;
44+ private wakeAt = Infinity ;
4245 private reclaiming ?: Active ;
4346
4447 constructor ( private readonly limits : Limits = DEFAULT_LIMITS ) { }
@@ -54,14 +57,18 @@ export class ReplayBudget {
5457 source ?: PressureSource ,
5558 ) : Release | Promise < Release > {
5659 checkAbort ( signal ) ;
57- if ( ! this . queues . has ( subject ) && this . fits ( subject , bytes ) )
58- return this . take ( subject , bytes , source ) ;
60+ if ( ! this . queues . has ( subject ) && this . fits ( subject , bytes ) ) {
61+ const release = this . take ( subject , bytes , source ) ;
62+ if ( this . waiting ) this . scheduleWake ( ) ;
63+ return release ;
64+ }
5965 if ( this . waiting >= this . limits . waiters ) throw busy ( ) ;
6066 return new Promise ( ( resolve , reject ) => {
6167 const cancel = ( error : unknown ) => {
6268 this . remove ( subject , waiter ) ;
6369 reject ( error ) ;
6470 this . dispatch ( ) ;
71+ this . scheduleWake ( ) ;
6572 } ;
6673 const abort = ( ) => cancel ( abortError ( ) ) ;
6774 const timeout = setTimeout ( ( ) => cancel ( busy ( ) ) , this . limits . waitMs ) ;
@@ -80,13 +87,17 @@ export class ReplayBudget {
8087 else this . queues . set ( subject , [ waiter ] ) ;
8188 this . waiting ++ ;
8289 signal . addEventListener ( 'abort' , abort , { once : true } ) ;
83- this . schedulePressure ( ) ;
90+ this . scheduleWake ( ) ;
8491 } ) ;
8592 }
8693
8794 private fits ( subject : string , bytes : number ) {
8895 // A legitimate singleton can exceed a budget, but cannot share that budget with another frame.
89- return this . fitsSubject ( subject , bytes ) && this . fitsGateway ( bytes ) ;
96+ return (
97+ this . fitsSubject ( subject , bytes ) &&
98+ this . fitsGateway ( bytes ) &&
99+ this . coolingUntil ( subject , bytes ) === undefined
100+ ) ;
90101 }
91102 private fitsSubject ( subject : string , bytes : number ) {
92103 const owned = this . heldBySubject . get ( subject ) ?? 0 ;
@@ -95,6 +106,24 @@ export class ReplayBudget {
95106 private fitsGateway ( bytes : number , held = this . held ) {
96107 return held === 0 || held + bytes <= this . limits . gateway ;
97108 }
109+ private coolingUntil ( subject : string , bytes : number , now = performance . now ( ) ) {
110+ const until = this . recentStalls . get ( subject ) ;
111+ if ( until === undefined ) return undefined ;
112+ if ( until <= now ) {
113+ this . recentStalls . delete ( subject ) ;
114+ return undefined ;
115+ }
116+ // Keep room for a new ordinary frame only after observed pressure and only near capacity.
117+ const headroom = Math . min ( this . limits . subject , this . limits . gateway ) ;
118+ if ( this . held && this . held + bytes + headroom > this . limits . gateway ) return until ;
119+ return undefined ;
120+ }
121+ private rememberStall ( subject : string ) {
122+ this . recentStalls . delete ( subject ) ;
123+ this . recentStalls . set ( subject , performance . now ( ) + ( this . limits . retryMs ?? 2000 ) ) ;
124+ if ( this . recentStalls . size > DEFAULT_LIMITS . waiters )
125+ this . recentStalls . delete ( this . recentStalls . keys ( ) . next ( ) . value ! ) ;
126+ }
98127
99128 private take ( subject : string , bytes : number , source ?: PressureSource ) : Release {
100129 this . held += bytes ;
@@ -107,11 +136,15 @@ export class ReplayBudget {
107136 released = true ;
108137 this . held -= bytes ;
109138 if ( active ) this . active . delete ( active ) ;
110- if ( this . reclaiming === active ) this . reclaiming = undefined ;
139+ if ( active && this . reclaiming === active ) {
140+ this . reclaiming = undefined ;
141+ this . rememberStall ( subject ) ;
142+ }
111143 const remaining = this . heldBySubject . get ( subject ) ! - bytes ;
112144 if ( remaining ) this . heldBySubject . set ( subject , remaining ) ;
113145 else this . heldBySubject . delete ( subject ) ;
114146 this . dispatch ( ) ;
147+ this . scheduleWake ( ) ;
115148 } ;
116149 }
117150
@@ -140,34 +173,54 @@ export class ReplayBudget {
140173 }
141174 if ( ! granted ) break ;
142175 }
143- this . schedulePressure ( ) ;
144176 }
145177
146178 private gatewayWaiter ( ) {
147179 for ( const [ subject , queue ] of this . queues ) {
148180 const first = queue [ 0 ] ! ;
149- if ( this . fitsSubject ( subject , first . bytes ) && ! this . fitsGateway ( first . bytes ) ) return first ;
181+ if (
182+ this . fitsSubject ( subject , first . bytes ) &&
183+ ! this . fitsGateway ( first . bytes ) &&
184+ this . coolingUntil ( subject , first . bytes ) === undefined
185+ )
186+ return first ;
150187 }
151188 return undefined ;
152189 }
153190
154- private schedulePressure ( ) {
155- if ( this . reclaiming || ! this . active . size || ! this . gatewayWaiter ( ) ) {
156- clearTimeout ( this . pressureTimer ) ;
157- this . pressureTimer = undefined ;
191+ private scheduleWake ( ) {
192+ const now = performance . now ( ) ;
193+ const pressure = this . limits . pressureMs ?? 1500 ;
194+ let at =
195+ ! this . reclaiming && this . active . size && this . gatewayWaiter ( )
196+ ? now + Math . min ( 100 , pressure )
197+ : Infinity ;
198+ for ( const [ subject , queue ] of this . queues ) {
199+ const bytes = queue [ 0 ] ! . bytes ;
200+ const cooled = this . coolingUntil ( subject , bytes , now ) ;
201+ if ( cooled !== undefined ) at = Math . min ( at , cooled ) ;
202+ else if ( this . fitsSubject ( subject , bytes ) && this . fitsGateway ( bytes ) ) at = now ;
203+ }
204+ if ( ! Number . isFinite ( at ) ) {
205+ clearTimeout ( this . wakeTimer ) ;
206+ this . wakeTimer = undefined ;
207+ this . wakeAt = Infinity ;
158208 return ;
159209 }
160- if ( this . pressureTimer ) return ;
161- const delay = this . limits . pressureMs ?? 1500 ;
162- this . pressureTimer = setTimeout (
210+ if ( this . wakeTimer && this . wakeAt <= at ) return ;
211+ clearTimeout ( this . wakeTimer ) ;
212+ this . wakeAt = at ;
213+ this . wakeTimer = setTimeout (
163214 ( ) => {
164- this . pressureTimer = undefined ;
215+ this . wakeTimer = undefined ;
216+ this . wakeAt = Infinity ;
217+ this . dispatch ( ) ;
165218 const waiting = this . gatewayWaiter ( ) ;
166- if ( waiting && performance . now ( ) - waiting . since >= delay )
167- this . reclaimStalled ( waiting , delay ) ;
168- this . schedulePressure ( ) ;
219+ if ( ! this . reclaiming && waiting && performance . now ( ) - waiting . since >= pressure )
220+ this . reclaimStalled ( waiting , pressure ) ;
221+ this . scheduleWake ( ) ;
169222 } ,
170- Math . min ( 100 , delay ) ,
223+ Math . max ( 1 , Math . ceil ( at - now ) ) ,
171224 ) . unref ( ) ;
172225 }
173226
0 commit comments