Skip to content

Commit f50667b

Browse files
committed
Added request_timeout field to session
Session, SessionConfig and SessionBuilder were equipped with such field.
1 parent a9c1b8f commit f50667b

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

scylla/src/transport/session.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ pub struct Session {
6363
metrics: Arc<Metrics>,
6464
default_consistency: Consistency,
6565
auto_await_schema_agreement_timeout: Option<Duration>,
66+
#[allow(dead_code)]
67+
request_timeout: Option<Duration>,
6668
}
6769

6870
/// This implementation deliberately omits some details from Cluster in order
@@ -120,7 +122,7 @@ pub struct SessionConfig {
120122
pub auth_password: Option<String>,
121123

122124
pub schema_agreement_interval: Duration,
123-
pub connect_timeout: std::time::Duration,
125+
pub connect_timeout: Duration,
124126

125127
/// Size of the per-node connection pool, i.e. how many connections the driver should keep to each node.
126128
/// The default is `PerShard(1)`, which is the recommended setting for Scylla clusters.
@@ -141,6 +143,10 @@ pub struct SessionConfig {
141143
/// Controls the timeout for the automatic wait for schema agreement after sending a schema-altering statement.
142144
/// If `None`, the automatic schema agreement is disabled.
143145
pub auto_await_schema_agreement_timeout: Option<Duration>,
146+
147+
/// Controls the client-side timeout for queries.
148+
/// If `None`, the queries have no timeout (the driver will block indefinitely).
149+
pub request_timeout: Option<Duration>,
144150
}
145151

146152
/// Describes database server known on Session startup.
@@ -176,13 +182,14 @@ impl SessionConfig {
176182
ssl_context: None,
177183
auth_username: None,
178184
auth_password: None,
179-
connect_timeout: std::time::Duration::from_secs(5),
185+
connect_timeout: Duration::from_secs(5),
180186
connection_pool_size: Default::default(),
181187
disallow_shard_aware_port: false,
182188
default_consistency: Consistency::LocalQuorum,
183189
fetch_schema_metadata: true,
184190
keepalive_interval: None,
185191
auto_await_schema_agreement_timeout: Some(std::time::Duration::from_secs(60)),
192+
request_timeout: Some(Duration::from_secs(30)),
186193
}
187194
}
188195

@@ -374,6 +381,7 @@ impl Session {
374381
metrics: Arc::new(Metrics::new()),
375382
default_consistency: config.default_consistency,
376383
auto_await_schema_agreement_timeout: config.auto_await_schema_agreement_timeout,
384+
request_timeout: config.request_timeout,
377385
};
378386

379387
if let Some(keyspace_name) = config.used_keyspace {

scylla/src/transport/session_builder.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,27 @@ impl SessionBuilder {
541541
self.config.auto_await_schema_agreement_timeout = None;
542542
self
543543
}
544+
545+
/// Changes client-side timeout
546+
/// The default is 30 seconds.
547+
///
548+
/// # Example
549+
/// ```
550+
/// # use scylla::{Session, SessionBuilder};
551+
/// # use std::time::Duration;
552+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
553+
/// let session: Session = SessionBuilder::new()
554+
/// .known_node("127.0.0.1:9042")
555+
/// .request_timeout(Some(Duration::from_millis(500)))
556+
/// .build() // Turns SessionBuilder into Session
557+
/// .await?;
558+
/// # Ok(())
559+
/// # }
560+
/// ```
561+
pub fn request_timeout(mut self, duration: Option<std::time::Duration>) -> Self {
562+
self.config.request_timeout = duration;
563+
self
564+
}
544565
}
545566

546567
/// Creates a [`SessionBuilder`] with default configuration, same as [`SessionBuilder::new`]
@@ -707,6 +728,21 @@ mod tests {
707728
assert!(builder.config.fetch_schema_metadata);
708729
}
709730

731+
#[test]
732+
fn request_timeout() {
733+
let mut builder = SessionBuilder::new();
734+
assert_eq!(
735+
builder.config.request_timeout,
736+
Some(std::time::Duration::from_secs(30))
737+
);
738+
739+
builder = builder.request_timeout(Some(std::time::Duration::from_secs(10)));
740+
assert_eq!(
741+
builder.config.request_timeout,
742+
Some(std::time::Duration::from_secs(10))
743+
);
744+
}
745+
710746
#[test]
711747
fn all_features() {
712748
let mut builder = SessionBuilder::new();

0 commit comments

Comments
 (0)