From e4bc5e8e326d7b4376d9ccd178ec28914ca0de25 Mon Sep 17 00:00:00 2001 From: Michael Zimmermann Date: Wed, 20 Jan 2021 18:00:36 +0100 Subject: [PATCH] use AtomicUsize instead of AtomicU64 - it's more portable (some architectures like MIPS don't support AtomicU64) - for most 64bit architectures usize should be 64bit as well - for most users even 32bit would probably be enough because: - it's tied to the connection(for streaming sockets) - the ID wraps and by the time that happens, all previous requests would have timed out unless you send a lot of requests and have a ton of RAM --- tarpc/src/rpc/client/channel.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tarpc/src/rpc/client/channel.rs b/tarpc/src/rpc/client/channel.rs index 74e5594..27e3a03 100644 --- a/tarpc/src/rpc/client/channel.rs +++ b/tarpc/src/rpc/client/channel.rs @@ -21,14 +21,24 @@ use futures::{ use log::{debug, info, trace}; use pin_project::{pin_project, pinned_drop}; use std::{ + convert::TryFrom, io, pin::Pin, sync::{ - atomic::{AtomicU64, Ordering}, + atomic::{AtomicUsize, Ordering}, Arc, }, }; +#[allow(dead_code)] +#[allow(clippy::no_effect)] +const CHECK_USIZE: () = { + if std::mem::size_of::() > std::mem::size_of::() { + // TODO: replace this with panic!() as soon as RFC 2345 gets stabilized + ["usize is too big to fit in u64"][42]; + } +}; + use super::{Config, NewClient}; /// Handles communication from the client to request dispatch. @@ -38,7 +48,7 @@ pub struct Channel { /// Channel to send a cancel message to the dispatcher. cancellation: RequestCancellation, /// The ID to use for the next request to stage. - next_request_id: Arc, + next_request_id: Arc, } impl Clone for Channel { @@ -106,7 +116,8 @@ impl Channel { let (response_completion, response) = oneshot::channel(); let cancellation = self.cancellation.clone(); - let request_id = self.next_request_id.fetch_add(1, Ordering::Relaxed); + let request_id = + u64::try_from(self.next_request_id.fetch_add(1, Ordering::Relaxed)).unwrap(); Send { fut: MapOkDispatchResponse::new( MapErrConnectionReset::new(self.to_dispatch.send(DispatchRequest { @@ -211,7 +222,7 @@ where client: Channel { to_dispatch, cancellation, - next_request_id: Arc::new(AtomicU64::new(0)), + next_request_id: Arc::new(AtomicUsize::new(0)), }, dispatch: RequestDispatch { config, @@ -721,7 +732,7 @@ mod tests { prelude::*, task::*, }; - use std::{pin::Pin, sync::atomic::AtomicU64, sync::Arc}; + use std::{pin::Pin, sync::atomic::AtomicUsize, sync::Arc}; #[tokio::test] async fn dispatch_response_cancels_on_drop() { @@ -851,7 +862,7 @@ mod tests { let channel = Channel { to_dispatch, cancellation, - next_request_id: Arc::new(AtomicU64::new(0)), + next_request_id: Arc::new(AtomicUsize::new(0)), }; (dispatch, channel, server_channel)