1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#![allow(unused_variables, dead_code, missing_docs)]
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
use tracing::debug;
use crate::common::url::Url;
use super::{NotifyListener, Registry};
pub const REGISTRY_GROUP_KEY: &str = "registry.group";
#[derive(Debug, Default, Clone)]
pub struct MemoryRegistry {
registries: Arc<RwLock<HashMap<String, String>>>,
}
impl MemoryRegistry {
pub fn new() -> MemoryRegistry {
MemoryRegistry {
registries: Arc::new(RwLock::new(HashMap::new())),
}
}
}
impl Registry for MemoryRegistry {
type NotifyListener = MemoryNotifyListener;
fn register(&mut self, mut url: Url) -> Result<(), crate::StdError> {
let registry_group = match url.get_param(REGISTRY_GROUP_KEY) {
Some(key) => key,
None => "dubbo".to_string(),
};
let dubbo_path = format!(
"/{}/{}/{}",
registry_group,
url.get_service_name(),
"provider",
);
url.params.insert("anyhost".to_string(), "true".to_string());
let raw_url = url.raw_url_string();
self.registries.write().unwrap().insert(dubbo_path, raw_url);
Ok(())
}
fn unregister(&mut self, url: crate::common::url::Url) -> Result<(), crate::StdError> {
let registry_group = match url.get_param(REGISTRY_GROUP_KEY) {
Some(key) => key,
None => "dubbo".to_string(),
};
let dubbo_path = format!(
"/{}/{}/{}",
registry_group,
url.get_service_name(),
"provider",
);
self.registries.write().unwrap().remove(&dubbo_path);
Ok(())
}
fn subscribe(
&self,
url: crate::common::url::Url,
listener: Self::NotifyListener,
) -> Result<(), crate::StdError> {
todo!()
}
fn unsubscribe(
&self,
url: crate::common::url::Url,
listener: Self::NotifyListener,
) -> Result<(), crate::StdError> {
todo!()
}
}
pub struct MemoryNotifyListener {
pub service_instances: Arc<RwLock<HashMap<String, Vec<Url>>>>,
}
impl NotifyListener for MemoryNotifyListener {
fn notify(&self, event: super::ServiceEvent) {
debug!("notify {:?}", event);
let mut map = self.service_instances.write().expect("msg");
match event.action.as_str() {
"ADD" => map.insert(event.key, event.service),
&_ => todo!(),
};
}
fn notify_all(&self, event: super::ServiceEvent) {
todo!()
}
}