Skip to content

Commit

Permalink
Merge pull request #193 from muzarski/heartbeat
Browse files Browse the repository at this point in the history
cluster: keepalive config options
  • Loading branch information
Lorak-mmk authored Oct 24, 2024
2 parents b23b531 + 99f6e44 commit 94b6d16
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
63 changes: 53 additions & 10 deletions scylla-rust-wrapper/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@ use std::time::Duration;

use crate::cass_compression_types::CassCompressionType;

// According to `cassandra.h` the default CPP driver's
// According to `cassandra.h` the defaults for
// - consistency for statements is LOCAL_ONE,
// - request client timeout is 12000 millis.
const DEFAULT_CONSISTENCY: Consistency = Consistency::LocalOne;
const DEFAULT_REQUEST_TIMEOUT_MILLIS: u64 = 12000;
// - request client timeout is 12000 millis,
const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_millis(12000);
// - fetching schema metadata is true
const DEFAULT_DO_FETCH_SCHEMA_METADATA: bool = true;
// - setting TCP_NODELAY is true
const DEFAULT_SET_TCP_NO_DELAY: bool = true;
// - connect timeout is 5000 millis
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_millis(5000);
// - keepalive interval is 30 secs
const DEFAULT_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(30);
// - keepalive timeout is 60 secs
const DEFAULT_KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(60);

const DRIVER_NAME: &str = "ScyllaDB Cpp-Rust Driver";
const DRIVER_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -157,15 +167,26 @@ pub fn build_session_builder(
pub unsafe extern "C" fn cass_cluster_new() -> *mut CassCluster {
let default_execution_profile_builder = ExecutionProfileBuilder::default()
.consistency(DEFAULT_CONSISTENCY)
.request_timeout(Some(Duration::from_millis(DEFAULT_REQUEST_TIMEOUT_MILLIS)));

// Set DRIVER_NAME and DRIVER_VERSION of cpp-rust driver.
let custom_identity = SelfIdentity::new()
.with_custom_driver_name(DRIVER_NAME)
.with_custom_driver_version(DRIVER_VERSION);
.request_timeout(Some(DEFAULT_REQUEST_TIMEOUT));

// Default config options - according to cassandra.h
let default_session_builder = {
// Set DRIVER_NAME and DRIVER_VERSION of cpp-rust driver.
let custom_identity = SelfIdentity::new()
.with_custom_driver_name(DRIVER_NAME)
.with_custom_driver_version(DRIVER_VERSION);

SessionBuilder::new()
.custom_identity(custom_identity)
.fetch_schema_metadata(DEFAULT_DO_FETCH_SCHEMA_METADATA)
.tcp_nodelay(DEFAULT_SET_TCP_NO_DELAY)
.connection_timeout(DEFAULT_CONNECT_TIMEOUT)
.keepalive_interval(DEFAULT_KEEPALIVE_INTERVAL)
.keepalive_timeout(DEFAULT_KEEPALIVE_TIMEOUT)
};

Box::into_raw(Box::new(CassCluster {
session_builder: SessionBuilder::new().custom_identity(custom_identity),
session_builder: default_session_builder,
port: 9042,
contact_points: Vec::new(),
// Per DataStax documentation: Without additional configuration the C/C++ driver
Expand Down Expand Up @@ -343,6 +364,28 @@ pub unsafe extern "C" fn cass_cluster_set_tcp_keepalive(
cluster.session_builder.config.tcp_keepalive_interval = tcp_keepalive_interval;
}

#[no_mangle]
pub unsafe extern "C" fn cass_cluster_set_connection_heartbeat_interval(
cluster_raw: *mut CassCluster,
interval_secs: c_uint,
) {
let cluster = ptr_to_ref_mut(cluster_raw);
let keepalive_interval = (interval_secs > 0).then(|| Duration::from_secs(interval_secs as u64));

cluster.session_builder.config.keepalive_interval = keepalive_interval;
}

#[no_mangle]
pub unsafe extern "C" fn cass_cluster_set_connection_idle_timeout(
cluster_raw: *mut CassCluster,
timeout_secs: c_uint,
) {
let cluster = ptr_to_ref_mut(cluster_raw);
let keepalive_timeout = (timeout_secs > 0).then(|| Duration::from_secs(timeout_secs as u64));

cluster.session_builder.config.keepalive_timeout = keepalive_timeout;
}

#[no_mangle]
pub unsafe extern "C" fn cass_cluster_set_connect_timeout(
cluster_raw: *mut CassCluster,
Expand Down
8 changes: 0 additions & 8 deletions src/testing_unimplemented.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ CASS_EXPORT CassError cass_cluster_set_cloud_secure_connection_bundle_no_ssl_lib
throw std::runtime_error(
"UNIMPLEMENTED cass_cluster_set_cloud_secure_connection_bundle_no_ssl_lib_init\n");
}
CASS_EXPORT void cass_cluster_set_connection_heartbeat_interval(CassCluster* cluster,
unsigned interval_secs) {
throw std::runtime_error("UNIMPLEMENTED cass_cluster_set_connection_heartbeat_interval\n");
}
CASS_EXPORT void cass_cluster_set_connection_idle_timeout(CassCluster* cluster,
unsigned timeout_secs) {
throw std::runtime_error("UNIMPLEMENTED cass_cluster_set_connection_idle_timeout\n");
}
CASS_EXPORT void cass_cluster_set_constant_reconnect(CassCluster* cluster, cass_uint64_t delay_ms) {
throw std::runtime_error("UNIMPLEMENTED cass_cluster_set_constant_reconnect\n");
}
Expand Down

0 comments on commit 94b6d16

Please sign in to comment.