diff --git a/src/imageproxy.rs b/src/imageproxy.rs index 0f4e6fa..22c4c68 100644 --- a/src/imageproxy.rs +++ b/src/imageproxy.rs @@ -73,11 +73,16 @@ type ChildFuture = Pin< Box, JoinError>>>, >; +struct ImageProxyInner { + sockfd: Mutex, + childwait: AsyncMutex, + protover: semver::Version, +} + /// Manage a child process proxy to fetch container images. +#[derive(Clone)] pub struct ImageProxy { - sockfd: Arc>, - childwait: Arc>, - protover: semver::Version, + inner: Arc, } impl std::fmt::Debug for ImageProxy { @@ -212,16 +217,18 @@ impl ImageProxy { // which may also be in process. // xref https://github.com/tokio-rs/tokio/issues/3520#issuecomment-968985861 let childwait = tokio::task::spawn_blocking(move || child.wait_with_output()); - let sockfd = Arc::new(Mutex::new(mysock)); + let sockfd = Mutex::new(mysock); - let mut r = Self { + let mut inner = Arc::new(ImageProxyInner { sockfd, - childwait: Arc::new(AsyncMutex::new(Box::pin(childwait))), + childwait: AsyncMutex::new(Box::pin(childwait)), protover: semver::Version::new(0, 0, 0), - }; + }); // Verify semantic version - let protover = r.impl_request::("Initialize", []).await?.0; + let protover = Self::impl_request::(Arc::clone(&inner), "Initialize", []) + .await? + .0; let protover = semver::Version::parse(protover.as_str())?; // Previously we had a feature to opt-in to requiring newer versions using `if cfg!()`. let supported = &*BASE_PROTO_VERSION; @@ -232,13 +239,12 @@ impl ImageProxy { supported )); } - r.protover = protover; - - Ok(r) + Arc::::get_mut(&mut inner).unwrap().protover = protover; + Ok(Self { inner }) } async fn impl_request_raw( - sockfd: Arc>, + sockfd: qg&Mutex, req: Request, ) -> Result<(T, Option<(File, u32)>)> { tracing::trace!("sending request {}", req.method.as_str()); @@ -291,9 +297,9 @@ impl ImageProxy { Ok(r) } - #[instrument(skip(args))] + #[instrument(skip(inner, args))] async fn impl_request( - &self, + inner: Arc, method: &str, args: T, ) -> Result<(R, Option<(File, u32)>)> @@ -301,8 +307,8 @@ impl ImageProxy { T: IntoIterator, I: Into, { - let req = Self::impl_request_raw(Arc::clone(&self.sockfd), Request::new(method, args)); - let mut childwait = self.childwait.lock().await; + let req = Self::impl_request_raw(&inner.sockfd, Request::new(method, args)); + let mut childwait = inner.childwait.lock().await; tokio::select! { r = req => { Ok(r?)