1 unstable release
Uses new Rust 2024
| 0.3.0 | Jul 13, 2025 |
|---|
#54 in #core
86 downloads per month
Used in 7 crates
13KB
112 lines
revoke-core
Core traits and types for the Revoke microservices framework.
Overview
revoke-core provides the foundational abstractions and common types used throughout the Revoke framework. It defines the core traits that all implementations must follow, ensuring consistency and interoperability between different components.
Features
- Service Registry Trait: Abstract interface for service discovery and registration
- Configuration Provider Trait: Unified configuration management interface
- Message Queue Trait: Common messaging abstraction
- Health Check Types: Standardized health status reporting
- Error Types: Consistent error handling across the framework
- Middleware Configurations: Common patterns for retry and circuit breaker
Core Traits
ServiceRegistry
Manages service registration and discovery:
use revoke_core::{ServiceRegistry, ServiceInfo, Protocol};
use uuid::Uuid;
// Register a service
let service = ServiceInfo {
id: Uuid::new_v4(),
name: "user-service".to_string(),
version: "1.0.0".to_string(),
address: "127.0.0.1".to_string(),
port: 8080,
protocol: Protocol::Http,
metadata: HashMap::new(),
};
registry.register(service).await?;
// Discover services
let services = registry.get_service("user-service").await?;
// Update health status
let health = HealthStatus {
service_id: service.id,
status: Status::Healthy,
last_check: chrono::Utc::now(),
message: Some("Service is running".to_string()),
};
registry.update_health(health).await?;
ConfigProvider
Provides configuration management:
use revoke_core::ConfigProvider;
// Get configuration value
let db_url = provider.get("database.url").await?;
// Set configuration value
provider.set("app.name", "my-service").await?;
// Watch for configuration changes
let mut stream = provider.watch("feature.flags").await?;
while let Some(value) = stream.next().await {
println!("Config changed: {}", value);
}
MessageQueue
Abstract message queue operations:
use revoke_core::MessageQueue;
// Publish a message
let message = b"Hello, World!";
queue.publish("events", message).await?;
// Subscribe to messages
let mut stream = queue.subscribe("events").await?;
while let Some(msg) = stream.next().await {
println!("Received: {:?}", msg);
}
Types
ServiceInfo
Represents a service instance:
pub struct ServiceInfo {
pub id: Uuid,
pub name: String,
pub version: String,
pub address: String,
pub port: u16,
pub protocol: Protocol,
pub metadata: HashMap<String, String>,
}
Protocol
Supported communication protocols:
pub enum Protocol {
Http,
Https,
Grpc,
Tcp,
}
HealthStatus
Service health information:
pub struct HealthStatus {
pub service_id: Uuid,
pub status: Status,
pub last_check: DateTime<Utc>,
pub message: Option<String>,
}
pub enum Status {
Healthy,
Unhealthy,
Unknown,
}
Error Handling
The framework uses a unified error type:
pub enum RevokeError {
ServiceNotFound(String),
ConfigError(String),
ConnectionError(String),
SerializationError(#[from] serde_json::Error),
IoError(#[from] std::io::Error),
Unknown(String),
}
pub type Result<T> = std::result::Result<T, RevokeError>;
Middleware Configuration
RetryConfig
Configuration for retry middleware:
use revoke_core::middleware::RetryConfig;
use std::time::Duration;
let config = RetryConfig {
max_attempts: 3,
initial_delay: Duration::from_millis(100),
max_delay: Duration::from_secs(10),
multiplier: 2.0,
};
CircuitBreakerConfig
Configuration for circuit breaker:
use revoke_core::middleware::CircuitBreakerConfig;
use std::time::Duration;
let config = CircuitBreakerConfig {
failure_threshold: 5,
success_threshold: 2,
timeout: Duration::from_secs(60),
};
Usage
Add to your Cargo.toml:
[dependencies]
revoke-core = { version = "0.1.0" }
Then use the traits in your implementation:
use revoke_core::{ServiceRegistry, ConfigProvider, Result};
use async_trait::async_trait;
struct MyRegistry;
#[async_trait]
impl ServiceRegistry for MyRegistry {
async fn register(&self, service: ServiceInfo) -> Result<()> {
// Implementation
Ok(())
}
// ... other methods
}
Design Principles
- Trait-based Abstraction: All major components are defined as traits, allowing for flexible implementations
- Async-first: All operations are async by default using Tokio
- Type Safety: Strong typing with serde serialization
- Error Propagation: Consistent error handling with the
?operator - Zero-copy Streaming: Uses streams for efficient data transfer
Integration
revoke-core is designed to be the foundation for:
- Service registries (Consul, etcd, Kubernetes)
- Configuration providers (Consul KV, etcd, files)
- Message queues (Redis, Kafka, RabbitMQ)
- Health check systems
- Monitoring and metrics collection
Examples
See the individual module documentation for specific implementation examples:
- revoke-registry - Service discovery implementations
- revoke-config - Configuration management
- revoke-mq - Message queue clients
Dependencies
~5–16MB
~174K SLoC