@@ -17,6 +17,7 @@ use std::collections::HashMap;
1717use std:: sync:: Arc ;
1818
1919use tokio:: runtime:: { Builder , Runtime } ;
20+ use tokio:: sync:: RwLock ;
2021
2122use crate :: config:: req:: {
2223 CreateConfigFileRequest , GetConfigFileRequest , PublishConfigFileRequest ,
@@ -35,8 +36,10 @@ use crate::discovery::req::{
3536} ;
3637
3738use super :: model:: config:: ConfigFile ;
39+ use super :: model:: naming:: ServiceInstances ;
3840use super :: plugin:: cache:: { Filter , ResourceCache , ResourceListener } ;
3941use super :: plugin:: connector:: Connector ;
42+ use super :: plugin:: loadbalance:: LoadBalancer ;
4043use super :: plugin:: location:: { LocationProvider , LocationSupplier } ;
4144
4245pub struct Engine
@@ -47,10 +50,11 @@ where
4750 local_cache : Arc < Box < dyn ResourceCache > > ,
4851 server_connector : Arc < Box < dyn Connector > > ,
4952 location_provider : Arc < LocationProvider > ,
53+ load_balancer : Arc < RwLock < HashMap < String , Arc < Box < dyn LoadBalancer > > > > > ,
5054}
5155
5256impl Engine {
53- pub fn new ( conf : Configuration ) -> Result < Self , PolarisError > {
57+ pub fn new ( arc_conf : Arc < Configuration > ) -> Result < Self , PolarisError > {
5458 let runtime = Arc :: new (
5559 Builder :: new_multi_thread ( )
5660 . enable_all ( )
@@ -60,7 +64,6 @@ impl Engine {
6064 . unwrap ( ) ,
6165 ) ;
6266
63- let arc_conf = Arc :: new ( conf) ;
6467 let client_id = crate :: core:: plugin:: plugins:: acquire_client_id ( arc_conf. clone ( ) ) ;
6568
6669 // 初始化 extensions
@@ -101,6 +104,7 @@ impl Engine {
101104 local_cache : Arc :: new ( local_cache) ,
102105 server_connector : server_connector,
103106 location_provider : Arc :: new ( location_provider) ,
107+ load_balancer : Arc :: new ( RwLock :: new ( extension. load_loadbalancers ( ) ) ) ,
104108 } )
105109 }
106110
@@ -220,17 +224,12 @@ impl Engine {
220224
221225 let svc_ins = ret. unwrap ( ) ;
222226
223- if only_available {
224- Ok ( InstancesResponse {
225- service_info : svc_ins. service ,
226- instances : svc_ins. available_instances ,
227- } )
228- } else {
229- Ok ( InstancesResponse {
230- service_info : svc_ins. service ,
231- instances : svc_ins. instances ,
232- } )
233- }
227+ Ok ( InstancesResponse {
228+ instances : ServiceInstances :: new (
229+ svc_ins. get_service_info ( ) ,
230+ svc_ins. list_instances ( only_available) . await ,
231+ ) ,
232+ } )
234233 }
235234
236235 /// get_service_rule 获取服务规则
@@ -340,6 +339,11 @@ impl Engine {
340339 } ;
341340 }
342341
342+ pub async fn lookup_loadbalancer ( & self , name : & str ) -> Option < Arc < Box < dyn LoadBalancer > > > {
343+ let lb = self . load_balancer . read ( ) . await ;
344+ lb. get ( name) . map ( |lb| lb. clone ( ) )
345+ }
346+
343347 /// register_resource_listener 注册资源监听器
344348 pub async fn register_resource_listener ( & self , listener : Arc < dyn ResourceListener > ) {
345349 self . local_cache . register_resource_listener ( listener) . await ;
0 commit comments