GEOLYTIX / xyz

An open source javascript framework for spatial data and application interfaces.
MIT License
88 stars 26 forks source link

pg dbs connection management improvements & docs 🐘 #1711

Closed RobAndrewHurst closed 1 week ago

RobAndrewHurst commented 2 weeks ago

pg dbs connection management improvements & docs 🐘

Overview

This PR implements robust database connection handling with retry logic, better error management, and connection pooling optimizations for our PostgreSQL connections.

Key Changes

  1. Implemented retry logic with exponential backoff
  2. Enhanced connection pool configuration
  3. Added comprehensive error handling
  4. Improved resource management
  5. Added detailed logging
  6. Added JSDoc documentation

Detailed Implementation

1. Retry Logic

2. Connection Pool Configuration

const pool = new Pool({
  connectionString: process.env[key],
  keepAlive: true,
  connectionTimeoutMillis: 5000,    // 5 second timeout for connection attempts
  idleTimeoutMillis: 30000,         // 30 second timeout for idle connections
  max: 20                           // Maximum pool size
});

3. Error Handling

4. Resource Management

5. Logging Enhancements

Testing

Performance Impact

Documentation

JSDoc documentation has been added.

Testing Checklist

Security Considerations

dbauszus-glx commented 2 weeks ago

I have update the module documentation to include requires for the pg and logger. The documentation for pg.Pool is linked in the docs.

RobAndrewHurst commented 2 weeks ago

Putting back into draft to address some issues

dbauszus-glx commented 2 weeks ago

I removed the retry codes as these are ambiguous dependent on the dbs provider and version. It is also not clear which type of error should be retried.

I created a sleep function as this easier to understand than a const sleep that is a function.

I created the clientQuery function to receive the pool param which has the dbs as an option.

image

RobAndrewHurst commented 1 week ago

It should be possible to set the RETRY_LIMIT through the process configuration. Ideally by adding a property to the connection string.

The dbs_string now takes an additional optional parameter at the end of the connection string itself separated by a '|'. This will override the RETRY_LIMIT on the module.

@dbauszus-glx Where should we add in this documentation? Into the wiki or on the module itself explaining how connection string work?

dbauszus-glx commented 1 week ago

Having thought about this. It is probably better to use process.env.RETRY_LIMIT for documentation purposes.

This and the DBS need to be added to the global env type documentation.

RobAndrewHurst commented 1 week ago

Having thought about this. It is probably better to use process.env.RETRY_LIMIT for documentation purposes.

This and the DBS need to be added to the global env type documentation.

This has now been updated. The RETRY_LIMIT const will be assigned from the process.env.RETRY_LIMIT if it is present otherwise it will default to 3.

I have also included the DBS_ param and RETRY_LIMIT in the global .env docs

sonarcloud[bot] commented 1 week ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud