Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit720897d

Browse files
committed
feat: add support for quick active health checks on connections
1 parent8488a46 commit720897d

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

‎tokio-postgres/src/client.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@ impl Client {
531531
simple_query::batch_execute(self.inner(), query).await
532532
}
533533

534+
/// Check the connection is alive and wait for the confirmation.
535+
pubasyncfncheck_connection(&self) ->Result<(),Error>{
536+
// sync is a very quick message to test the connection health.
537+
query::sync(self.inner()).await
538+
}
539+
534540
/// Begins a new database transaction.
535541
///
536542
/// The transaction will roll back by default - use the `commit` method to commit it.

‎tokio-postgres/src/query.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,13 @@ impl RowStream {
323323
self.rows_affected
324324
}
325325
}
326+
327+
pubasyncfnsync(client:&InnerClient) ->Result<(),Error>{
328+
let buf =Bytes::from_static(b"S\0\0\0\x04");
329+
letmut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?;
330+
331+
match responses.next().await?{
332+
Message::ReadyForQuery(_) =>Ok(()),
333+
_ =>Err(Error::unexpected_message()),
334+
}
335+
}

‎tokio-postgres/tests/test/main.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ async fn scram_password_ok() {
147147
connect("user=scram_user password=password dbname=postgres").await;
148148
}
149149

150+
#[tokio::test]
151+
asyncfnsync(){
152+
let client =connect("user=postgres").await;
153+
client.check_connection().await.unwrap();
154+
}
155+
150156
#[tokio::test]
151157
asyncfnpipelined_prepare(){
152158
let client =connect("user=postgres").await;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp