SessionBuilder provides a convenient way to configure and create new Session instances.
Overview
The builder pattern allows you to:- Configure connection parameters
- Set compression and TLS options
- Configure execution profiles and policies
- Set timeouts and keepalive intervals
- Customize session behavior
Creating a SessionBuilder
Building a Session
build
Finishes configuration and creates a
Session.Returns: Result<Session, NewSessionError>Example:Connection Configuration
known_node
Adds a known node with a hostname. If the port is not specified, 9042 is used as default.Parameters:
hostname: impl AsRef<str>- Hostname or IP address with optional port
SelfExample:known_node_addr
Adds a known node with a
SocketAddr.Parameters:node_addr: SocketAddr- Socket address of the node
SelfExample:known_nodes
Adds multiple known nodes with hostnames.Parameters:
hostnames: impl IntoIterator<Item = impl AsRef<str>>- Collection of hostnames
SelfExample:known_nodes_addr
Adds multiple known nodes with socket addresses.Parameters:
node_addrs: impl IntoIterator<Item = impl Borrow<SocketAddr>>- Collection of socket addresses
SelfExample:local_ip_address
Sets the local IP address all TCP sockets are bound to.By default, this is
None, which is equivalent to INADDR_ANY for IPv4 or in6addr_any for IPv6.Parameters:local_ip_address: Option<impl Into<IpAddr>>- Local IP address
SelfExample:connection_timeout
Changes connection timeout. The default is 5 seconds.Parameters:
duration: Duration- Connection timeout
SelfExample:hostname_resolution_timeout
Changes DNS hostname resolution timeout. The default is 5 seconds. Using
None disables the timeout.Parameters:duration: Option<Duration>- Resolution timeout
SelfExample:Authentication and Security
user
Sets username and password for plain text authentication.Parameters:
username: impl Into<String>- Usernamepasswd: impl Into<String>- Password
SelfExample:authenticator_provider
Sets a custom authenticator provider.Parameters:
authenticator_provider: Arc<dyn AuthenticatorProvider>- Custom authenticator
SelfExample:tls_context
Provides TLS context for encrypted connections. If set to
None, TLS is not used. Default is None.Parameters:tls_context: Option<impl Into<TlsContext>>- TLS configuration
SelfExample (with OpenSSL):Compression and Protocol
compression
Sets preferred compression algorithm. The default is no compression. If not supported by the server, falls back to no compression.Parameters:
compression: Option<Compression>- Compression algorithm
SelfExample:tcp_nodelay
Sets the TCP nodelay flag. The default is
true.Parameters:nodelay: bool- TCP nodelay setting
SelfExample:tcp_keepalive_interval
Sets the TCP keepalive interval. The default is
None (no TCP-layer keepalives).Parameters:interval: Duration- Keepalive interval
SelfExample:write_coalescing
Enables or disables write coalescing, which delays socket flushes to batch multiple writes. The default is
true.Parameters:enable: bool- Whether to enable write coalescing
SelfExample:write_coalescing_delay
Controls the write coalescing delay (if enabled). Default is
WriteCoalescingDelay::SmallNondeterministic.Parameters:delay: WriteCoalescingDelay- Delay configuration
SelfExample:Connection Pool
pool_size
Sets the per-node connection pool size. The default is one connection per shard (recommended for ScyllaDB).Parameters:
size: PoolSize- Pool size configuration
SelfExample:disallow_shard_aware_port
Prevents the driver from connecting to the shard-aware port (19042), even if the node supports it.Parameters:
disallow: bool- Whether to disallow shard-aware port
SelfExample:shard_aware_local_port_range
Specifies the local port range used for shard-aware connections. Default is
ShardAwarePortRange::EPHEMERAL_PORT_RANGE.Parameters:port_range: ShardAwarePortRange- Port range configuration
SelfExample:Keepalive Configuration
keepalive_interval
Sets the interval for CQL-layer keepalive requests. The default is 30 seconds.Parameters:
interval: Duration- Keepalive interval
SelfExample:keepalive_timeout
Sets the keepalive timeout. The default is 30 seconds.Parameters:
timeout: Duration- Keepalive timeout
SelfExample:Keyspace and Schema
use_keyspace
Sets the keyspace to use on all connections. Each connection will send
USE <keyspace_name> before sending requests.Parameters:keyspace_name: impl Into<String>- Keyspace namecase_sensitive: bool- Whether to quote the keyspace name
SelfExample:keyspaces_to_fetch
Sets which keyspaces to fetch metadata for. Empty list (default) means all keyspaces.Parameters:
keyspaces: impl IntoIterator<Item = impl Into<String>>- Keyspace names
SelfExample:fetch_schema_metadata
Sets whether to fetch schema metadata. The default is
true.Parameters:fetch: bool- Whether to fetch schema metadata
SelfExample:schema_agreement_interval
Sets how often the driver should ask if schema is in agreement. The default is 200 milliseconds.Parameters:
timeout: Duration- Check interval
SelfExample:schema_agreement_timeout
Sets the timeout for waiting for schema agreement. The default is 60 seconds.Parameters:
timeout: Duration- Timeout duration
SelfExample:auto_await_schema_agreement
Controls automatic waiting for schema agreement after schema-altering statements. The default is enabled.Parameters:
enabled: bool- Whether to automatically await schema agreement
SelfExample:refresh_metadata_on_auto_schema_agreement
Sets whether to refresh metadata after reaching schema agreement. The default is
true.Parameters:refresh_metadata: bool- Whether to refresh metadata
SelfExample:metadata_request_serverside_timeout
Sets server-side timeout for metadata queries. The default is 2 seconds.Parameters:
timeout: Duration- Server-side timeout
SelfExample:cluster_metadata_refresh_interval
Sets the interval for automatic cluster metadata refreshes. The default is 60 seconds.Parameters:
interval: Duration- Refresh interval
SelfExample:Policies and Profiles
default_execution_profile_handle
Sets the default execution profile for statements that don’t specify their own.Parameters:
profile_handle: ExecutionProfileHandle- Execution profile handle
SelfExample:address_translator
Uses a custom address translator for peer addresses retrieved from the cluster.Parameters:
translator: Arc<dyn AddressTranslator>- Address translator
SelfExample:host_filter
Sets the host filter, which decides whether to open connections to nodes.Parameters:
filter: Arc<dyn HostFilter>- Host filter
SelfExample:timestamp_generator
Sets a timestamp generator for client-side timestamp generation.Parameters:
timestamp_generator: Arc<dyn TimestampGenerator>- Timestamp generator
SelfExample:Tracing Configuration
tracing_info_fetch_attempts
Sets the number of attempts to fetch tracing info. The default is 10 attempts.Cassandra users may want to increase this value.Parameters:
attempts: NonZeroU32- Number of attempts
SelfExample:tracing_info_fetch_interval
Sets the delay between attempts to fetch tracing info. The default is 3 milliseconds.Cassandra users may want to increase this value.Parameters:
interval: Duration- Interval between attempts
SelfExample:tracing_info_fetch_consistency
Sets the consistency level for fetching tracing info. The default is
Consistency::One.Parameters:consistency: Consistency- Consistency level
SelfExample:Identity Configuration
custom_identity
Sets custom identity information sent to the server in the STARTUP message.Parameters:
identity: SelfIdentity<'static>- Custom identity
SelfExample:Related Types
Session- The session created by this builderExecutionProfile- Configuration for request executionSessionConfig- Low-level session configuration
