Skip to content

Commit 6117105

Browse files
committed
comments
1 parent 3354381 commit 6117105

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

mill-rpc/examples/multi_service_server.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ impl math_service::Service for MathImpl {
5656
if n < 4 {
5757
return true;
5858
}
59+
60+
#[allow(clippy::incompatible_msrv)]
5961
if n.is_multiple_of(2) {
6062
return false;
6163
}
6264
let mut i = 3;
6365
while i * i <= n {
66+
#[allow(clippy::incompatible_msrv)]
6467
if n.is_multiple_of(i) {
6568
return false;
6669
}

mill-rpc/src/client.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct RpcClient {
3535
tcp_client: Mutex<TcpClient<RpcClientHandler>>,
3636
shared: Arc<ClientShared>,
3737
next_request_id: AtomicU64,
38-
timeout: Duration,
38+
timeout: AtomicU64,
3939
}
4040

4141
impl RpcClient {
@@ -62,13 +62,18 @@ impl RpcClient {
6262
tcp_client: Mutex::new(tcp_client),
6363
shared,
6464
next_request_id: AtomicU64::new(1),
65-
timeout: Duration::from_secs(30),
65+
timeout: AtomicU64::new(30 * 1000),
6666
}))
6767
}
6868

6969
/// Set the default timeout for RPC calls.
70-
pub fn set_timeout(&mut self, timeout: Duration) {
71-
self.timeout = timeout;
70+
pub fn set_timeout(&self, timeout: Duration) {
71+
self.timeout
72+
.store(timeout.as_millis() as u64, Ordering::SeqCst);
73+
}
74+
75+
fn timeout(&self) -> Duration {
76+
Duration::from_millis(self.timeout.load(Ordering::SeqCst))
7277
}
7378

7479
/// Send a request and wait for a response (blocking).
@@ -93,15 +98,18 @@ impl RpcClient {
9398
}
9499

95100
let frame = Frame::request(request_id, service_id, method_id, payload, false);
96-
{
101+
let send_result = {
97102
let client = self.tcp_client.lock().unwrap();
98-
client
99-
.send(&frame.encode())
100-
.map_err(|e| RpcError::unavailable(format!("Send failed: {}", e)))?;
103+
client.send(&frame.encode())
104+
};
105+
if let Err(e) = send_result {
106+
let mut pending = self.shared.pending.lock().unwrap();
107+
pending.remove(&request_id);
108+
return Err(RpcError::unavailable(format!("Send failed: {}", e)));
101109
}
102110

103111
let mut pending = self.shared.pending.lock().unwrap();
104-
let deadline = std::time::Instant::now() + self.timeout;
112+
let deadline = std::time::Instant::now() + self.timeout();
105113

106114
loop {
107115
if let Some(req) = pending.get(&request_id) {

0 commit comments

Comments
 (0)