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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions src/forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import type { URL } from 'url';
import util from 'util';

import { Socket } from './socket';

Check failure on line 8 in src/forward.ts

View workflow job for this annotation

GitHub Actions / Lint

All imports in the declaration are only used as types. Use `import type`
import { badGatewayStatusCodes, errorCodeToStatusCode } from './statuses';
import { countTargetBytes } from './utils/count_target_bytes';
import { countTargetBytes, SocketPreviousStats } from './utils/count_target_bytes';

Check failure on line 10 in src/forward.ts

View workflow job for this annotation

GitHub Actions / Lint

Imports "SocketPreviousStats" are only used as type
import { getBasicAuthorizationHeader } from './utils/get_basic';
import { validHeadersOnly } from './utils/valid_headers_only';

Expand Down Expand Up @@ -114,8 +115,12 @@
}
});

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