| Crates.io | auto-discovery |
| lib.rs | auto-discovery |
| version | 0.2.0 |
| created_at | 2025-07-11 15:38:05.875518+00 |
| updated_at | 2025-07-15 19:55:04.549289+00 |
| description | A general-purpose network and system service discovery library for Rust applications |
| homepage | |
| repository | https://github.com/ciresnave/auto-discovery |
| max_upload_size | |
| id | 1748080 |
| size | 503,379 |
A production-grade network and system service discovery library for Rust applications that provides an extensible, async-first solution for automatically detecting, connecting to, and coordinating with other services in a network environment.
use auto_discovery::safety::{SafetyConfig, SafetyManager};
// Configure safety features
let safety_config = SafetyConfig {
rate_limit_per_second: 10,
retry_max_attempts: 3,
retry_initial_interval: Duration::from_millis(100),
health_check_interval: Duration::from_secs(1),
operation_timeout: Duration::from_secs(5),
};
let safety_manager = SafetyManager::new(safety_config);
// Use with automatic retry
let result = safety_manager.with_retry(|| Box::pin(async {
manager.register_service(service.clone()).await
})).await?;
// Rate limit check
safety_manager.check_rate_limit(&service_type).await?;
use auto_discovery::safety::{LoadBalancer, LoadBalancerConfig, LoadBalancingStrategy};
// Configure load balancer
let lb_config = LoadBalancerConfig {
strategy: LoadBalancingStrategy::LeastLoaded,
decay_time: Duration::from_secs(10),
rtt_threshold: Duration::from_millis(100),
};
let balancer = LoadBalancer::new(lb_config);
// Add services to load balancer
balancer.update_service(service.clone(), 0.0).await?;
// Select optimal service
let selected = balancer.select_service()
.expect("Should have services available");
// Record metrics
balancer.record_request(
&selected.id(),
Duration::from_millis(50),
true
);
use auto_discovery::safety::{HealthMonitor, ServiceStatus};
let health_monitor = HealthMonitor::new();
// Update service health
health_monitor.update_service(&service, is_healthy);
// Check service status
if let Some(status) = health_monitor.get_service_status(&service.id()) {
match status {
ServiceStatus::Healthy => println!("Service is healthy"),
ServiceStatus::Degraded => println!("Service is degraded"),
ServiceStatus::Unhealthy => println!("Service is unhealthy"),
}
}
// Clean up stale entries
health_monitor.cleanup_stale(Duration::from_secs(300));
Add this to your Cargo.toml:
[dependencies]
auto-discovery = "0.1.0"
tokio = { version = "1.0", features = ["full"] }
use auto_discovery::{
config::DiscoveryConfig,
protocols::ProtocolManagerBuilder,
service::ServiceInfo,
types::ServiceType,
};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure discovery with builder pattern
let config = DiscoveryConfig::builder()
.discovery_interval(Duration::from_secs(5))
.verify_services(true)
.build();
// Create protocol manager with selected protocols
let mut manager = ProtocolManagerBuilder::new(config)
.with_mdns(true)
.with_upnp(true)
.build()
.await?;
// Register our service
let service = ServiceInfo::new(
"my_service",
"_http._tcp",
8080,
Some(vec![("version", "1.0")]),
);
manager.register_service(service).await?;
// Discover services
let service_types = vec![ServiceType::new("_http._tcp")?];
let services = manager
.discover_services(service_types, Duration::from_secs(5))
.await?;
// Process discovered services
for service in services {
println!("Found service: {} at {}:{}",
service.name(),
service.address(),
service.port());
}
Ok(())
}
The library uses a protocol manager to handle multiple discovery protocols:
use auto_discovery::protocols::ProtocolManagerBuilder;
// Create a manager with selected protocols
let manager = ProtocolManagerBuilder::new(config)
.with_mdns(true) // Enable mDNS
.with_upnp(true) // Enable UPnP
.with_dns_sd(false) // Disable DNS-SD
.build()
.await?;
// Discover services across all enabled protocols
let services = manager
.discover_services(service_types, timeout)
.await?;
Use the builder pattern for type-safe configuration:
use auto_discovery::config::DiscoveryConfig;
let config = DiscoveryConfig::builder()
.discovery_interval(Duration::from_secs(30))
.verify_services(true)
.network_interface(NetworkInterface::new("eth0"))
.build();
The crate includes several examples demonstrating different features:
builder_pattern.rs: Using the builder pattern for configurationcross_protocol.rs: Working with multiple protocolsbasic_usage.rs: Simple service discoverysecurity_verification.rs: Service verification featuresRun an example with:
cargo run --example builder_pattern
The library currently supports the following protocols:
mDNS: Multicast DNS for local network service discovery
UPnP: Universal Plug and Play for device discovery
DNS-SD: DNS Service Discovery (in development)
Run the test suite:
cargo test
This includes:
// Enable service verification in config
let config = DiscoveryConfig::builder()
.verify_services(true)
.build();
// Verify a specific service
let verified = manager.verify_service(&service).await?;
// Discover services across multiple protocols
let service_types = vec![
ServiceType::new("_http._tcp")?, // mDNS
ServiceType::new("urn:my-service:1")?, // UPnP
];
let services = manager
.discover_services(service_types, timeout)
.await?;
For detailed documentation and API reference, visit docs.rs/auto-discovery.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate and follow the existing code style.
Licensed under either of:
at your option.
For more detailed documentation, visit docs.rs/auto-discovery.