picodata-rust
Description
picodata-rust
is an asynchronous Rust driver for interacting with a Picodata cluster based on PostgreSQL. It provides load balancing across nodes, automatic topology discovery, and connection pooling.
Installation
Add the following to your Cargo.toml
:
[dependencies]
picodata-rust = { version = "1.0.0" }
Enable TLS support if needed:
[dependencies]
picodata-rust = { version = "1.0.0", features = ["tls"] }
Quick Start
use std::time::Duration;
use picodata_rust::{Config, connect_cfg, Client, Error};
#[tokio::main]
async fn main() -> Result<(), Error> {
let mut pg_cfg = pg::Config::new();
pg_cfg.host("127.0.0.1");
pg_cfg.user("user");
pg_cfg.password("password");
pg_cfg.dbname("mydb");
let mut cfg = Config {
postgres: pg_cfg,
pool_max_size: 10,
discorery_interval: Duration::from_secs(1),
};
let client: Client = connect_cfg(cfg).await?;
let rows = client.query(
"SELECT id, name FROM users WHERE active = $1",
&[&true]
).await?;
for row in rows {
let id: i32 = row.get("id");
let name: &str = row.get("name");
println!("User {}: {}", id, name);
}
client.close().await?;
Ok(())
}
TLS Configuration Example
If you built the driver with the tls
feature enabled in Cargo.toml
, you can simply provide paths to your certificate files in the Config
struct:
use std::time::Duration;
use picodata_rust::{Config, connect_cfg, Client, Error};
#[tokio::main]
async fn main() -> Result<(), Error> {
let mut pg_cfg = pg::Config::new();
pg_cfg.host("127.0.0.1");
pg_cfg.user("user");
pg_cfg.password("password");
pg_cfg.dbname("mydb");
let mut cfg = Config {
postgres: pg_cfg,
pool_max_size: 10,
discorery_interval: Duration::from_secs(1),
ssl_cert_file: Some("/path/to/client.crt".into()),
ssl_key_file: Some("/path/to/client.key".into()),
ssl_ca_file: Some("/path/to/ca-cert.pem".into()),
};
let client: Client = connect_cfg(cfg).await?;
client.close().await?;
Ok(())
}
Custom Load Balancing Strategy
By default, RoundRobin
is used, but you can supply any strategy implementing the Strategy
trait:
use picodata_rust::{Config, client::Client, strategy::{Random, Strategy}};
let client = Client::new_with_strategy(cfg, Random).await?;