Skip to content

Commit 97e3ee7

Browse files
committed
feat: close function to flush events
1 parent d95037f commit 97e3ee7

File tree

5 files changed

+46
-33
lines changed

5 files changed

+46
-33
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
edition = "2021"
33
name = "feature-probe-server-sdk"
4-
version = "1.1.0"
4+
version = "1.1.1"
55
license = "Apache-2.0"
66
authors = ["maintain@featureprobe.com"]
77
description = "FeatureProbe Server Side SDK for Rust"
@@ -43,8 +43,8 @@ thiserror = "1.0"
4343
tracing = "0.1"
4444
url = "2"
4545

46-
feature-probe-event-std = { optional = true, version = "1.0.4", package="feature-probe-event" }
47-
feature-probe-event-tokio = { optional = true, version = "1.0.4", features=["use_tokio"], default-features=false, package="feature-probe-event" }
46+
feature-probe-event-std = { optional = true, version = "1.1.2", package="feature-probe-event" }
47+
feature-probe-event-tokio = { optional = true, version = "1.1.2", features=["use_tokio"], default-features=false, package="feature-probe-event" }
4848

4949
reqwest = { optional = true, version = "0.11", default-features = false, features = ["rustls-tls", "json"] }
5050
tokio = {optional = true, version = "1", features = ["full"]}
@@ -59,5 +59,5 @@ criterion = "0.3"
5959
rusty-hook = "^0.11.2"
6060
tokio = { version = "1", features = ["full"] }
6161
tracing-subscriber = "0.3"
62-
feature-probe-server = "1.0.4"
62+
feature-probe-server = "1.2.1"
6363

examples/demo.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ use std::time::Duration;
66

77
#[tokio::main]
88
async fn main() {
9-
// let _ = tracing_subscriber::fmt()
10-
// .with_env_filter("feature_probe_server_sdk=trace")
11-
// .init();
12-
13-
let remote_url = "https://featureprobe.io/server";
149
// let remote_url = "http://localhost:4007"; // for local docker
10+
let remote_url = "https://featureprobe.io/server";
11+
// this key can fetch data, but can not change toggle
1512
let server_sdk_key = "server-8ed48815ef044428826787e9a238b9c6a479f98c";
13+
// let server_sdk_key = /* paste server key from project list for changing toggle */;
1614
let interval = Duration::from_millis(2000);
1715
let config = FPConfig {
1816
remote_url: remote_url.to_owned(),
@@ -32,20 +30,14 @@ async fn main() {
3230
}
3331
};
3432

35-
let user = FPUser::new("user_id").with("city", "Paris");
36-
let discount = fp.number_value("promotion_activity", &user, 9.0);
37-
println!("Result => discount for user in Paris is : {:?}", discount);
33+
let user = FPUser::new("unique user key");
34+
let enable = fp.bool_value("campaign_enable", &user, false);
35+
println!("Result => campaign_enable : {:?}", enable);
3836

39-
let detail = fp.number_detail("promotion_activity", &user, 9.0);
37+
let detail = fp.bool_detail("campaign_enable", &user, false);
38+
// println!(" => value : {:?}", detail.reason); // same as bool_value
4039
println!(" => reason : {:?}", detail.reason);
4140
println!(" => rule index : {:?}", detail.rule_index);
4241

43-
let user2 = FPUser::new("user_id").with("city", "New York");
44-
let discount2 = fp.number_value("promotion_activity", &user2, 9.0);
45-
println!(
46-
"Result => discount for user in New York is : {:?}",
47-
discount2
48-
);
49-
50-
tokio::time::sleep(interval).await;
42+
fp.close();
5143
}

src/feature_probe.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct FeatureProbe {
3535
#[cfg(any(feature = "event", feature = "event_tokio"))]
3636
event_recorder: Option<EventRecorder>,
3737
config: InnerConfig,
38+
should_stop: Arc<RwLock<bool>>,
3839
}
3940

4041
impl FeatureProbe {
@@ -115,9 +116,20 @@ impl FeatureProbe {
115116
syncer: None,
116117
#[cfg(any(feature = "event", feature = "event_tokio"))]
117118
event_recorder: None,
119+
should_stop: Arc::new(RwLock::new(false)),
118120
}
119121
}
120122

123+
pub fn close(&self) {
124+
if let Some(recorder) = &self.event_recorder {
125+
recorder.flush();
126+
}
127+
let mut should_stop = self.should_stop.write();
128+
*should_stop = true;
129+
130+
info!("featureprobe client closed");
131+
}
132+
121133
fn generic_detail<T: Default + Debug>(
122134
&self,
123135
toggle: &str,
@@ -202,7 +214,7 @@ impl FeatureProbe {
202214
self.config.http_client.clone(),
203215
repo,
204216
);
205-
syncer.sync(self.config.wait_first_resp);
217+
syncer.sync(self.config.wait_first_resp, self.should_stop.clone());
206218
self.syncer = Some(syncer);
207219
Ok(())
208220
}
@@ -216,12 +228,14 @@ impl FeatureProbe {
216228
};
217229
let flush_interval = self.config.refresh_interval;
218230
let auth = SdkAuthorization(self.config.server_sdk_key.clone()).encode();
231+
let should_stop = self.should_stop.clone();
219232
let event_recorder = EventRecorder::new(
220233
events_url,
221234
auth,
222235
(*crate::USER_AGENT).clone(),
223236
flush_interval,
224237
100,
238+
should_stop,
225239
);
226240
self.event_recorder = Some(event_recorder);
227241
Ok(())

src/sync.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Synchronizer {
4444
}
4545

4646
#[cfg(feature = "use_std")]
47-
pub fn sync(&self, wait_first_resp: bool) {
47+
pub fn sync(&self, wait_first_resp: bool, should_stop: Arc<RwLock<bool>>) {
4848
let inner = self.inner.clone();
4949
if wait_first_resp {
5050
inner.do_sync()
@@ -55,13 +55,16 @@ impl Synchronizer {
5555
}
5656
loop {
5757
inner.do_sync();
58+
if *should_stop.read() {
59+
break;
60+
}
5861
std::thread::sleep(inner.refresh_interval);
5962
}
6063
});
6164
}
6265

6366
#[cfg(feature = "use_tokio")]
64-
pub fn sync(&self, wait_first_resp: bool) {
67+
pub fn sync(&self, wait_first_resp: bool, should_stop: Arc<RwLock<bool>>) {
6568
use std::sync::mpsc::sync_channel;
6669
let inner = self.inner.clone();
6770
let client = match &self.inner.client {
@@ -79,6 +82,9 @@ impl Synchronizer {
7982
}
8083
loop {
8184
inner.do_sync(&client).await;
85+
if *should_stop.read() {
86+
break;
87+
}
8288
interval.tick().await;
8389
}
8490
});
@@ -170,7 +176,8 @@ mod tests {
170176
let port = 9009;
171177
setup_mock_api(port).await;
172178
let syncer = build_synchronizer(port);
173-
syncer.sync(true);
179+
let should_stop = Arc::new(RwLock::new(false));
180+
syncer.sync(true, should_stop);
174181

175182
let repo = syncer.repository();
176183
let repo = repo.read();

0 commit comments

Comments
 (0)