Crate armature_distributed

Crate armature_distributed 

Source
Expand description

Distributed Systems Support for Armature

This crate provides distributed coordination primitives.

§Features

  • Distributed Locks - Redis-based distributed locks
  • Leader Election - Automatic leader election with callbacks
  • TTL Management - Automatic lock/leadership renewal
  • RAII Pattern - Automatic cleanup on drop

§Quick Start

§Distributed Locks

use armature_distributed::*;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Connect to Redis
    let client = redis::Client::open("redis://127.0.0.1/")?;
    let conn = client.get_connection_manager().await?;

    // Create a distributed lock
    let lock = RedisLock::new("my-resource", Duration::from_secs(30), conn);

    // Acquire the lock
    let guard = lock.acquire().await?;

    // Critical section
    println!("Lock acquired, doing work...");

    // Lock is automatically released when guard is dropped
    drop(guard);

    Ok(())
}

§Leader Election

use armature_distributed::*;
use std::sync::Arc;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = redis::Client::open("redis://127.0.0.1/")?;
    let conn = client.get_connection_manager().await?;

    let election = Arc::new(
        LeaderElection::new("my-service-leader", Duration::from_secs(30), conn)
            .on_elected(|| async {
                println!("I am the leader!");
            })
            .on_revoked(|| async {
                println!("I lost leadership");
            })
    );

    // Start election (runs in background)
    let election_clone = election.clone();
    tokio::spawn(async move {
        election_clone.start().await
    });

    // Check leadership status
    if election.is_leader() {
        println!("This node is the leader");
    }

    Ok(())
}

Re-exports§

pub use leader::LeaderElection;
pub use leader::LeaderElectionBuilder;
pub use leader::LeaderError;
pub use lock::DistributedLock;
pub use lock::LockBuilder;
pub use lock::LockError;
pub use lock::LockGuard;
pub use lock::RedisLock;

Modules§

leader
Distributed leader election using Redis
lock
Distributed locks using Redis