Skip to content

Commit 9c4307d

Browse files
committed
chore: use sessionCache and sessionList
1 parent 632334a commit 9c4307d

File tree

2 files changed

+33
-38
lines changed

2 files changed

+33
-38
lines changed

packages/node-http-handler/src/node-http2-handler.spec.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ describe(NodeHttp2Handler.name, () => {
4747
});
4848

4949
afterEach(() => {
50-
// @ts-ignore: access private property
51-
const sessionPool = nodeH2Handler.sessionPool;
52-
for (const [, session] of sessionPool) {
53-
session.destroy();
54-
}
55-
sessionPool.clear();
50+
nodeH2Handler.destroy();
5651
});
5752

5853
it("has metadata", () => {
@@ -219,30 +214,30 @@ describe(NodeHttp2Handler.name, () => {
219214
});
220215

221216
describe("destroy", () => {
222-
it("destroys session and clears sessionPool", async () => {
217+
it("destroys session and clears sessionCache", async () => {
223218
await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {});
224219

225220
// @ts-ignore: access private property
226-
const session: ClientHttp2Session = nodeH2Handler.sessions[0];
221+
const session: ClientHttp2Session = nodeH2Handler.sessionList[0];
227222

228223
// @ts-ignore: access private property
229-
expect(nodeH2Handler.sessionPool.size).toBe(1);
224+
expect(nodeH2Handler.sessionCache.size).toBe(1);
230225
// @ts-ignore: access private property
231-
expect(nodeH2Handler.sessions.length).toBe(1);
226+
expect(nodeH2Handler.sessionList.length).toBe(1);
232227
expect(session.destroyed).toBe(false);
233228
nodeH2Handler.destroy();
234229
// @ts-ignore: access private property
235-
expect(nodeH2Handler.sessionPool.size).toBe(0);
230+
expect(nodeH2Handler.sessionCache.size).toBe(0);
236231
// @ts-ignore: access private property
237-
expect(nodeH2Handler.sessions.length).toBe(0);
232+
expect(nodeH2Handler.sessionList.length).toBe(0);
238233
expect(session.destroyed).toBe(true);
239234
});
240235
});
241236

242237
describe("abortSignal", () => {
243238
it("will not create session if request already aborted", async () => {
244239
// @ts-ignore: access private property
245-
expect(nodeH2Handler.sessionPool.size).toBe(0);
240+
expect(nodeH2Handler.sessionCache.size).toBe(0);
246241
await expect(
247242
nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {
248243
abortSignal: {
@@ -252,15 +247,15 @@ describe(NodeHttp2Handler.name, () => {
252247
})
253248
).rejects.toHaveProperty("name", "AbortError");
254249
// @ts-ignore: access private property
255-
expect(nodeH2Handler.sessionPool.size).toBe(0);
250+
expect(nodeH2Handler.sessionCache.size).toBe(0);
256251
});
257252

258253
it("will not create request on session if request already aborted", async () => {
259254
// Create a session by sending a request.
260255
await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {});
261256

262257
// @ts-ignore: access private property
263-
const session: ClientHttp2Session = nodeH2Handler.sessions[0];
258+
const session: ClientHttp2Session = nodeH2Handler.sessionList[0];
264259
const requestSpy = jest.spyOn(session, "request");
265260

266261
await expect(
@@ -347,15 +342,15 @@ describe(NodeHttp2Handler.name, () => {
347342

348343
const authority = `${protocol}//${hostname}:${port}`;
349344
// @ts-ignore: access private property
350-
const session: ClientHttp2Session = nodeH2Handler.sessions[0];
345+
const session: ClientHttp2Session = nodeH2Handler.sessionList[0];
351346
expect(session.closed).toBe(false);
352347
// @ts-ignore: access private property
353-
expect(nodeH2Handler.sessionPool.get(authority)).toBeDefined();
348+
expect(nodeH2Handler.sessionCache.get(authority)).toBeDefined();
354349
setTimeout(() => {
355350
expect(session.closed).toBe(true);
356351
expect(session.destroyed).toBe(false);
357352
// @ts-ignore: access private property
358-
expect(nodeH2Handler.sessionPool.get(authority)).not.toBeDefined();
353+
expect(nodeH2Handler.sessionCache.get(authority)).not.toBeDefined();
359354
done();
360355
}, sessionTimeout + 100);
361356
});
@@ -365,7 +360,7 @@ describe(NodeHttp2Handler.name, () => {
365360
await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {});
366361

367362
// @ts-ignore: access private property
368-
const session: ClientHttp2Session = nodeH2Handler.sessions[0];
363+
const session: ClientHttp2Session = nodeH2Handler.sessionList[0];
369364
expect(session.closed).toBe(false);
370365
setTimeout(() => {
371366
expect(session.closed).toBe(true);
@@ -435,18 +430,18 @@ describe(NodeHttp2Handler.name, () => {
435430
});
436431

437432
describe("destroy", () => {
438-
it("destroys session and clears connections", async () => {
433+
it("destroys session and empties sessionList", async () => {
439434
await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {});
440435

441436
// @ts-ignore: access private property
442-
const session: ClientHttp2Session = nodeH2Handler.sessions[0];
437+
const session: ClientHttp2Session = nodeH2Handler.sessionList[0];
443438

444439
// @ts-ignore: access private property
445-
expect(nodeH2Handler.sessions.length).toBe(1);
440+
expect(nodeH2Handler.sessionList.length).toBe(1);
446441
expect(session.destroyed).toBe(false);
447442
nodeH2Handler.destroy();
448443
// @ts-ignore: access private property
449-
expect(nodeH2Handler.sessions.length).toBe(0);
444+
expect(nodeH2Handler.sessionList.length).toBe(0);
450445
expect(session.destroyed).toBe(true);
451446
});
452447
});

packages/node-http-handler/src/node-http2-handler.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ export class NodeHttp2Handler implements HttpHandler {
3838
private readonly disableSessionCache?: boolean;
3939

4040
public readonly metadata = { handlerProtocol: "h2" };
41-
private sessions: ClientHttp2Session[];
42-
private sessionPool: Map<string, ClientHttp2Session>;
41+
private sessionList: ClientHttp2Session[];
42+
private sessionCache: Map<string, ClientHttp2Session>;
4343

4444
constructor({ requestTimeout, sessionTimeout, disableSessionCache }: NodeHttp2HandlerOptions = {}) {
4545
this.requestTimeout = requestTimeout;
4646
this.sessionTimeout = sessionTimeout;
4747
this.disableSessionCache = disableSessionCache;
48-
this.sessions = [];
49-
this.sessionPool = new Map<string, ClientHttp2Session>();
48+
this.sessionList = [];
49+
this.sessionCache = new Map<string, ClientHttp2Session>();
5050
}
5151

5252
destroy(): void {
53-
this.sessions.forEach((session) => this.destroySession(session));
54-
this.sessionPool.clear();
53+
this.sessionList.forEach((session) => this.destroySession(session));
54+
this.sessionCache.clear();
5555
}
5656

5757
handle(request: HttpRequest, { abortSignal }: HttpHandlerOptions = {}): Promise<{ response: HttpResponse }> {
@@ -71,7 +71,7 @@ export class NodeHttp2Handler implements HttpHandler {
7171

7272
const { hostname, method, port, protocol, path, query } = request;
7373
const authority = `${protocol}//${hostname}${port ? `:${port}` : ""}`;
74-
const session = this.disableSessionCache ? this.getSession(authority) : this.getSessionFromPool(authority);
74+
const session = this.disableSessionCache ? this.getSession(authority) : this.getSessionFromCache(authority);
7575

7676
const reject = (err: Error) => {
7777
if (this.disableSessionCache) {
@@ -157,7 +157,7 @@ export class NodeHttp2Handler implements HttpHandler {
157157
});
158158
}
159159

160-
this.sessions.push(newSession);
160+
this.sessionList.push(newSession);
161161
return newSession;
162162
}
163163

@@ -166,15 +166,15 @@ export class NodeHttp2Handler implements HttpHandler {
166166
* @param authority The URL to create a session for.
167167
* @returns A session for the given URL.
168168
*/
169-
private getSessionFromPool(authority: string): ClientHttp2Session {
170-
const connectionPool = this.sessionPool;
169+
private getSessionFromCache(authority: string): ClientHttp2Session {
170+
const connectionPool = this.sessionCache;
171171
const existingSession = connectionPool.get(authority);
172172
if (existingSession) return existingSession;
173173

174174
const newSession = this.getSession(authority);
175175
connectionPool.set(authority, newSession);
176176
const destroySessionCb = () => {
177-
this.deleteSessionFromPool(authority, newSession);
177+
this.deleteSessionFromCache(authority, newSession);
178178
};
179179
newSession.on("goaway", destroySessionCb);
180180
newSession.on("error", destroySessionCb);
@@ -199,19 +199,19 @@ export class NodeHttp2Handler implements HttpHandler {
199199
if (!session.destroyed) {
200200
session.destroy();
201201
}
202-
this.sessions = this.sessions.filter((s) => s !== session);
202+
this.sessionList = this.sessionList.filter((s) => s !== session);
203203
}
204204

205205
/**
206206
* Delete a session from the connection pool.
207207
* @param authority The authority of the session to delete.
208208
* @param session The session to delete.
209209
*/
210-
private deleteSessionFromPool(authority: string, session: ClientHttp2Session): void {
211-
if (this.sessionPool.get(authority) !== session) {
210+
private deleteSessionFromCache(authority: string, session: ClientHttp2Session): void {
211+
if (this.sessionCache.get(authority) !== session) {
212212
// If the session is not in the pool, it has already been deleted.
213213
return;
214214
}
215-
this.sessionPool.delete(authority);
215+
this.sessionCache.delete(authority);
216216
}
217217
}

0 commit comments

Comments
 (0)