Rust SDK
Installation
Add to Cargo.toml:
[dependencies]
tollmeshcache = "1.0"
tokio = { version = "1", features = ["full"] }
Quick Start
use tollmeshcache::{Client, ClientConfig};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = ClientConfig::new()
.with_host("localhost")
.with_port(8080);
let client = Client::new(config)?;
// Rate limiting
let result = client.consume("user-123", 100, Duration::from_secs(60)).await?;
if result.ok {
println!("Request allowed");
}
Ok(())
}
Features
- ✅ Full async/await with tokio
- ✅ Type-safe error handling
- ✅ Zero-cost abstractions
- ✅ Connection pooling with reqwest
- ✅ Exponential backoff retry logic
API Reference
This section covers rate limiting, replay protection, and caching. This SDK also has full, identical-API support for Job Queues, Sorted Sets, Streams, Pub/Sub, Transactions, Persistence, Pipelines, WASM Scripting, Search, Ranking, and Metrics — see the full API Reference for all of them, with live-verified examples.
Rate Limiting
let result = client.consume(key, limit, window).await?;
// Fields: ok, remaining, reset_at
Replay Protection
let result = client.seen(key, ttl).await?;
// Field: seen
Caching
client.cache_set(namespace, key, value, ttl).await?;
let (value, exists) = client.cache_get(namespace, key).await?;
Health
let health = client.health().await?;
let peers = client.get_peers().await?;
Error Handling
use tollmeshcache::{TollMeshError, ErrorCode};
match client.consume("key", 100, Duration::from_secs(60)).await {
Ok(result) => println!("Success: {:?}", result),
Err(error) => {
match error {
TollMeshError::RateLimited => println!("Rate limited"),
TollMeshError::NotFound => println!("Not found"),
_ => eprintln!("Error: {}", error),
}
}
}
Configuration
let config = ClientConfig::new()
.with_host("localhost")
.with_port(8080)
.with_timeout(Duration::from_secs(5))
.with_api_key("optional-key")
.with_verify_ssl(true)
.with_max_retries(3)
.with_connection_pool_size(10);
let client = Client::new(config)?;
Retry Logic
use tollmeshcache::with_retry;
let result = with_retry(
|| async { client.consume("key", 100, Duration::from_secs(60)).await },
Default::default()
).await?;
Testing
cargo test
Examples
See examples/ for:
rate_limiting.rs- Rate limiting patterns
Run examples:
cargo run --example rate_limiting