Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: make proxy clonable #31

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/imageproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,16 @@ type ChildFuture = Pin<
Box<dyn Future<Output = std::result::Result<std::io::Result<std::process::Output>, JoinError>>>,
>;

struct ImageProxyInner {
sockfd: Mutex<File>,
childwait: AsyncMutex<ChildFuture>,
protover: semver::Version,
}

/// Manage a child process proxy to fetch container images.
#[derive(Clone)]
pub struct ImageProxy {
sockfd: Arc<Mutex<File>>,
childwait: Arc<AsyncMutex<ChildFuture>>,
protover: semver::Version,
inner: Arc<ImageProxyInner>,
}

impl std::fmt::Debug for ImageProxy {
Expand Down Expand Up @@ -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::<String, _, ()>("Initialize", []).await?.0;
let protover = Self::impl_request::<String, _, ()>(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;
Expand All @@ -232,13 +239,12 @@ impl ImageProxy {
supported
));
}
r.protover = protover;

Ok(r)
Arc::<ImageProxyInner>::get_mut(&mut inner).unwrap().protover = protover;
Ok(Self { inner })
}

async fn impl_request_raw<T: serde::de::DeserializeOwned + Send + 'static>(
sockfd: Arc<Mutex<File>>,
sockfd: qg&Mutex<File>,
req: Request,
) -> Result<(T, Option<(File, u32)>)> {
tracing::trace!("sending request {}", req.method.as_str());
Expand Down Expand Up @@ -291,18 +297,18 @@ impl ImageProxy {
Ok(r)
}

#[instrument(skip(args))]
#[instrument(skip(inner, args))]
async fn impl_request<R: serde::de::DeserializeOwned + Send + 'static, T, I>(
&self,
inner: Arc<ImageProxyInner>,
method: &str,
args: T,
) -> Result<(R, Option<(File, u32)>)>
where
T: IntoIterator<Item = I>,
I: Into<serde_json::Value>,
{
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?)
Expand Down