use crate::error::RegistryError;
use std::collections::HashMap;
use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct ServiceInfo {
pub name: String,
pub id: String,
pub address: SocketAddr,
pub metadata: HashMap<String, String>,
pub ttl: Duration,
}
impl ServiceInfo {
pub fn new(name: impl Into<String>, id: impl Into<String>, address: SocketAddr) -> Self {
Self {
name: name.into(),
id: id.into(),
address,
metadata: HashMap::new(),
ttl: Duration::from_secs(10),
}
}
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
pub fn with_ttl(mut self, ttl: Duration) -> Self {
self.ttl = ttl;
self
}
}
pub trait ServiceRegistry: Send {
fn register<'a>(
&'a mut self,
service: &'a ServiceInfo,
) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + 'a>>;
fn deregister<'a>(
&'a mut self,
service: &'a ServiceInfo,
) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + 'a>>;
fn send_heartbeat<'a>(
&'a mut self,
service_id: &'a str,
) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + 'a>>;
fn shutdown(&mut self) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + '_>>;
}