Skip to content

Commit 59c2e3a

Browse files
author
Hui Zhu
authored
Merge pull request #261 from jokemanfire/dev5
Context:optimize external interfaces
2 parents a958466 + b1e3524 commit 59c2e3a

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

example/async-client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async fn main() {
3838
println!(
3939
"Green Thread 1 - {} -> {:?} ended: {:?}",
4040
"health.check()",
41-
thc.check(context::with_timeout(20 * 1000 * 1000), &req)
41+
thc.check(context::with_duration(core::time::Duration::from_millis(20)), &req)
4242
.await,
4343
now.elapsed(),
4444
);

src/context.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//
55

66
use crate::proto::KeyValue;
7+
use core::time::Duration;
78
use std::collections::HashMap;
8-
99
#[derive(Clone, Default, Debug)]
1010
pub struct Context {
1111
pub metadata: HashMap<String, Vec<String>>,
@@ -18,6 +18,11 @@ pub fn with_timeout(i: i64) -> Context {
1818
..Default::default()
1919
}
2020
}
21+
///With_timeout is not friendly enough to external interfaces
22+
///Keep with_timeout for forward compatibility
23+
pub fn with_duration(du: Duration) -> Context {
24+
with_timeout(du.as_nanos() as i64)
25+
}
2126

2227
pub fn with_metadata(md: HashMap<String, Vec<String>>) -> Context {
2328
Context {
@@ -131,7 +136,7 @@ mod tests {
131136
assert_eq!(0, ctx.timeout_nano);
132137
assert_eq!(ctx.metadata.len(), 0);
133138

134-
let mut ctx = context::with_timeout(99);
139+
let mut ctx = context::with_duration(core::time::Duration::from_nanos(99));
135140
assert_eq!(99, ctx.timeout_nano);
136141
assert_eq!(ctx.metadata.len(), 0);
137142

0 commit comments

Comments
 (0)