Skip to content

Commit

Permalink
Refactor to repeat less
Browse files Browse the repository at this point in the history
  • Loading branch information
glasser committed Jul 23, 2021
1 parent 2f45dd3 commit 26638b0
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions packages/apollo-server/src/stoppable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import http from 'http';
import type http from 'http';
import https from 'https';
import type { Socket } from 'net';

Expand All @@ -35,18 +35,16 @@ export class Stopper {
private stopped = false;

constructor(private server: http.Server | https.Server) {
const onConnection = (socket: Socket) => {
this.reqsPerSocket.set(socket, 0);
socket.once('close', () => this.reqsPerSocket.delete(socket));
};

if (server instanceof https.Server) {
server.on('secureConnection', onConnection);
} else {
server.on('connection', onConnection);
}
// Keep a number in reqsPerSocket for each current connection.
server.on(
server instanceof https.Server ? 'secureConnection' : 'connection',
(socket: Socket) => {
this.reqsPerSocket.set(socket, 0);
socket.once('close', () => this.reqsPerSocket.delete(socket));
},
);

// Track how many requests are active on the socket.
// Track how many HTTP requests are active on the socket.
server.on(
'request',
(req: http.IncomingMessage, res: http.ServerResponse) => {
Expand Down

0 comments on commit 26638b0

Please sign in to comment.