Skip to content

Clean connection state up after protocol named prepared statement #163

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,19 @@ where

self.buffer.put(&original[..]);

let mut final_buffer = self.buffer.clone();
let first_message_code = final_buffer.get_u8() as char;
let _ = final_buffer.get_i32() as usize;

// Almost certainly true
if first_message_code == 'P' {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not run this check in 'P' => { block? Should be cheaper than cloning the entire buffer (which can be very large) every S (effectively every query).

Copy link
Contributor

@levkk levkk Sep 8, 2022

Choose a reason for hiding this comment

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

Also, aren't all Postgres strings NULL-terminated, so this check would never fire?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point on the clone, the reason I run this in the S and not in the P is that we have two places where we handle P buffering so I am trying to avoid duplication. Let me see if we can get away with no clones.

re: NULL-terminated, we only care about the first byte after P and the 32bit size. If it is \0 the this is an unnamed prepared statement, anything else would be interpreted as a named prepared statement.

if final_buffer.get_u8() != 0 {
// This is a named prepared statement
// Server connection state will need to be cleared at checkin
server.mark_dirty();
}
}

self.send_and_receive_loop(
code,
self.buffer.clone(),
Expand Down
5 changes: 5 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ impl Server {
pub fn last_activity(&self) -> SystemTime {
self.last_activity
}

// Marks a connection as needing DISCARD ALL at checkin
pub fn mark_dirty(&mut self) {
self.needs_cleanup = true;
}
}

impl Drop for Server {
Expand Down