Skip to content

Commit

Permalink
fix(node-http-handler): handle close event in H2 from server side (#3619
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AllanZhengYP authored May 18, 2022
1 parent cf420b3 commit c528661
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
6 changes: 0 additions & 6 deletions packages/node-http-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@
"typedoc": "0.19.2",
"typescript": "~4.6.2"
},
"jest": {
"coveragePathIgnorePatterns": [
"/node_modules/",
"<rootDir>/*.mock.ts"
]
},
"engines": {
"node": ">= 12.0.0"
},
Expand Down
19 changes: 10 additions & 9 deletions packages/node-http-handler/src/node-http2-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,22 @@ describe(NodeHttp2Handler.name, () => {
expectSessionCreatedAndUnreffed(createdSessions[3]);
});

it("handles connections destroyed by servers", async () => {
const port3 = port + 2;
const mockH2Server3 = createMockHttp2Server().listen(port3);
it.each([
["destroy", port + 2],
["close", port + 3],
])("handles servers calling connections %s", async (func, port) => {
const mockH2Server4 = createMockHttp2Server().listen(port);
let establishedConnections = 0;
let numRequests = 0;

mockH2Server3.on("stream", (request: Http2Stream) => {
// transmit goaway frame and then shut down the connection.
mockH2Server4.on("stream", (request: Http2Stream) => {
numRequests += 1;
request.session.destroy();
request.session[func]();
});
mockH2Server3.on("connection", () => {
mockH2Server4.on("connection", () => {
establishedConnections += 1;
});
const req = new HttpRequest({ ...getMockReqOptions(), port: port3 });
const req = new HttpRequest({ ...getMockReqOptions(), port });
expect(establishedConnections).toBe(0);
expect(numRequests).toBe(0);
await rejects(
Expand All @@ -277,7 +278,7 @@ describe(NodeHttp2Handler.name, () => {
);
expect(establishedConnections).toBe(3);
expect(numRequests).toBe(3);
mockH2Server3.close();
mockH2Server4.close();

// Not keeping node alive
expect(createdSessions).toHaveLength(3);
Expand Down
2 changes: 2 additions & 0 deletions packages/node-http-handler/src/node-http2-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ export class NodeHttp2Handler implements HttpHandler {
newSession.on("error", destroySessionCb);
newSession.on("frameError", destroySessionCb);

newSession.on("close", () => this.deleteSessionFromCache(authority, newSession));

if (this.config?.sessionTimeout) {
newSession.setTimeout(this.config.sessionTimeout, destroySessionCb);
}
Expand Down

0 comments on commit c528661

Please sign in to comment.