-
Notifications
You must be signed in to change notification settings - Fork 224
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
[WIP] IPC #31
Closed
Closed
[WIP] IPC #31
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "alloy-transport-ipc" | ||
version.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
exclude.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
alloy-transport.workspace = true | ||
alloy-pubsub.workspace = true | ||
|
||
|
||
tokio = { workspace = true, features = ["io-util", "sync", "rt"] } | ||
winapi = "0.3.9" | ||
tracing.workspace = true | ||
serde_json.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# alloy-transport-ipc | ||
|
||
IPC transport implementation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// #![doc = include_str!("../README.md")] | ||
// #![doc( | ||
// html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg", | ||
// html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico" | ||
// )] | ||
// #![warn( | ||
// missing_copy_implementations, | ||
// missing_debug_implementations, | ||
// missing_docs, | ||
// unreachable_pub, | ||
// clippy::missing_const_for_fn, | ||
// rustdoc::all | ||
// )] | ||
// #![cfg_attr(not(test), warn(unused_crate_dependencies))] | ||
// #![deny(unused_must_use, rust_2018_idioms)] | ||
// #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] | ||
|
||
#[cfg(unix)] | ||
mod unix; | ||
|
||
#[cfg(windows)] | ||
mod windows; | ||
|
||
use alloy_pubsub::ConnectionInterface; | ||
|
||
/// IPC Connection details | ||
pub struct IpcConnect { | ||
path: std::path::PathBuf, | ||
} | ||
|
||
/// An ongoing IPC connection to a backend. | ||
pub struct IpcBackend<T> { | ||
/// The IPC socket connection. For windows this is a named pipe, for unix | ||
/// it is a unix socket. | ||
pub(crate) stream: T, | ||
|
||
/// The interface to the connection. | ||
pub(crate) interface: ConnectionInterface, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use crate::{IpcBackend, IpcConnect}; | ||
|
||
use alloy_pubsub::PubSubConnect; | ||
use alloy_transport::{TransportError, TransportErrorKind}; | ||
use serde_json::value::RawValue; | ||
use std::time::Duration; | ||
use tokio::{net::UnixStream, time::sleep}; | ||
use tracing::error; | ||
|
||
const KEEPALIVE: u64 = 10; | ||
|
||
impl PubSubConnect for IpcConnect { | ||
fn is_local(&self) -> bool { | ||
true | ||
} | ||
|
||
fn connect<'a: 'b, 'b>( | ||
&'a self, | ||
) -> alloy_transport::Pbf<'b, alloy_pubsub::ConnectionHandle, TransportError> { | ||
Box::pin(async move { | ||
let stream = | ||
UnixStream::connect(&self.path).await.map_err(TransportErrorKind::custom)?; | ||
|
||
let (handle, interface) = alloy_pubsub::ConnectionHandle::new(); | ||
let backend = IpcBackend { stream, interface }; | ||
|
||
backend.spawn(); | ||
|
||
Ok(handle) | ||
}) | ||
} | ||
} | ||
|
||
impl IpcBackend<UnixStream> { | ||
/// Handle a message from the server. | ||
pub async fn handle(&mut self, msg: Box<RawValue>) -> Result<(), ()> { | ||
todo!() | ||
} | ||
|
||
/// Send a message to the server. | ||
pub async fn send(&mut self, msg: Box<RawValue>) -> Result<(), ()> { | ||
todo!() | ||
} | ||
|
||
fn spawn(self) { | ||
tokio::spawn(async move { | ||
let mut err = false; | ||
let keepalive = sleep(Duration::from_secs(KEEPALIVE)); | ||
|
||
loop { | ||
// We bias the loop as follows | ||
// 1. New dispatch. | ||
// 2. Keepalive. | ||
// 3. Response or notification from server. | ||
// This ensures that keepalive is sent only if no other messages | ||
// have been sent in the last 10 seconds. And prioritizes new | ||
// dispatches over responses from the server. This will fail if | ||
// the client saturates the task with dispatches, but that's | ||
// probably not a big deal. | ||
tokio::select! { | ||
biased; | ||
// we've received a new dispatch, so we send it via | ||
// websocket. We handle new work before processing any | ||
// responses from the server. | ||
inst = self.interface.recv_from_frontend() => { | ||
match inst { | ||
Some(msg) => { | ||
// Reset the keepalive timer. | ||
keepalive.set(sleep(Duration::from_secs(KEEPALIVE))); | ||
if let Err(e) = self.send(msg).await { | ||
error!(err = %e, "WS connection error"); | ||
err = true; | ||
break | ||
} | ||
}, | ||
// dispatcher has gone away, or shutdown was received | ||
None => { | ||
break | ||
}, | ||
} | ||
}, | ||
// Send a ping to the server, if no other messages have been | ||
// sent in the last 10 seconds. | ||
_ = &mut keepalive => { | ||
// Reset the keepalive timer. | ||
keepalive.set(sleep(Duration::from_secs(KEEPALIVE))); | ||
if let Err(e) = self.stream.send(Message::Ping(vec![])).await { | ||
error!(err = %e, "WS connection error"); | ||
err = true; | ||
break | ||
} | ||
} | ||
resp = self.stream.next() => { | ||
match resp { | ||
Some(Ok(item)) => { | ||
err = self.handle(item).await.is_err(); | ||
if err { break } | ||
}, | ||
Some(Err(e)) => { | ||
tracing::error!(err = %e, "WS connection error"); | ||
err = true; | ||
break | ||
} | ||
None => { | ||
error!("WS server has gone away"); | ||
err = true; | ||
break | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
if err { | ||
self.interface.close_with_error(); | ||
} | ||
}); | ||
} | ||
} |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.rs/tokio/latest/tokio/macro.select.html#fairness TIL