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
24
use crate :: common:: consts;
25
25
use crate :: common:: url:: Url ;
26
26
use crate :: protocol:: triple:: triple_exporter:: TripleExporter ;
27
+ use crate :: protocol:: triple:: triple_protocol:: TripleProtocol ;
27
28
use crate :: protocol:: BoxExporter ;
28
29
use crate :: protocol:: BoxInvoker ;
29
30
use crate :: protocol:: Protocol ;
30
31
31
32
#[ derive( Clone , Default ) ]
32
33
pub struct RegistryProtocol {
33
34
// registerAddr: Registry
34
- registries : Arc < HashMap < String , BoxRegistry > > ,
35
+ registries : Arc < RwLock < HashMap < String , BoxRegistry > > > ,
35
36
// providerUrl: Exporter
36
- exporters : Arc < HashMap < String , BoxExporter > > ,
37
+ exporters : Arc < RwLock < HashMap < String , BoxExporter > > > ,
37
38
}
38
39
39
40
impl RegistryProtocol {
40
41
pub fn new ( ) -> Self {
41
42
RegistryProtocol {
42
- registries : Arc :: new ( HashMap :: new ( ) ) ,
43
- exporters : Arc :: new ( HashMap :: new ( ) ) ,
43
+ registries : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
44
+ exporters : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
44
45
}
45
46
}
46
47
47
- pub fn get_registry ( & self , url : Url ) -> BoxRegistry {
48
- // self.registries.clone().insert(url.location.clone(), Box::new(MemoryRegistry::default()));
48
+ pub fn get_registry ( & mut self , url : Url ) -> BoxRegistry {
49
+ let mem = MemoryRegistry :: default ( ) ;
50
+ self . registries
51
+ . write ( )
52
+ . unwrap ( )
53
+ . insert ( url. location , Box :: new ( mem. clone ( ) ) ) ;
49
54
50
- // *(self.registries.get(&url.location).unwrap())
51
- Box :: new ( MemoryRegistry :: default ( ) )
55
+ Box :: new ( mem)
52
56
}
53
57
}
54
58
@@ -60,12 +64,27 @@ impl Protocol for RegistryProtocol {
60
64
todo ! ( )
61
65
}
62
66
63
- async fn export ( self , url : Url ) -> BoxExporter {
67
+ async fn export ( mut self , url : Url ) -> BoxExporter {
64
68
// getProviderUrl
65
69
// getRegisterUrl
66
70
// init Exporter based on provider_url
67
71
// server registry based on register_url
68
72
// start server health check
73
+ let registry_url = get_registry_url ( url. clone ( ) ) ;
74
+ if !registry_url. protocol . is_empty ( ) {
75
+ let mut reg = self . get_registry ( registry_url. clone ( ) ) ;
76
+ reg. register ( registry_url. clone ( ) ) . unwrap ( ) ;
77
+ }
78
+
79
+ match url. clone ( ) . protocol . as_str ( ) {
80
+ "triple" => {
81
+ let pro = Box :: new ( TripleProtocol :: new ( ) ) ;
82
+ return pro. export ( url) . await ;
83
+ }
84
+ _ => {
85
+ tracing:: error!( "protocol {:?} not implemented" , url. protocol) ;
86
+ }
87
+ }
69
88
Box :: new ( TripleExporter :: new ( ) )
70
89
}
71
90
async fn refer ( self , url : Url ) -> Self :: Invoker {
0 commit comments