pub type SessionBuilder = GenericSessionBuilder<DefaultMode>;

Implementations§

source§

impl SessionBuilder

source

pub fn new() -> Self

Creates new SessionBuilder with default configuration

Default configuration
  • Compression: None
source

pub fn known_node(self, hostname: impl AsRef<str>) -> Self

Add a known node with a hostname

Examples
let session: Session = SessionBuilder::new().known_node("127.0.0.1:9042").build().await?;
let session: Session = SessionBuilder::new().known_node("db1.example.com").build().await?;
source

pub fn known_node_addr(self, node_addr: SocketAddr) -> Self

Add a known node with an IP address

Example
let session: Session = SessionBuilder::new()
    .known_node_addr(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 9042))
    .build()
    .await?;
source

pub fn known_nodes( self, hostnames: impl IntoIterator<Item = impl AsRef<str>> ) -> Self

Add a list of known nodes with hostnames

Example
let session: Session = SessionBuilder::new()
    .known_nodes(["127.0.0.1:9042", "db1.example.com"])
    .build()
    .await?;
source

pub fn known_nodes_addr( self, node_addrs: impl IntoIterator<Item = impl Borrow<SocketAddr>> ) -> Self

Add a list of known nodes with IP addresses

Example
let addr1 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(172, 17, 0, 3)), 9042);
let addr2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(172, 17, 0, 4)), 9042);

let session: Session = SessionBuilder::new()
    .known_nodes_addr([addr1, addr2])
    .build()
    .await?;
source

pub fn user( self, username: impl Into<String>, passwd: impl Into<String> ) -> Self

Set username and password for plain text authentication.
If the database server will require authentication

Example
let session: Session = SessionBuilder::new()
    .known_node("127.0.0.1:9042")
    .use_keyspace("my_keyspace_name", false)
    .user("cassandra", "cassandra")
    .build()
    .await?;
source

pub fn authenticator_provider( self, authenticator_provider: Arc<dyn AuthenticatorProvider> ) -> Self

Set custom authenticator provider to create an authenticator instance during a session creation.

Example
use bytes::Bytes;
use scylla::{Session, SessionBuilder};
use async_trait::async_trait;
use scylla::authentication::{AuthenticatorProvider, AuthenticatorSession, AuthError};

struct CustomAuthenticator;

#[async_trait]
impl AuthenticatorSession for CustomAuthenticator {
    async fn evaluate_challenge(&mut self, token: Option<&[u8]>) -> Result<Option<Vec<u8>>, AuthError> {
        Ok(None)
    }

    async fn success(&mut self, token: Option<&[u8]>) -> Result<(), AuthError> {
        Ok(())
    }
}

struct CustomAuthenticatorProvider;

#[async_trait]
impl AuthenticatorProvider for CustomAuthenticatorProvider {
    async fn start_authentication_session(&self, _authenticator_name: &str) -> Result<(Option<Vec<u8>>, Box<dyn AuthenticatorSession>), AuthError> {
        Ok((None, Box::new(CustomAuthenticator)))
    }
}

let session: Session = SessionBuilder::new()
    .known_node("127.0.0.1:9042")
    .use_keyspace("my_keyspace_name", false)
    .user("cassandra", "cassandra")
    .authenticator_provider(Arc::new(CustomAuthenticatorProvider))
    .build()
    .await?;
source

pub fn address_translator(self, translator: Arc<dyn AddressTranslator>) -> Self

Uses a custom address translator for peer addresses retrieved from the cluster. By default, no translation is performed.

Example
struct IdentityTranslator;

#[async_trait]
impl AddressTranslator for IdentityTranslator {
    async fn translate_address(
        &self,
        untranslated_peer: &UntranslatedPeer
    ) -> Result<SocketAddr, TranslationError> {
        Ok(untranslated_peer.untranslated_address)
    }
}

let session: Session = SessionBuilder::new()
    .known_node("127.0.0.1:9042")
    .address_translator(Arc::new(IdentityTranslator))
    .build()
    .await?;
Example
let mut translation_rules = HashMap::new();
let addr_before_translation = SocketAddr::from_str("192.168.0.42:19042").unwrap();
let addr_after_translation = SocketAddr::from_str("157.123.12.42:23203").unwrap();
translation_rules.insert(addr_before_translation, addr_after_translation);
let session: Session = SessionBuilder::new()
    .known_node("127.0.0.1:9042")
    .address_translator(Arc::new(translation_rules))
    .build()
    .await?;
source

pub fn write_coalescing(self, enable: bool) -> Self

If true, the driver will inject a small delay before flushing data to the socket - by rescheduling the task that writes data to the socket. This gives the task an opportunity to collect more write requests and write them in a single syscall, increasing the efficiency.

However, this optimization may worsen latency if the rate of requests issued by the application is low, but otherwise the application is heavily loaded with other tasks on the same tokio executor. Please do performance measurements before committing to disabling this option.

This option is true by default.

Example
let session: Session = SessionBuilder::new()
    .known_node("127.0.0.1:9042")
    .write_coalescing(false) // Enabled by default
    .build()
    .await?;

Trait Implementations§

source§

impl Default for SessionBuilder

Creates a SessionBuilder with default configuration, same as SessionBuilder::new

source§

fn default() -> Self

Returns the “default value” for a type. Read more