@@ -14,14 +14,13 @@ export class ServiceProvider {
1414 private constructor ( ) {
1515 this . _clientInstance = new Client ( ) ;
1616 }
17-
1817 static async initialize ( ) : Promise < ServiceProvider | null > {
1918 if ( ! ServiceProvider . instance ) {
2019 const instance = new ServiceProvider ( ) ;
2120 const isInitialized = await instance . _clientInstance . initialize ( ) ;
2221 if ( ! isInitialized ) {
2322 logger . error ( 'Failed to initialize client instance. Returning null.' ) ;
24- return null ; // Return null if initialization fails
23+ return null ;
2524 }
2625 instance . _connectionPool = instance . _clientInstance . getConnectionPool ( ) ;
2726 ServiceProvider . instance = instance ;
@@ -42,61 +41,41 @@ export class ServiceProvider {
4241 await this . _connectionPool . close ( ) ;
4342 }
4443 async getPoolClint ( ) : Promise < PoolClient > {
45- return await this . _connectionPool . getClient ( ) ;
44+ const client = await this . _connectionPool . getClient ( ) ;
45+ client . on ( 'error' , ( err : any ) => {
46+ logger . error ( 'Unexpected client error:' , err ) ;
47+ client . release ( ) ;
48+ } ) ;
49+ return client ;
4650 }
4751 async getGroupService ( ) {
48- return await this . retryConnect ( async ( ) => {
49- const client = await this . getPoolClint ( ) ;
50- return new GroupService ( client ) ;
51- } ) ;
52+ const client = await this . getPoolClint ( ) ;
53+ return new GroupService ( client ) ;
5254 }
5355 async getUserService ( ) {
54- return await this . retryConnect ( async ( ) => {
55- const client = await this . getPoolClint ( ) ;
56- return new UserService ( client ) ;
57- } ) ;
56+ const client = await this . getPoolClint ( ) ;
57+ return new UserService ( client ) ;
5858 }
5959 async getRulesService ( ) {
60- return await this . retryConnect ( async ( ) => {
61- const client = await this . getPoolClint ( ) ;
62- return new GroupRuleService ( client ) ;
63- } ) ;
60+ const client = await this . getPoolClint ( ) ;
61+ return new GroupRuleService ( client ) ;
6462 }
6563 async getWarnsService ( ) {
66- return await this . retryConnect ( async ( ) => {
67- const clint = await this . getPoolClint ( ) ;
68- return new WarningDatabaseService ( clint ) ;
69- } ) ;
64+ const clint = await this . getPoolClint ( ) ;
65+ return new WarningDatabaseService ( clint ) ;
7066 }
7167 async healthCheck ( ) : Promise < boolean > {
68+ const client = await this . getPoolClint ( ) ;
7269 try {
73- const client = await this . getPoolClint ( ) ;
7470 await client . query ( 'SELECT NOW()' ) ;
7571 client . release ( ) ;
7672 logger . info ( 'Database is healthy.' ) ;
7773 return true ;
7874 } catch ( err : any ) {
7975 logger . error ( 'Database health check failed:' , err . message ) ;
8076 return false ;
77+ } finally {
78+ client . release ( ) ;
8179 }
8280 }
83- private async retryConnect < T > ( fn : ( ) => Promise < T > , retries = 3 , delay = 5000 ) : Promise < T | null > {
84- let lastError : any ;
85-
86- for ( let attempt = 0 ; attempt < retries ; attempt ++ ) {
87- try {
88- return await fn ( ) ;
89- } catch ( error : any ) {
90- lastError = error ;
91- logger . warn ( `Retry Attempt ${ attempt + 1 } failed with error: ${ error . message || error } .` , 'Database' ) ;
92- if ( attempt < retries - 1 ) {
93- const backoffTime = delay * Math . pow ( 2 , attempt ) ;
94- logger . warn ( `Retrying in ${ backoffTime } ms...` , 'Database' ) ;
95- await new Promise ( ( res ) => setTimeout ( res , backoffTime ) ) ;
96- }
97- }
98- }
99- logger . error ( `All ${ retries } retry attempts failed. Last error: ${ lastError ?. message || lastError } ` , 'Database' ) ;
100- return null ;
101- }
10281}
0 commit comments