fix(refactor): remove deprecated Port80Handler and related utilities

- Deleted event-utils.ts which contained deprecated Port80Handler and its subscribers.
- Updated index.ts to remove the export of event-utils.
- Refactored ConnectionManager to extend LifecycleComponent for better resource management.
- Added BinaryHeap implementation for efficient priority queue operations.
- Introduced EnhancedConnectionPool for managing pooled connections with lifecycle management.
- Implemented LifecycleComponent to manage timers and event listeners automatically.
- Added comprehensive tests for BinaryHeap and LifecycleComponent to ensure functionality.
This commit is contained in:
2025-05-31 18:01:09 +00:00
parent 7b81186bb3
commit 829ae0d6a3
13 changed files with 1354 additions and 559 deletions

View File

@ -3,11 +3,12 @@ import type { IConnectionRecord, ISmartProxyOptions } from './models/interfaces.
import { SecurityManager } from './security-manager.js';
import { TimeoutManager } from './timeout-manager.js';
import { logger } from '../../core/utils/logger.js';
import { LifecycleComponent } from '../../core/utils/lifecycle-component.js';
/**
* Manages connection lifecycle, tracking, and cleanup with performance optimizations
*/
export class ConnectionManager {
export class ConnectionManager extends LifecycleComponent {
private connectionRecords: Map<string, IConnectionRecord> = new Map();
private terminationStats: {
incoming: Record<string, number>;
@ -16,7 +17,6 @@ export class ConnectionManager {
// Performance optimization: Track connections needing inactivity check
private nextInactivityCheck: Map<string, number> = new Map();
private inactivityCheckTimer: NodeJS.Timeout | null = null;
// Connection limits
private readonly maxConnections: number;
@ -31,6 +31,8 @@ export class ConnectionManager {
private securityManager: SecurityManager,
private timeoutManager: TimeoutManager
) {
super();
// Set reasonable defaults for connection limits
this.maxConnections = settings.defaults.security.maxConnections
@ -134,12 +136,10 @@ export class ConnectionManager {
*/
private startInactivityCheckTimer(): void {
// Check every 30 seconds for connections that need inactivity check
this.inactivityCheckTimer = setInterval(() => {
this.setInterval(() => {
this.performOptimizedInactivityCheck();
}, 30000);
// Allow process to exit even with timer
this.inactivityCheckTimer.unref();
// Note: LifecycleComponent's setInterval already calls unref()
}
/**
@ -196,11 +196,9 @@ export class ConnectionManager {
this.processCleanupQueue();
} else if (!this.cleanupTimer) {
// Otherwise, schedule batch processing
this.cleanupTimer = setTimeout(() => {
this.cleanupTimer = this.setTimeout(() => {
this.processCleanupQueue();
}, 100);
this.cleanupTimer.unref();
}
}
@ -209,7 +207,7 @@ export class ConnectionManager {
*/
private processCleanupQueue(): void {
if (this.cleanupTimer) {
clearTimeout(this.cleanupTimer);
this.clearTimeout(this.cleanupTimer);
this.cleanupTimer = null;
}
@ -225,11 +223,9 @@ export class ConnectionManager {
// If there are more in queue, schedule next batch
if (this.cleanupQueue.size > 0) {
this.cleanupTimer = setTimeout(() => {
this.cleanupTimer = this.setTimeout(() => {
this.processCleanupQueue();
}, 10);
this.cleanupTimer.unref();
}
}
@ -531,17 +527,15 @@ export class ConnectionManager {
/**
* Clear all connections (for shutdown)
*/
public clearConnections(): void {
// Stop timers
if (this.inactivityCheckTimer) {
clearInterval(this.inactivityCheckTimer);
this.inactivityCheckTimer = null;
}
if (this.cleanupTimer) {
clearTimeout(this.cleanupTimer);
this.cleanupTimer = null;
}
public async clearConnections(): Promise<void> {
// Delegate to LifecycleComponent's cleanup
await this.cleanup();
}
/**
* Override LifecycleComponent's onCleanup method
*/
protected async onCleanup(): Promise<void> {
// Process connections in batches to avoid blocking
const connections = Array.from(this.connectionRecords.values());