Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions pgdog/src/frontend/client/query_engine/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::pin::pin;

use tracing::{info, trace};

use crate::{
Expand Down Expand Up @@ -58,12 +60,21 @@ impl QueryEngine {
}
}

match safe_timeout(
context.timeouts.query_timeout(&State::Active),
Box::pin(self.client_server_exchange(context)),
)
.await
{
// Stack-pinned, not `Box::pin`: this runs once per query, and boxing
// costs a malloc, a free and a memcpy of the whole exchange future
// every time. `Pin<&mut F>` is a `Future`, so `safe_timeout` takes it.
//
// The inner block matters: unlike the boxed future, which was moved
// into `safe_timeout` and dropped by the `.await`, `pin!` binds a local
// that would otherwise hold its borrow of `self` and `context` until
// the end of the function, past the arms below.
Comment on lines +63 to +70

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd prefer to see comments like this as part of the commit message rather than on the code itself. These often get out of sync with the code they're talking about and result in the code base being littered with inaccurate statements about why it's written a certain way. I'd only mention the borrow of self and context if the code fails to compile without the block

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure i will take care of it

let query_timeout = context.timeouts.query_timeout(&State::Active);
let result = {
let exchange = pin!(self.client_server_exchange(context));
safe_timeout(query_timeout, exchange).await
};

match result {
Ok(response) => response?,
Err(err) => {
// Close the conn, it could be stuck executing a query
Expand Down
Loading