use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub use crate::error::DiscoveryError;
#[derive(Debug, Clone)]
pub struct ServiceInstance {
pub id: String,
pub name: String,
pub address: String,
pub metadata: HashMap<String, String>,
pub status: InstanceStatus,
pub last_heartbeat: Option<std::time::Instant>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum InstanceStatus {
Healthy,
Unhealthy,
Starting,
Draining,
}
pub struct WatchStream {
pub rx: tokio::sync::mpsc::Receiver<Vec<ServiceInstance>>,
}
#[async_trait]
pub trait Discovery: Send + Sync + 'static {
async fn register(&self, instance: ServiceInstance) -> Result<(), DiscoveryError>;
async fn discover(&self, service_name: &str) -> Result<Vec<ServiceInstance>, DiscoveryError>;
async fn heartbeat(&self, service_name: &str, instance_id: &str) -> Result<(), DiscoveryError>;
async fn deregister(&self, service_name: &str, instance_id: &str)
-> Result<(), DiscoveryError>;
async fn watch(&self, service_name: &str) -> Result<WatchStream, DiscoveryError>;
}