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

AddReadOnly inTargetSessionAttrs#1110

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
3 changes: 3 additions & 0 deletionstokio-postgres/src/config.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,6 +34,8 @@ pub enum TargetSessionAttrs {
Any,
/// The session must allow writes.
ReadWrite,
/// The session allow only reads.
ReadOnly,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This presumably needs to be used somewhere in the connection code.

Copy link
ContributorAuthor

@chandr-andrchandr-andrMar 3, 2024
edited
Loading

Choose a reason for hiding this comment

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

@sfackler Sry, haven't committed my latest changes. Please have a look.

}

/// TLS configuration.
Expand DownExpand Up@@ -622,6 +624,7 @@ impl Config {
let target_session_attrs = match value {
"any" => TargetSessionAttrs::Any,
"read-write" => TargetSessionAttrs::ReadWrite,
"read-only" => TargetSessionAttrs::ReadOnly,
_ => {
return Err(Error::config_parse(Box::new(InvalidValue(
"target_session_attrs",
Expand Down
14 changes: 12 additions & 2 deletionstokio-postgres/src/connect.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -160,7 +160,7 @@ where
let has_hostname = hostname.is_some();
let (mut client, mut connection) = connect_raw(socket, tls, has_hostname, config).await?;

ifletTargetSessionAttrs::ReadWrite = config.target_session_attrs {
ifconfig.target_session_attrs !=TargetSessionAttrs::Any {
let rows = client.simple_query_raw("SHOW transaction_read_only");
pin_mut!(rows);

Expand All@@ -185,11 +185,21 @@ where

match next.await.transpose()? {
Some(SimpleQueryMessage::Row(row)) => {
if row.try_get(0)? == Some("on") {
let read_only_result = row.try_get(0)?;
if read_only_result == Some("on")
&& config.target_session_attrs == TargetSessionAttrs::ReadWrite
{
return Err(Error::connect(io::Error::new(
io::ErrorKind::PermissionDenied,
"database does not allow writes",
)));
} else if read_only_result == Some("off")
&& config.target_session_attrs == TargetSessionAttrs::ReadOnly
{
return Err(Error::connect(io::Error::new(
io::ErrorKind::PermissionDenied,
"database is not read only",
)));
} else {
break;
}
Expand Down
8 changes: 8 additions & 0 deletionstokio-postgres/tests/test/parse.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,6 +34,14 @@ fn settings() {
.keepalives_idle(Duration::from_secs(30))
.target_session_attrs(TargetSessionAttrs::ReadWrite),
);
check(
"connect_timeout=3 keepalives=0 keepalives_idle=30 target_session_attrs=read-only",
Config::new()
.connect_timeout(Duration::from_secs(3))
.keepalives(false)
.keepalives_idle(Duration::from_secs(30))
.target_session_attrs(TargetSessionAttrs::ReadOnly),
);
}

#[test]
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp