@@ -3,14 +3,15 @@ use serde_json::Value;
33use std:: collections:: HashMap ;
44use std:: fmt:: Debug ;
55use std:: sync:: Arc ;
6- use std:: time:: Duration ;
7- use tracing:: info;
8- use url:: Url ;
6+ use tracing:: trace;
97
10- use crate :: evalutate:: { EvalDetail , Repository } ;
118use crate :: sync:: Synchronizer ;
129use crate :: user:: FPUser ;
13- use crate :: { FPDetail , FPError , SdkAuthorization , Toggle } ;
10+ use crate :: {
11+ config:: FPConfig ,
12+ evalutate:: { EvalDetail , Repository } ,
13+ } ;
14+ use crate :: { FPDetail , SdkAuthorization , Toggle } ;
1415#[ cfg( feature = "event" ) ]
1516use feature_probe_event_std:: event:: AccessEvent ;
1617#[ cfg( feature = "event" ) ]
@@ -23,31 +24,26 @@ use feature_probe_event_tokio::event::AccessEvent;
2324use feature_probe_event_tokio:: recorder:: unix_timestamp;
2425#[ cfg( feature = "event_tokio" ) ]
2526use feature_probe_event_tokio:: recorder:: EventRecorder ;
26- #[ cfg( feature = "use_tokio" ) ]
27- use reqwest:: Client ;
2827
2928#[ derive( Debug , Default , Clone ) ]
3029pub struct FeatureProbe {
3130 repo : Arc < RwLock < Repository > > ,
3231 syncer : Option < Synchronizer > ,
3332 #[ cfg( any( feature = "event" , feature = "event_tokio" ) ) ]
3433 event_recorder : Option < EventRecorder > ,
35- config : InnerConfig ,
34+ config : FPConfig ,
3635 should_stop : Arc < RwLock < bool > > ,
3736}
3837
3938impl FeatureProbe {
40- pub fn new ( config : FPConfig ) -> Result < Self , FPError > {
41- let config = build_config ( config) ;
39+ pub fn new ( config : FPConfig ) -> Self {
4240 let mut slf = Self {
4341 config,
4442 ..Default :: default ( )
4543 } ;
4644
47- match slf. start ( ) {
48- Ok ( _) => Ok ( slf) ,
49- Err ( e) => Err ( e) ,
50- }
45+ slf. start ( ) ;
46+ slf
5147 }
5248
5349 pub fn new_for_test ( toggle : & str , value : Value ) -> Self {
@@ -106,7 +102,7 @@ impl FeatureProbe {
106102
107103 pub fn new_with ( server_key : String , repo : Repository ) -> Self {
108104 Self {
109- config : InnerConfig {
105+ config : FPConfig {
110106 server_sdk_key : server_key,
111107 ..Default :: default ( )
112108 } ,
@@ -119,7 +115,7 @@ impl FeatureProbe {
119115 }
120116
121117 pub fn close ( & self ) {
122- info ! ( "closing featureprobe client" ) ;
118+ trace ! ( "closing featureprobe client" ) ;
123119 #[ cfg( any( feature = "event" , feature = "event_tokio" ) ) ]
124120 if let Some ( recorder) = & self . event_recorder {
125121 recorder. flush ( ) ;
@@ -195,19 +191,15 @@ impl FeatureProbe {
195191 None
196192 }
197193
198- fn start ( & mut self ) -> Result < ( ) , FPError > {
199- self . sync ( ) ? ;
194+ fn start ( & mut self ) {
195+ self . sync ( ) ;
200196 #[ cfg( any( feature = "event" , feature = "event_tokio" ) ) ]
201- self . flush_events ( ) ?;
202- Ok ( ( ) )
197+ self . flush_events ( ) ;
203198 }
204199
205- fn sync ( & mut self ) -> Result < ( ) , FPError > {
206- info ! ( "sync url {}" , & self . config. toggles_url) ;
207- let toggles_url: Url = match Url :: parse ( & self . config . toggles_url ) {
208- Err ( e) => return Err ( FPError :: UrlError ( e. to_string ( ) ) ) ,
209- Ok ( url) => url,
210- } ;
200+ fn sync ( & mut self ) {
201+ trace ! ( "sync url {}" , & self . config. toggles_url) ;
202+ let toggles_url = self . config . toggles_url . clone ( ) ;
211203 let refresh_interval = self . config . refresh_interval ;
212204 let auth = SdkAuthorization ( self . config . server_sdk_key . clone ( ) ) . encode ( ) ;
213205 let repo = self . repo . clone ( ) ;
@@ -220,17 +212,13 @@ impl FeatureProbe {
220212 repo,
221213 ) ;
222214 self . syncer = Some ( syncer. clone ( ) ) ;
223- syncer. sync ( self . config . start_wait , self . should_stop . clone ( ) ) ?;
224- Ok ( ( ) )
215+ syncer. sync ( self . config . start_wait , self . should_stop . clone ( ) ) ;
225216 }
226217
227218 #[ cfg( any( feature = "event" , feature = "event_tokio" ) ) ]
228- fn flush_events ( & mut self ) -> Result < ( ) , FPError > {
229- info ! ( "flush_events" ) ;
230- let events_url: Url = match Url :: parse ( & self . config . events_url ) {
231- Err ( e) => return Err ( FPError :: UrlError ( e. to_string ( ) ) ) ,
232- Ok ( url) => url,
233- } ;
219+ fn flush_events ( & mut self ) {
220+ trace ! ( "flush_events" ) ;
221+ let events_url = self . config . events_url . clone ( ) ;
234222 let flush_interval = self . config . refresh_interval ;
235223 let auth = SdkAuthorization ( self . config . server_sdk_key . clone ( ) ) . encode ( ) ;
236224 let should_stop = self . should_stop . clone ( ) ;
@@ -243,7 +231,6 @@ impl FeatureProbe {
243231 should_stop,
244232 ) ;
245233 self . event_recorder = Some ( event_recorder) ;
246- Ok ( ( ) )
247234 }
248235
249236 #[ cfg( feature = "internal" ) ]
@@ -252,52 +239,6 @@ impl FeatureProbe {
252239 }
253240}
254241
255- #[ derive( Debug , Default , Clone ) ]
256- pub struct FPConfig {
257- pub remote_url : String ,
258- pub toggles_url : Option < String > ,
259- pub events_url : Option < String > ,
260- pub server_sdk_key : String ,
261- pub refresh_interval : Duration ,
262- #[ cfg( feature = "use_tokio" ) ]
263- pub http_client : Option < Client > ,
264- pub start_wait : Option < Duration > ,
265- }
266-
267- #[ derive( Debug , Default , Clone ) ]
268- pub struct InnerConfig {
269- pub toggles_url : String ,
270- pub events_url : String ,
271- pub server_sdk_key : String ,
272- pub refresh_interval : Duration ,
273- #[ cfg( feature = "use_tokio" ) ]
274- pub http_client : Option < Client > ,
275- pub start_wait : Option < Duration > ,
276- }
277-
278- fn build_config ( mut config : FPConfig ) -> InnerConfig {
279- info ! ( "build_config from {:?}" , config) ;
280- if !config. remote_url . ends_with ( '/' ) {
281- config. remote_url += "/" ;
282- }
283- if config. toggles_url . is_none ( ) {
284- config. toggles_url = Some ( config. remote_url . clone ( ) + "api/server-sdk/toggles" )
285- }
286- if config. events_url . is_none ( ) {
287- config. events_url = Some ( config. remote_url + "api/events" )
288- }
289-
290- InnerConfig {
291- toggles_url : config. toggles_url . expect ( "not none" ) ,
292- events_url : config. events_url . expect ( "not none" ) ,
293- server_sdk_key : config. server_sdk_key ,
294- refresh_interval : config. refresh_interval ,
295- start_wait : config. start_wait ,
296- #[ cfg( feature = "use_tokio" ) ]
297- http_client : config. http_client ,
298- }
299- }
300-
301242#[ cfg( test) ]
302243mod tests {
303244 use serde_json:: json;
0 commit comments