apache_dubbo/registry/
memory_registry.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#![allow(unused_variables, dead_code, missing_docs)]
19use std::collections::HashMap;
20use std::sync::RwLock;
21
22use super::{NotifyListener, Registry};
23
24// 从url中获取服务注册的元数据
25/// rawURL = fmt.Sprintf("%s://%s%s?%s", c.Protocol, host, c.Path, s)
26/// dubboPath = fmt.Sprintf("/%s/%s/%s", r.URL.GetParam(constant.RegistryGroupKey, "dubbo"), r.service(c), common.DubboNodes[common.PROVIDER])
27
28pub const REGISTRY_GROUP_KEY: &str = "registry.group";
29
30#[derive(Debug, Default)]
31pub struct MemoryRegistry {
32    registries: RwLock<HashMap<String, String>>,
33}
34
35impl MemoryRegistry {
36    pub fn new() -> MemoryRegistry {
37        MemoryRegistry {
38            registries: RwLock::new(HashMap::new()),
39        }
40    }
41}
42
43impl Registry for MemoryRegistry {
44    type NotifyListener = MemoryNotifyListener;
45
46    fn register(&mut self, mut url: crate::common::url::Url) -> Result<(), crate::StdError> {
47        // define provider label: ${registry.group}/${service_name}/provider
48        let registry_group = match url.get_param(REGISTRY_GROUP_KEY.to_string()) {
49            Some(key) => key,
50            None => "dubbo".to_string(),
51        };
52
53        let dubbo_path = format!(
54            "/{}/{}/{}",
55            registry_group,
56            url.get_service_name().join(","),
57            "provider",
58        );
59
60        url.params.insert("anyhost".to_string(), "true".to_string());
61        // define triple url path
62        let raw_url = format!("{}?{}", url.to_url(), url.encode_param(),);
63
64        self.registries.write().unwrap().insert(dubbo_path, raw_url);
65        Ok(())
66    }
67
68    fn unregister(&mut self, url: crate::common::url::Url) -> Result<(), crate::StdError> {
69        let registry_group = match url.get_param(REGISTRY_GROUP_KEY.to_string()) {
70            Some(key) => key,
71            None => "dubbo".to_string(),
72        };
73
74        let dubbo_path = format!(
75            "/{}/{}/{}",
76            registry_group,
77            url.get_service_name().join(","),
78            "provider",
79        );
80        self.registries.write().unwrap().remove(&dubbo_path);
81
82        Ok(())
83    }
84
85    fn subscribe(
86        &self,
87        url: crate::common::url::Url,
88        listener: Self::NotifyListener,
89    ) -> Result<(), crate::StdError> {
90        todo!()
91    }
92
93    fn unsubscribe(
94        &self,
95        url: crate::common::url::Url,
96        listener: Self::NotifyListener,
97    ) -> Result<(), crate::StdError> {
98        todo!()
99    }
100}
101
102pub struct MemoryNotifyListener {}
103
104impl NotifyListener for MemoryNotifyListener {
105    fn notify(&self, event: super::ServiceEvent) {
106        todo!()
107    }
108
109    fn notify_all(&self, event: super::ServiceEvent) {
110        todo!()
111    }
112}