Skip to content

Commit

Permalink
fix: set session as gracefully closing on goaway frame
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kaufman committed Oct 27, 2022
1 parent 51eeaf5 commit 207e275
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions source/agent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// See https://github.com/facebook/jest/issues/2549
// eslint-disable-next-line node/prefer-global/url
const {URL} = require('url');
const { URL } = require('url');
const EventEmitter = require('events');
const tls = require('tls');
const http2 = require('http2');
Expand Down Expand Up @@ -142,7 +142,7 @@ const gracefullyClose = session => {
};

class Agent extends EventEmitter {
constructor({timeout = 0, maxSessions = Number.POSITIVE_INFINITY, maxEmptySessions = 10, maxCachedTlsSessions = 100} = {}) {
constructor({ timeout = 0, maxSessions = Number.POSITIVE_INFINITY, maxEmptySessions = 10, maxCachedTlsSessions = 100 } = {}) {
super();

// SESSIONS[NORMALIZED_OPTIONS] = [];
Expand Down Expand Up @@ -179,7 +179,7 @@ class Agent extends EventEmitter {
};

// Reusing TLS sessions increases performance.
this.tlsSessionCache = new QuickLRU({maxSize: maxCachedTlsSessions});
this.tlsSessionCache = new QuickLRU({ maxSize: maxCachedTlsSessions });
}

get protocol() {
Expand Down Expand Up @@ -256,7 +256,7 @@ class Agent extends EventEmitter {
// They will be executed at a different time.
resolve();
} else {
listeners = [{resolve, reject}];
listeners = [{ resolve, reject }];
}

try {
Expand All @@ -269,8 +269,8 @@ class Agent extends EventEmitter {

if (options) {
// Validate servername
const {servername} = options;
const {hostname} = origin;
const { servername } = options;
const { hostname } = origin;
if (servername && hostname !== servername) {
throw new Error(`Origin ${hostname} differs from servername ${servername}`);
}
Expand Down Expand Up @@ -383,7 +383,7 @@ class Agent extends EventEmitter {
let socket;

try {
const computedOptions = {...options};
const computedOptions = { ...options };

if (computedOptions.settings === undefined) {
computedOptions.settings = this.settings;
Expand All @@ -405,7 +405,7 @@ class Agent extends EventEmitter {

// Node.js return https://false:443 instead of https://1.1.1.1:443
const getOriginSet = () => {
const {socket} = session;
const { socket } = session;

let originSet;
if (socket.servername === false) {
Expand Down Expand Up @@ -440,6 +440,12 @@ class Agent extends EventEmitter {
session.destroy();
});

session.once('goaway', () => {
// Prevent session from being used for new requests.
// The session will eventually emit either an 'error' or 'close' event.
session[kGracefullyClosing] = true;
});

session.once('close', () => {
this._sessionCount--;

Expand Down Expand Up @@ -485,7 +491,7 @@ class Agent extends EventEmitter {
const origin = originSet[index];

if (origin in queue) {
const {listeners, completed} = queue[origin];
const { listeners, completed } = queue[origin];

let index = 0;

Expand Down Expand Up @@ -724,7 +730,7 @@ class Agent extends EventEmitter {
closeEmptySessions(maxCount = Number.POSITIVE_INFINITY) {
let closedCount = 0;

const {sessions} = this;
const { sessions } = this;

// eslint-disable-next-line guard-for-in
for (const key in sessions) {
Expand All @@ -748,7 +754,7 @@ class Agent extends EventEmitter {
}

destroy(reason) {
const {sessions, queue} = this;
const { sessions, queue } = this;

// eslint-disable-next-line guard-for-in
for (const key in sessions) {
Expand Down

0 comments on commit 207e275

Please sign in to comment.