Skip to content

Conversation

sarahboyce
Copy link
Contributor

@sarahboyce sarahboyce commented Dec 11, 2023

@apollo13 has done all the hard work in this PR: #16881 hoping to push it over the line.

Also hope the tests are testing the right thing, tried to take inspiration from psycopg_pool tests: https://github.com/psycopg/psycopg/blob/52ed68a9d699c1e43e07a4d04441534197552de5/tests/pool/test_pool.py#L103

But let me know what I'm missing - very new space for me 👍

Related ticket: https://code.djangoproject.com/ticket/33497

@sarahboyce sarahboyce force-pushed the 33497_postgres_connection_pool branch 2 times, most recently from 2867c32 to e5884b8 Compare December 11, 2023 10:52
@sarahboyce sarahboyce force-pushed the 33497_postgres_connection_pool branch from e5884b8 to 96afd8c Compare December 11, 2023 11:12
@apollo13
Copy link
Member

Lovely, thank you for picking up my work.

@sarahboyce sarahboyce force-pushed the 33497_postgres_connection_pool branch 2 times, most recently from 9933116 to bf048c2 Compare December 11, 2023 11:58
@sarahboyce sarahboyce force-pushed the 33497_postgres_connection_pool branch from bf048c2 to 4af72c8 Compare December 11, 2023 12:52
Copy link
Contributor

@bluetech bluetech left a comment

Choose a reason for hiding this comment

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

Thanks for working on this @sarahboyce. I left some minor comments, but it looks good to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

One subtlety with this trick that I checked is that pool.open is thread safe; luckily it is 👍

@sarahboyce sarahboyce force-pushed the 33497_postgres_connection_pool branch from 4af72c8 to 372ff92 Compare December 12, 2023 19:19
@sarahboyce sarahboyce force-pushed the 33497_postgres_connection_pool branch 2 times, most recently from 0a24043 to 003b3e2 Compare December 12, 2023 20:35
@sarahboyce sarahboyce force-pushed the 33497_postgres_connection_pool branch from 003b3e2 to 1c9a7fa Compare December 12, 2023 22:05
Copy link
Contributor

@bluetech bluetech left a comment

Choose a reason for hiding this comment

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

Thanks for the update, I left a few more replies and comments.

@sarahboyce sarahboyce force-pushed the 33497_postgres_connection_pool branch 7 times, most recently from f975d56 to 14c65fd Compare December 13, 2023 21:13
@apollo13
Copy link
Member

apollo13 commented Dec 13, 2023 via email

@sarahboyce sarahboyce force-pushed the 33497_postgres_connection_pool branch 4 times, most recently from 8a4741f to 6e7dbdc Compare December 14, 2023 12:59
@felixxm
Copy link
Member

felixxm commented Jan 12, 2024

I had the same warning for test_skip_class_unless_db_feature but I was able to fix it via sarahboyce@962566c -- this wouldn't hurt in current main either, @felixxm do you want an extra cleanup PR for that one or can we just keep it as extra commit in this PR?

Merged in 6e520d9.

@ridersofrohan
Copy link

Thanks for all the work on this! Is there any update on when it will be merged in and included in a Django Release?

@apollo13
Copy link
Member

apollo13 commented Jan 31, 2024 via email

@gerardrbentley
Copy link

I'm finding this does the trick to limit PG connections when running under ASGI (in that not every request grabs a new connection). Awesome work; much less complicated than pg_bouncer for most needs

Here's my test repo using the patch branch if anyone wants to extend: https://github.com/gerardrbentley/demo-django

Copy link
Member

Choose a reason for hiding this comment

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

Any reason why this can't be moved to the Postgres backend destroy_test_db method?

Copy link
Member

Choose a reason for hiding this comment

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

Nothing specific no, should be doable 👍

Copy link
Member

Choose a reason for hiding this comment

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

Got rid of it completely, couldn't find the reason why it was there :)

Copy link
Member

Choose a reason for hiding this comment

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

Oh I found it. When tests do fail it leaves the pool open. I wonder if we should add a hook to Django to prepare the base backend for something like close_pool

Copy link
Member

@charettes charettes Feb 17, 2024

Choose a reason for hiding this comment

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

Doesn't this hook already exist in the form of destroy_test_db? Why can't it be moved there?

In other words, why can't destroy_test_db simply do

if hasattr(self.connection, "close_pool"):
    connection.close_pool()

For the time being

Copy link
Member

Choose a reason for hiding this comment

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

because there are more locations where I might wanna do some closing, like here:

self.connection.close()

But yes, for now I will probably add it inside destroy_test_db.

Copy link
Member

Choose a reason for hiding this comment

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

I made it postgres specific in _destroy_test_db for now.

@apollo13 apollo13 force-pushed the 33497_postgres_connection_pool branch from fc7078b to 5b9263c Compare February 17, 2024 16:38
@yorickr
Copy link

yorickr commented Feb 19, 2024

Hi!
First of all, thanks for all the work that's being done on this.

I'm looking to use this thread pool in the future, in combination with gevent. After some local testing, I found out the current solution opens a new threadpool on every new request, which for my use case is the same as using persistent connections.

Is this something that can/will be considered in the requirements? Is this threadpool even something that can be used with gevent in the first place?

Thank you 🙏

@apollo13
Copy link
Member

As far as I know Django doesn't support gevent officially. So no, this is not something I would consider, I have no idea how gevent works. That said if the existing thread locals do work, I am kinda surprised since I am not doing anything magic (I think).

@apollo13 apollo13 force-pushed the 33497_postgres_connection_pool branch 2 times, most recently from d03886a to 74f2407 Compare February 19, 2024 18:35
@bluetech
Copy link
Contributor

@yorickr

I'm looking to use this thread pool in the future, in combination with gevent. After some local testing, I found out the current solution opens a new threadpool on every new request, which for my use case is the same as using persistent connections.

I think gevent should work without any problem (and correctly) with this implementation, can you provide a way to reproduce what you're seeing?

@felixxm felixxm self-assigned this Feb 22, 2024
@felixxm felixxm force-pushed the 33497_postgres_connection_pool branch from 74f2407 to 340e848 Compare February 22, 2024 13:04
@felixxm
Copy link
Member

felixxm commented Feb 22, 2024

I started working on this patch .... 🚧

@apollo13
Copy link
Member

apollo13 commented Feb 22, 2024 via email

@felixxm felixxm force-pushed the 33497_postgres_connection_pool branch 2 times, most recently from ab62fda to 856058e Compare February 23, 2024 12:25
@felixxm felixxm force-pushed the 33497_postgres_connection_pool branch 3 times, most recently from 0d8ea9d to a691ea1 Compare February 28, 2024 08:57
@felixxm felixxm closed this Feb 28, 2024
@felixxm felixxm force-pushed the 33497_postgres_connection_pool branch from a691ea1 to 107aa76 Compare February 28, 2024 09:02
@felixxm
Copy link
Member

felixxm commented Feb 28, 2024

Superseded by #17914.

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.

10 participants