refactor(socket-utils): replace direct socket cleanup with centralized cleanupSocket utility across connection management

This commit is contained in:
2025-06-01 08:02:32 +00:00
parent eb2e67fecc
commit bed1a76537
5 changed files with 19 additions and 58 deletions

View File

@ -1,5 +1,6 @@
import * as plugins from '../../plugins.js';
import { type IHttpProxyOptions, type IConnectionEntry, type ILogger, createLogger } from './models/types.js';
import { cleanupSocket } from '../../core/utils/socket-utils.js';
/**
* Manages a pool of backend connections for efficient reuse
@ -133,14 +134,7 @@ export class ConnectionPool {
if ((connection.isIdle && now - connection.lastUsed > idleTimeout) ||
connections.length > (this.options.connectionPoolSize || 50)) {
try {
if (!connection.socket.destroyed) {
connection.socket.end();
connection.socket.destroy();
}
} catch (err) {
this.logger.error(`Error destroying pooled connection to ${host}`, err);
}
cleanupSocket(connection.socket, `pool-${host}-idle`);
connections.shift(); // Remove from pool
removed++;
@ -170,14 +164,7 @@ export class ConnectionPool {
this.logger.debug(`Closing ${connections.length} connections to ${host}`);
for (const connection of connections) {
try {
if (!connection.socket.destroyed) {
connection.socket.end();
connection.socket.destroy();
}
} catch (error) {
this.logger.error(`Error closing connection to ${host}:`, error);
}
cleanupSocket(connection.socket, `pool-${host}-close`);
}
}

View File

@ -18,6 +18,7 @@ import { RequestHandler, type IMetricsTracker } from './request-handler.js';
import { WebSocketHandler } from './websocket-handler.js';
import { ProxyRouter } from '../../routing/router/index.js';
import { RouteRouter } from '../../routing/router/route-router.js';
import { cleanupSocket } from '../../core/utils/socket-utils.js';
import { FunctionCache } from './function-cache.js';
/**
@ -520,11 +521,7 @@ export class HttpProxy implements IMetricsTracker {
// Close all tracked sockets
for (const socket of this.socketMap.getArray()) {
try {
socket.destroy();
} catch (error) {
this.logger.error('Error destroying socket', error);
}
cleanupSocket(socket, 'http-proxy-stop');
}
// Close all connection pool connections