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`);
}
}