-
Notifications
You must be signed in to change notification settings - Fork 227
Async PG support #412
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
Draft
levkk
wants to merge
3
commits into
main
Choose a base branch
from
levkk-async
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Async PG support #412
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -932,7 +932,7 @@ where | |
} | ||
|
||
// Grab a server from the pool. | ||
let connection = match pool | ||
let mut connection = match pool | ||
.get(query_router.shard(), query_router.role(), &self.stats) | ||
.await | ||
{ | ||
|
@@ -975,9 +975,8 @@ where | |
} | ||
}; | ||
|
||
let mut reference = connection.0; | ||
let server = &mut *connection.0; | ||
let address = connection.1; | ||
let server = &mut *reference; | ||
|
||
// Server is assigned to the client in case the client wants to | ||
// cancel a query later. | ||
|
@@ -1000,6 +999,7 @@ where | |
|
||
// Set application_name. | ||
server.set_name(&self.application_name).await?; | ||
server.switch_async(false); | ||
|
||
let mut initial_message = Some(message); | ||
|
||
|
@@ -1019,12 +1019,37 @@ where | |
None => { | ||
trace!("Waiting for message inside transaction or in session mode"); | ||
|
||
match tokio::time::timeout( | ||
idle_client_timeout_duration, | ||
read_message(&mut self.read), | ||
) | ||
.await | ||
{ | ||
let message = tokio::select! { | ||
message = tokio::time::timeout( | ||
idle_client_timeout_duration, | ||
read_message(&mut self.read), | ||
) => message, | ||
|
||
server_message = server.recv() => { | ||
debug!("Got async message"); | ||
|
||
let server_message = match server_message { | ||
Ok(message) => message, | ||
Err(err) => { | ||
pool.ban(&address, BanReason::MessageReceiveFailed, Some(&self.stats)); | ||
server.mark_bad(); | ||
return Err(err); | ||
} | ||
}; | ||
|
||
match write_all_half(&mut self.write, &server_message).await { | ||
Ok(_) => (), | ||
Err(err) => { | ||
server.mark_bad(); | ||
return Err(err); | ||
} | ||
}; | ||
|
||
continue; | ||
} | ||
}; | ||
|
||
match message { | ||
Ok(Ok(message)) => message, | ||
Ok(Err(err)) => { | ||
// Client disconnected inside a transaction. | ||
|
@@ -1141,9 +1166,14 @@ where | |
|
||
// Sync | ||
// Frontend (client) is asking for the query result now. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to update these docs to include Flush. Something like:
|
||
'S' => { | ||
'S' | 'H' => { | ||
debug!("Sending query to server"); | ||
|
||
if code == 'H' { | ||
server.switch_async(true); | ||
debug!("Client requested flush, going async"); | ||
} | ||
|
||
self.buffer.put(&message[..]); | ||
|
||
let first_message_code = (*self.buffer.get(0).unwrap_or(&0)) as char; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import psycopg2 | ||
import asyncio | ||
import asyncpg | ||
|
||
PGCAT_HOST = "127.0.0.1" | ||
PGCAT_PORT = "6432" | ||
|
||
|
||
def regular_main(): | ||
# Connect to the PostgreSQL database | ||
conn = psycopg2.connect( | ||
host=PGCAT_HOST, | ||
database="sharded_db", | ||
user="sharding_user", | ||
password="sharding_user", | ||
port=PGCAT_PORT, | ||
) | ||
|
||
# Open a cursor to perform database operations | ||
cur = conn.cursor() | ||
|
||
# Execute a SQL query | ||
cur.execute("SELECT 1") | ||
|
||
# Fetch the results | ||
rows = cur.fetchall() | ||
|
||
# Print the results | ||
for row in rows: | ||
print(row[0]) | ||
|
||
# Close the cursor and the database connection | ||
cur.close() | ||
conn.close() | ||
|
||
|
||
async def main(): | ||
# Connect to the PostgreSQL database | ||
conn = await asyncpg.connect( | ||
host=PGCAT_HOST, | ||
database="sharded_db", | ||
user="sharding_user", | ||
password="sharding_user", | ||
port=PGCAT_PORT, | ||
) | ||
|
||
# Execute a SQL query | ||
for _ in range(25): | ||
rows = await conn.fetch("SELECT 1") | ||
|
||
# Print the results | ||
for row in rows: | ||
print(row[0]) | ||
|
||
# Close the database connection | ||
await conn.close() | ||
|
||
|
||
regular_main() | ||
asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
asyncio==3.4.3 | ||
asyncpg==0.27.0 | ||
black==23.3.0 | ||
click==8.1.3 | ||
mypy-extensions==1.0.0 | ||
packaging==23.1 | ||
pathspec==0.11.1 | ||
platformdirs==3.2.0 | ||
psutil==5.9.1 | ||
psycopg2==2.9.3 | ||
psutil==5.9.1 | ||
tomli==2.0.1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I tested this idea trying to see if this helps with #303
Something else it's missing that doesn't allow to send the server messages to the client. Maybe this 2 issues are related and could be solved both with the same fix