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

Ensure DB connections are closed when we've finished with them #1619

Merged
merged 5 commits into from
Jul 6, 2021

Conversation

babbageclunk
Copy link
Member

@babbageclunk babbageclunk commented Jul 5, 2021

Closes #1576

What this PR does / why we need it:
Before this change, the different resources would leak database connections whenever they were reconciled. Add defer db.Close() to all of the places that open database connections so that they are closed at the end of the method.

Special notes for your reviewer:
I started off making a cache to keep the *sql.DBs in so that they could be reused between reconciliations, but then the difficulty was ensuring we didn't keep the DBs in the cache indefinitely. One option was to hook up the database controller so it could remove the cached entry when the database was deleted, but that's quite fiddly architecturally. I also wrote some code to clear DBs from the cache if they haven't been requested after a fixed amount of time, which would work but would be really difficult to test (to an extent that is probably not worth it).

How does this PR make you feel:
gif

@codecov-commenter
Copy link

codecov-commenter commented Jul 5, 2021

Codecov Report

Merging #1619 (364dbad) into master (a087f27) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1619   +/-   ##
=======================================
  Coverage   62.94%   62.94%           
=======================================
  Files         187      187           
  Lines       12359    12359           
=======================================
  Hits         7779     7779           
  Misses       3896     3896           
  Partials      684      684           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a087f27...364dbad. Read the comment docs.

@babbageclunk babbageclunk changed the title Ensure DB connections get closed when we've finished with them Ensure DB connections are closed when we've finished with them Jul 5, 2021
babbageclunk and others added 4 commits July 6, 2021 11:17
This is much simpler than a cache of DBs. I started going down that
path but as always it's removing no-longer needed DBs from the cache
that makes it more complicated. For now this will fix the leak and if
we have a problem with opening and closing connections being too slow
we can fix that then.
This is much simpler than trying to use a DB cache for it.
In the azuresqlaction, azuresqlmanageduser and azuresqluser
reconcilers.
The ASO CI subscription can't create servers in eastus2 but westus2 is
allowed at the moment.
@babbageclunk babbageclunk force-pushed the bug/db-connection-leak branch from 650d598 to ea608c2 Compare July 5, 2021 23:34
@babbageclunk babbageclunk mentioned this pull request Jul 5, 2021
Copy link
Member

@theunrepentantgeek theunrepentantgeek left a comment

Choose a reason for hiding this comment

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

Looks good. :shipit:

@babbageclunk babbageclunk merged commit d4b0803 into Azure:master Jul 6, 2021
@babbageclunk babbageclunk deleted the bug/db-connection-leak branch July 6, 2021 18:04
@johgoe
Copy link
Contributor

johgoe commented Jul 19, 2021

@matthchr @babbageclunk Is there already a timeline for the next release, which includes this fix?

@matthchr
Copy link
Member

@johgoe There's no hard deadline but I would imagine sometime in the next week. Are you waiting for this change? (I assume yes given you're asking here).

@johgoe
Copy link
Contributor

johgoe commented Jul 19, 2021

@matthchr Thank you for you fast reply. Currently we have to restart our PostgreSQL Server too often. We are happy to hear that the release should be available sometime next week.

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.

Bug: PostgreSqlUser opens more and more connections
6 participants