Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated connection cleanup process to handle expired connections and those exceeding config.maxIdle #3022

Conversation

robert-pitt-foodhub
Copy link
Contributor

@robert-pitt-foodhub robert-pitt-foodhub commented Sep 7, 2024

Description

This PR addresses issue #3020 by improving the connection cleanup process in the MySQL pool. The changes ensure that both connections exceeding the config.maxIdle setting and expired idle connections (those exceeding the config.idleTimeout) are correctly cleaned up.

Summary of changes:

  • lib/pool.js

    • Updated the connection cleanup logic to check for both config.maxIdle and expired connections based on config.idleTimeout.
  • New tests:

    • Added a new test test-pool-release-idle-connection-timeout.test.cjs to verify the removal of idle connections after the timeout period.
    • Adjusted test-pool-release-idle-connection.test.cjs to reflect the new cleanup behaviour.

Key Changes:

  • Improved connection cleanup mechanism in lib/pool.js.
  • Added a new integration test to validate timeout-based cleanup (test-pool-release-idle-connection-timeout.test.cjs).
  • Modified existing tests to cover new logic and ensure all scenarios are handled.

Checklist

  • Code compiles correctly
  • All tests passing
  • Relevant documentation updated (if applicable)
  • Verified with different pool configurations (e.g., low/high idleTimeout, maxIdle)

How to Test

  1. Create a pool with the following settings:

    • connectionLimit: 3
    • maxIdle: 1
    • idleTimeout: 5000 (5 seconds)
  2. Open and release 3 connections.

  3. Ensure connections are removed after 5 seconds if idle.

  4. Run the provided integration tests (npm test or equivalent).

Linked Issues

Screenshots (if applicable)

See linked issue

…hat have expired as well as the connections that exceed the config.maxIdle setting
@robert-pitt-foodhub robert-pitt-foodhub force-pushed the bugfix/resolve-hanging-connnections-in-pool branch from 776563e to e9f2366 Compare September 7, 2024 13:39
@wellwelwel
Copy link
Collaborator

Thanks for the PR and the detailed reports and explanations, @robert-pitt-foodhub 🤝

Copy link

codecov bot commented Sep 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.13%. Comparing base (dee0c08) to head (820569d).
Report is 6 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3022   +/-   ##
=======================================
  Coverage   88.13%   88.13%           
=======================================
  Files          71       71           
  Lines       12889    12890    +1     
  Branches     1352     1354    +2     
=======================================
+ Hits        11360    11361    +1     
  Misses       1529     1529           
Flag Coverage Δ
compression-0 88.13% <100.00%> (+<0.01%) ⬆️
compression-1 88.13% <100.00%> (+<0.01%) ⬆️
tls-0 87.55% <100.00%> (+<0.01%) ⬆️
tls-1 87.89% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@robert-pitt-foodhub
Copy link
Contributor Author

Thanks @wellwelwel, Happy to help get this bug resolved, I have made another commit to the PR to add a new test case that specifically validates the connections are removed after. they are expired, let me know if there is any other test cases you would like me to address or documentation that needs updating.

Thanks

@wellwelwel wellwelwel linked an issue Sep 7, 2024 that may be closed by this pull request
@wellwelwel
Copy link
Collaborator

wellwelwel commented Sep 9, 2024

also replying #3020 (comment)

@robert-pitt-foodhub, although the changes to the code are relatively simple, this seems a complex behavior to reproduce in a test case.

I tried removing your changes, keeping your test, but it didn't fail even with the current logic.

@robert-pitt-foodhub
Copy link
Contributor Author

robert-pitt-foodhub commented Sep 10, 2024

Hey @wellwelwel, here's a failing test case that fail on the main branch but pass with the new code, Let me know if this helps

'use strict';

const createPool = require('../common.test.cjs').createPool;
const { assert } = require('poku');

const pool = new createPool({
  connectionLimit: 3,
  maxIdle: 2,
  idleTimeout: 1000,
});

/**
 * Create UP TO 3 connections, and allow a maximum of 2 idle connections, after
 * the 1000ms idle timeout has passed, within 1000ms of the timeout, all
 * connections should be closed if they are not in use.
 */

pool.getConnection((err1, connection1) => {
  assert.ifError(err1);
  assert.ok(connection1);
  // connection1.release();

  pool.getConnection((err2, connection2) => {
    assert.ifError(err2);
    assert.ok(connection2);
    // connection2.release();

    pool.getConnection((err3, connection3) => {
      assert.ifError(err3);
      assert.ok(connection3);
      // connection3.release();

      connection1.release();
      connection2.release();
      connection3.release();

      setTimeout(() => {
        assert(
          pool._allConnections.length === 0,
          `Expected all connections to be closed, but found ${pool._allConnections.length}`,
        );
        assert(
          pool._freeConnections.length === 0,
          `Expected all free connections to be closed, but found ${pool._freeConnections.length}`,
        );
      }, 5000);
    });
  });
});

Main Branch
Screenshot 2024-09-10 at 01 41 34

This Branch + LRU cache change
Screenshot 2024-09-10 at 01 42 45

@wellwelwel
Copy link
Collaborator

wellwelwel commented Sep 10, 2024

here's a failing test case that fail on the main branch but pass with the new code

Thanks @robert-pitt-foodhub, I believe this last test is the ideal 🙋🏻‍♂️

@robert-pitt-foodhub
Copy link
Contributor Author

Would you like to to add this test to the PR test cases @wellwelwel

@wellwelwel
Copy link
Collaborator

Would you like to to add this test to the PR test cases @wellwelwel

For fixes, it's usually helpful to have a test that expects a failure to ensure that a new change doesn't break again in the future 🙋🏻‍♂️

Copy link
Collaborator

@wellwelwel wellwelwel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pool isn't being closed 🙋🏻‍♂️

@wellwelwel
Copy link
Collaborator

LGMT

Thanks again, @robert-pitt-foodhub 🤝
Can I merge or do you want to make some changes?

@robert-pitt-foodhub
Copy link
Contributor Author

Im happy for to you merge this yes, there is one other outstanding change which I think needs to be addressed which I noted in the Issue, but that we can deal with separately if you like.

Thanks for your support as well, go ahead and merge when your ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Idle Connections are not being cleaned up under certain conditions.
3 participants