Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed traffic stats for http connection reuse #572

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { URL } from 'url';

import type { Socket } from './socket';
import { badGatewayStatusCodes, createCustomStatusHttpResponse, errorCodeToStatusCode } from './statuses';
import type { SocketPreviousStats } from './utils/count_target_bytes';
import { countTargetBytes } from './utils/count_target_bytes';
import { getBasicAuthorizationHeader } from './utils/get_basic';

Expand Down Expand Up @@ -85,9 +86,15 @@ export const chain = (
const fn = proxy.protocol === 'https:' ? https.request : http.request;
const client = fn(proxy.origin, options as unknown as http.ClientRequestArgs);

client.on('connect', (response, targetSocket, clientHead) => {
client.once('socket', (targetSocket: Socket & SocketPreviousStats) => {
// socket can be re-used by multiple requests (HTTP keep alive)
// (even in multiple Server objects)
targetSocket.previousBytesRead = targetSocket.bytesRead;
targetSocket.previousBytesWritten = targetSocket.bytesWritten;
countTargetBytes(sourceSocket, targetSocket);
});

client.on('connect', (response, targetSocket, clientHead) => {
if (sourceSocket.readyState !== 'open') {
// Sanity check, should never reach.
targetSocket.destroy();
Expand Down
10 changes: 8 additions & 2 deletions src/forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import stream from 'stream';
import type { URL } from 'url';
import util from 'util';

import type { Socket } from './socket';
import { badGatewayStatusCodes, errorCodeToStatusCode } from './statuses';
import type { SocketPreviousStats } from './utils/count_target_bytes';
import { countTargetBytes } from './utils/count_target_bytes';
import { getBasicAuthorizationHeader } from './utils/get_basic';
import { validHeadersOnly } from './utils/valid_headers_only';
Expand Down Expand Up @@ -114,8 +116,12 @@ export const forward = async (
}
});

client.once('socket', (socket) => {
countTargetBytes(request.socket, socket);
client.once('socket', (socket: Socket & SocketPreviousStats) => {
// socket can be re-used by multiple requests (HTTP keep alive)
// (even in multiple Server objects)
socket.previousBytesRead = socket.bytesRead;
socket.previousBytesWritten = socket.bytesWritten;
countTargetBytes(request.socket, socket, (handler) => response.once('close', handler));
});

// Can't use pipeline here as it automatically destroys the streams
Expand Down
27 changes: 18 additions & 9 deletions src/utils/count_target_bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ const calculateTargetStats = Symbol('calculateTargetStats');

type Stats = { bytesWritten: number | null, bytesRead: number | null };

export type SocketPreviousStats = { previousBytesWritten?: number, previousBytesRead?: number };

interface Extras {
[targetBytesWritten]: number;
[targetBytesRead]: number;
[targets]: Set<net.Socket>;
[targets]: Set<net.Socket & SocketPreviousStats>;
[calculateTargetStats]: () => Stats;
}

// @ts-expect-error TS is not aware that `source` is used in the assertion.
function typeSocket(source: unknown): asserts source is net.Socket & Extras {}

export const countTargetBytes = (source: net.Socket, target: net.Socket): void => {
export const countTargetBytes = (
source: net.Socket,
target: net.Socket & SocketPreviousStats,
finishRegister?: (handler: () => void) => void,
): void => {
typeSocket(source);

source[targetBytesWritten] = source[targetBytesWritten] || 0;
Expand All @@ -26,21 +32,24 @@ export const countTargetBytes = (source: net.Socket, target: net.Socket): void =

source[targets].add(target);

target.once('close', () => {
source[targetBytesWritten] += target.bytesWritten;
source[targetBytesRead] += target.bytesRead;

const finishHandler = () => {
source[targetBytesWritten] += (target.bytesWritten - (target.previousBytesWritten || 0));
source[targetBytesRead] += (target.bytesRead - (target.previousBytesRead || 0));
source[targets].delete(target);
});
};
if (!finishRegister) {
finishRegister = (handler: () => void) => target.once('close', handler);
}
finishRegister(finishHandler);

if (!source[calculateTargetStats]) {
source[calculateTargetStats] = () => {
let bytesWritten = source[targetBytesWritten];
let bytesRead = source[targetBytesRead];

for (const socket of source[targets]) {
bytesWritten += socket.bytesWritten;
bytesRead += socket.bytesRead;
bytesWritten += (socket.bytesWritten - (socket.previousBytesWritten || 0));
bytesRead += (socket.bytesRead - (socket.previousBytesRead || 0));
}

return {
Expand Down
Loading