@xec-sh/core
Version:
Universal shell execution engine
59 lines • 1.98 kB
JavaScript
export class ConnectionPoolMetricsCollector {
constructor() {
this.connectionsCreated = 0;
this.connectionsDestroyed = 0;
this.connectionsFailed = 0;
this.reuseCount = 0;
this.lastCleanup = null;
}
onConnectionCreated() {
this.connectionsCreated++;
}
onConnectionDestroyed() {
this.connectionsDestroyed++;
}
onConnectionFailed() {
this.connectionsFailed++;
}
onConnectionReused() {
this.reuseCount++;
}
onCleanup() {
this.lastCleanup = new Date();
}
getMetrics(poolSize, connections) {
const activeConnections = Array.from(connections.values()).filter(c => c.isAlive).length;
const idleConnections = poolSize - activeConnections;
const now = Date.now();
let totalIdleTime = 0;
let totalUseCount = 0;
for (const conn of connections.values()) {
if (conn.isAlive) {
totalIdleTime += now - conn.lastUsed.getTime();
totalUseCount += conn.useCount;
}
}
const averageIdleTime = activeConnections > 0 ? totalIdleTime / activeConnections : 0;
const averageUseCount = activeConnections > 0 ? totalUseCount / activeConnections : 0;
return {
activeConnections,
idleConnections,
totalConnections: poolSize,
connectionsCreated: this.connectionsCreated,
connectionsDestroyed: this.connectionsDestroyed,
connectionsFailed: this.connectionsFailed,
reuseCount: this.reuseCount,
averageIdleTime,
averageUseCount,
lastCleanup: this.lastCleanup
};
}
reset() {
this.connectionsCreated = 0;
this.connectionsDestroyed = 0;
this.connectionsFailed = 0;
this.reuseCount = 0;
this.lastCleanup = null;
}
}
//# sourceMappingURL=connection-pool-metrics.js.map