1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use thiserror::Error;
5use uuid::Uuid;
6
7#[derive(Error, Debug)]
8pub enum RevokeError {
9 #[error("Service not found: {0}")]
10 ServiceNotFound(String),
11
12 #[error("Configuration error: {0}")]
13 ConfigError(String),
14
15 #[error("Connection error: {0}")]
16 ConnectionError(String),
17
18 #[error("Serialization error: {0}")]
19 SerializationError(#[from] serde_json::Error),
20
21 #[error("IO error: {0}")]
22 IoError(#[from] std::io::Error),
23
24 #[error("Unknown error: {0}")]
25 Unknown(String),
26}
27
28pub type Result<T> = std::result::Result<T, RevokeError>;
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct ServiceInfo {
32 pub id: Uuid,
33 pub name: String,
34 pub version: String,
35 pub address: String,
36 pub port: u16,
37 pub protocol: Protocol,
38 pub metadata: HashMap<String, String>,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub enum Protocol {
43 Http,
44 Https,
45 Grpc,
46 Tcp,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct HealthStatus {
51 pub service_id: Uuid,
52 pub status: Status,
53 pub last_check: chrono::DateTime<chrono::Utc>,
54 pub message: Option<String>,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
58pub enum Status {
59 Healthy,
60 Unhealthy,
61 Unknown,
62}
63
64#[async_trait]
65pub trait ServiceRegistry: Send + Sync {
66 async fn register(&self, service: ServiceInfo) -> Result<()>;
67 async fn deregister(&self, service_id: Uuid) -> Result<()>;
68 async fn get_service(&self, name: &str) -> Result<Vec<ServiceInfo>>;
69 async fn update_health(&self, status: HealthStatus) -> Result<()>;
70}
71
72#[async_trait]
73pub trait ConfigProvider: Send + Sync {
74 async fn get(&self, key: &str) -> Result<String>;
75 async fn set(&self, key: &str, value: &str) -> Result<()>;
76 async fn watch(&self, key: &str) -> Result<Box<dyn futures::Stream<Item = String> + Send + Unpin>>;
77}
78
79#[async_trait]
80pub trait MessageQueue: Send + Sync {
81 async fn publish(&self, topic: &str, message: &[u8]) -> Result<()>;
82 async fn subscribe(
83 &self,
84 topic: &str,
85 ) -> Result<Box<dyn futures::Stream<Item = Vec<u8>> + Send + Unpin>>;
86}
87
88#[derive(Debug, Clone)]
89pub struct ServiceContext {
90 pub service_info: ServiceInfo,
91 pub config: HashMap<String, String>,
92}
93
94pub mod middleware {
95 use std::time::Duration;
96
97 #[derive(Debug, Clone)]
98 pub struct RetryConfig {
99 pub max_attempts: u32,
100 pub initial_delay: Duration,
101 pub max_delay: Duration,
102 pub multiplier: f32,
103 }
104
105 impl Default for RetryConfig {
106 fn default() -> Self {
107 Self {
108 max_attempts: 3,
109 initial_delay: Duration::from_millis(100),
110 max_delay: Duration::from_secs(10),
111 multiplier: 2.0,
112 }
113 }
114 }
115
116 #[derive(Debug, Clone)]
117 pub struct CircuitBreakerConfig {
118 pub failure_threshold: u32,
119 pub success_threshold: u32,
120 pub timeout: Duration,
121 }
122
123 impl Default for CircuitBreakerConfig {
124 fn default() -> Self {
125 Self {
126 failure_threshold: 5,
127 success_threshold: 2,
128 timeout: Duration::from_secs(60),
129 }
130 }
131 }
132}