-
-
Notifications
You must be signed in to change notification settings - Fork 883
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
Make functions work with both connection and pool #3420
Conversation
|
||
let person_id = data.person_id; | ||
let person_view = PersonView::read(context.pool(), person_id).await?; | ||
let person_view = PersonView::read(&mut conn, person_id).await?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code is not very nice with &mut
everywhere. Maybe you could add a method context.conn()
which can replace context.pool()
directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
context.pool()
is now replaced with context.conn().await?
.
The functions now take impl DbConn
, which makes it possible to use &mut *conn
instead. This would be needed for transactions.
You need to run |
I would caution against this change because it makes the pool less effective. You're allocating a postgresql connection (which is a fairly expensive whole process) when you don't actually need it, making other concurrent queries wait if the pool is empty. The default pool size is only 5 (which should be increased), but even with larger sizes you can easily run out if you keep holding database connections while doing unrelated stuff (e.g. HTTP calls). If there's no reason for two queries to need the same Connection, then you shouldn't force them to use the same connection. Of course, if there has to be a transaction somewhere they will need to use the same connection, but most code, especially read-only queries, shouldn't care about it. |
I will fix this by reverting the commit "Get rid of repetitive &mut *context.conn().await?" |
The CI steps are stuck on "This step hasn't started yet." |
This def does make the DB calls more low-level and flexible. This was a ton of work, thanks for doing this. @phiresky does this look okay now? |
The functions now work with both connections and pools. I had a long fight with the borrow checker. I am very excited to finally make it work. I added this to
Functions need to take Also, this change now causes code to fail borrow checking if a |
CI ran out of disk space |
::core::result::Result::Err(__v) => return ::core::result::Result::Err(__v), | ||
} | ||
}),+)) | ||
}.await, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will lead to misleading code, where it appears that queries will run in parallel, but actually they dont. So I would prefer to remove this case and put unimplemented!()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be more misleading for a function to take DbPool
but not work with the Conn
variant
The code looks good, only thing I dont like is how it requires Anyway I confirmed that tests are all passing, so CI is fine. We should merge this asap in order to avoid conflicts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, this is next in line for merge.
Is it still possible after this PR to get an actually owned DbPool that doesn't borrow something? I'm trying to fix #3605 after merging this but can't figure out how to pass DbPools to tokio::spawned tasks. |
Use https://github.com/LemmyNet/lemmy/blob/main/crates/db_schema/src/utils.rs |
You can also convert it using |
Hey, sorry for necroing this old PR. I'm still new to the code base and I'm very confused about this apporach of an enum over pool vs connection. Why not make the functions generic over the requisite traits? Wouldn't that work just as well without having to use this (in my opinion, quite strange) enum? |
This gets rid of repetitive calls to
get_conn
indb_schema::impls
.More importantly, it allows the functions to be used in transactions because the actions in a transaction can't use different connections.
#1161 (comment)