Skip to content

Commit a4cd97a

Browse files
committed
feat(database): Add connection pool closing and reinitialization logic
1 parent 41dfd62 commit a4cd97a

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

src/database/ConnectionPool.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import logger from '../utils/logger';
44
export class ConnectionPool {
55
private _pool: Pool;
66
private _isProduction: 'development' | 'production';
7+
private _isClosed: boolean = false;
78
constructor() {
89
const connectionString = this.getConnectionString();
910
this._isProduction = Config.environment;
@@ -59,6 +60,8 @@ export class ConnectionPool {
5960
}
6061

6162
async close(): Promise<void> {
63+
if (this._isClosed) return; // Prevent closing the pool more than once
64+
this._isClosed = true;
6265
await this._pool.end();
6366
}
6467
private initializePool(connectionString: string): Pool {
@@ -72,6 +75,10 @@ export class ConnectionPool {
7275
});
7376
}
7477
async reinitializePool() {
78+
if (this._isClosed) {
79+
throw new Error('Cannot reinitialize a closed pool.');
80+
}
81+
7582
if (this._pool && !this._pool.ended) {
7683
await this._pool.end();
7784
}

src/service/database/ServiceProvider.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ import { UserService } from '../../database/models/User';
66
import { GroupRuleService } from '../../database/models/GroupRule';
77
import { WarningDatabaseService } from '../../database/models/Warning';
88
import logger from '../../utils/logger';
9-
import { CopBot } from '../../bot';
109

1110
export class ServiceProvider {
1211
private static instance: ServiceProvider;
1312
private _clientInstance: Client;
1413
private _connectionPool!: ConnectionPool;
15-
16-
private lastRequestTime: number | null = null; // Track the last request time
17-
private readonly requestInterval: number = 5000;
1814
private constructor() {
1915
this._clientInstance = new Client();
2016
}
@@ -31,18 +27,6 @@ export class ServiceProvider {
3127
}
3228
return ServiceProvider.instance;
3329
}
34-
private async enforceRateLimit(): Promise<void> {
35-
const now = Date.now();
36-
if (this.lastRequestTime) {
37-
const elapsed = now - this.lastRequestTime;
38-
if (elapsed < this.requestInterval) {
39-
const waitTime = this.requestInterval - elapsed;
40-
logger.error(`⚠️ Rate limit exceeded. Please wait for ${waitTime} ms before making another request.`);
41-
await new Promise((resolve) => setTimeout(resolve, waitTime));
42-
}
43-
}
44-
this.lastRequestTime = Date.now();
45-
}
4630
static getInstance() {
4731
return ServiceProvider.instance;
4832
}
@@ -57,7 +41,6 @@ export class ServiceProvider {
5741
await this._connectionPool.close();
5842
}
5943
async getPoolClint(): Promise<PoolClient> {
60-
await this.enforceRateLimit();
6144
try {
6245
const client = await this._connectionPool.getClient();
6346
client.on('error', (err: any) => {

0 commit comments

Comments
 (0)