@@ -203,9 +203,14 @@ export const getRequestListener = (
203203 errorHandler ?: CustomErrorHandler
204204 overrideGlobalObjects ?: boolean
205205 autoCleanupIncoming ?: boolean
206+ requestsPerForcedGC ?: number
206207 } = { }
207208) => {
208209 const autoCleanupIncoming = options . autoCleanupIncoming ?? true
210+ const requestsPerForcedGC = options . requestsPerForcedGC ?? 0
211+ let requestCount = 0
212+ const gc = global . gc as ( ) => void
213+
209214 if ( options . overrideGlobalObjects !== false && global . Request !== LightweightRequest ) {
210215 Object . defineProperty ( global , 'Request' , {
211216 value : LightweightRequest ,
@@ -214,6 +219,9 @@ export const getRequestListener = (
214219 value : LightweightResponse ,
215220 } )
216221 }
222+ if ( requestsPerForcedGC && ! gc ) {
223+ throw new Error ( '`global.gc` is not available. Please run node with `--expose-gc` flag.' )
224+ }
217225
218226 return async (
219227 incoming : IncomingMessage | Http2ServerRequest ,
@@ -281,6 +289,14 @@ export const getRequestListener = (
281289 }
282290 } )
283291 }
292+
293+ if ( requestsPerForcedGC ) {
294+ requestCount ++
295+ if ( requestCount >= requestsPerForcedGC ) {
296+ gc ( )
297+ requestCount = 0
298+ }
299+ }
284300 } )
285301
286302 res = fetchCallback ( req , { incoming, outgoing } as HttpBindings ) as
0 commit comments