16
16
*/
17
17
18
18
use std:: collections:: HashMap ;
19
- use std:: sync:: Arc ;
19
+ use std:: sync:: { Arc , RwLock } ;
20
20
21
21
use super :: memory_registry:: MemoryRegistry ;
22
22
use super :: BoxRegistry ;
23
23
use crate :: codegen:: TripleInvoker ;
24
- use crate :: common:: consts;
25
24
use crate :: common:: url:: Url ;
26
25
use crate :: protocol:: triple:: triple_exporter:: TripleExporter ;
26
+ use crate :: protocol:: triple:: triple_protocol:: TripleProtocol ;
27
27
use crate :: protocol:: BoxExporter ;
28
28
use crate :: protocol:: BoxInvoker ;
29
29
use crate :: protocol:: Protocol ;
30
30
31
31
#[ derive( Clone , Default ) ]
32
32
pub struct RegistryProtocol {
33
33
// registerAddr: Registry
34
- registries : Arc < HashMap < String , BoxRegistry > > ,
34
+ registries : Arc < RwLock < HashMap < String , BoxRegistry > > > ,
35
35
// providerUrl: Exporter
36
- exporters : Arc < HashMap < String , BoxExporter > > ,
36
+ exporters : Arc < RwLock < HashMap < String , BoxExporter > > > ,
37
+ // serviceName: registryUrls
38
+ services : HashMap < String , Vec < Url > > ,
37
39
}
38
40
39
41
impl RegistryProtocol {
40
42
pub fn new ( ) -> Self {
41
43
RegistryProtocol {
42
- registries : Arc :: new ( HashMap :: new ( ) ) ,
43
- exporters : Arc :: new ( HashMap :: new ( ) ) ,
44
+ registries : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
45
+ exporters : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
46
+ services : HashMap :: new ( ) ,
44
47
}
45
48
}
46
49
47
- pub fn get_registry ( & self , url : Url ) -> BoxRegistry {
48
- // self.registries.clone().insert(url.location.clone(), Box::new(MemoryRegistry::default()));
50
+ pub fn with_services ( mut self , services : HashMap < String , Vec < Url > > ) -> Self {
51
+ self . services . extend ( services) ;
52
+ self
53
+ }
54
+
55
+ pub fn get_registry ( & mut self , url : Url ) -> BoxRegistry {
56
+ let mem = MemoryRegistry :: default ( ) ;
57
+ self . registries
58
+ . write ( )
59
+ . unwrap ( )
60
+ . insert ( url. location , Box :: new ( mem. clone ( ) ) ) ;
49
61
50
- // *(self.registries.get(&url.location).unwrap())
51
- Box :: new ( MemoryRegistry :: default ( ) )
62
+ Box :: new ( mem)
52
63
}
53
64
}
54
65
@@ -60,14 +71,34 @@ impl Protocol for RegistryProtocol {
60
71
todo ! ( )
61
72
}
62
73
63
- async fn export ( self , url : Url ) -> BoxExporter {
74
+ async fn export ( mut self , url : Url ) -> BoxExporter {
64
75
// getProviderUrl
65
76
// getRegisterUrl
66
77
// init Exporter based on provider_url
67
78
// server registry based on register_url
68
79
// start server health check
69
- Box :: new ( TripleExporter :: new ( ) )
80
+ let registry_url = self . services . get ( url. get_service_name ( ) . join ( "," ) . as_str ( ) ) ;
81
+ if let Some ( urls) = registry_url {
82
+ for url in urls. clone ( ) . iter ( ) {
83
+ if !url. protocol . is_empty ( ) {
84
+ let mut reg = self . get_registry ( url. clone ( ) ) ;
85
+ reg. register ( url. clone ( ) ) . unwrap ( ) ;
86
+ }
87
+ }
88
+ }
89
+
90
+ match url. clone ( ) . protocol . as_str ( ) {
91
+ "triple" => {
92
+ let pro = Box :: new ( TripleProtocol :: new ( ) ) ;
93
+ return pro. export ( url) . await ;
94
+ }
95
+ _ => {
96
+ tracing:: error!( "protocol {:?} not implemented" , url. protocol) ;
97
+ Box :: new ( TripleExporter :: new ( ) )
98
+ }
99
+ }
70
100
}
101
+
71
102
async fn refer ( self , url : Url ) -> Self :: Invoker {
72
103
// getRegisterUrl
73
104
// get Registry from registry_url
@@ -76,15 +107,3 @@ impl Protocol for RegistryProtocol {
76
107
Box :: new ( TripleInvoker :: new ( url) )
77
108
}
78
109
}
79
-
80
- fn get_registry_url ( mut url : Url ) -> Url {
81
- if url. protocol == consts:: REGISTRY_PROTOCOL {
82
- url. protocol = url. get_param ( "registry" . to_string ( ) ) . unwrap ( ) ;
83
- }
84
-
85
- url
86
- }
87
-
88
- fn get_provider_url ( url : Url ) -> Url {
89
- url
90
- }
0 commit comments