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

refactor!: modularize features and improve conditional compilation #112

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo build --verbose
- run: cargo build --features remote --verbose
- run: cargo build --no-default-features --verbose
- run: cargo build --no-default-features --features macros --verbose
- run: cargo build --no-default-features --features tracing --verbose
- run: cargo build --no-default-features --features remote --verbose
- run: RUSTFLAGS="--cfg tokio_unstable" cargo build --verbose
- run: cargo test --features remote --verbose
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ categories = ["asynchronous", "concurrency", "rust-patterns"]
keywords = ["actor", "tokio"]

[features]
remote = ["dep:libp2p", "dep:libp2p-identity", "dep:linkme", "dep:rmp-serde", "dep:internment"]
default = ["macros", "tracing"]
macros = ["dep:kameo_macros"]
remote = ["dep:internment", "dep:libp2p", "dep:libp2p-identity", "dep:linkme", "dep:once_cell", "dep:rmp-serde"]
tracing = ["dep:tracing", "tokio/tracing"]

[dependencies]
kameo_macros = { version = "0.13.0", path = "./macros" }
kameo_macros = { version = "0.13.0", path = "./macros", optional = true }

dyn-clone = "1.0"
futures = "0.3"
internment = { version = "0.8.5", features = ["serde"], optional = true }
libp2p = { version = "0.54.1", features = ["cbor", "dns", "kad", "mdns", "macros", "quic", "request-response", "rsa", "serde", "tokio"], optional = true }
libp2p-identity = { version = "0.2.9", features = ["rand", "rsa"], optional = true }
linkme = { version= "0.3.28", optional = true }
once_cell = "1.19"
once_cell = { version = "1.19", optional = true }
rmp-serde = { version = "1.3.0", optional = true }
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.37", features = ["macros", "rt", "sync", "time", "tracing"] }
tokio = { version = "1.37", features = ["macros", "rt", "sync", "time"] }
tokio-stream = "0.1"
tracing = "0.1"
tracing = { version = "0.1", optional = true }

[dev-dependencies]
criterion = { version = "0.5", features = ["async_tokio"] }
Expand Down
6 changes: 6 additions & 0 deletions src/actor/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tokio::{
sync::Semaphore,
task::JoinHandle,
};
#[cfg(feature = "tracing")]
use tracing::{error, trace};

use crate::{
Expand Down Expand Up @@ -285,6 +286,7 @@ where
{
let id = actor_ref.id();
let name = A::name();
#[cfg(feature = "tracing")]
trace!(%id, %name, "actor started");

let start_res = AssertUnwindSafe(actor.on_start(actor_ref.clone()))
Expand Down Expand Up @@ -413,6 +415,7 @@ where
}

#[inline]
#[cfg(feature = "tracing")]
fn log_actor_stop_reason(id: ActorID, name: &str, reason: &ActorStopReason) {
match reason {
reason @ ActorStopReason::Normal
Expand All @@ -425,3 +428,6 @@ fn log_actor_stop_reason(id: ActorID, name: &str, reason: &ActorStopReason) {
}
}
}

#[cfg(not(feature = "tracing"))]
fn log_actor_stop_reason(_id: ActorID, _name: &str, _reason: &ActorStopReason) {}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ pub mod reply;
pub mod request;

pub use actor::{spawn, Actor};
#[cfg(feature = "macros")]
pub use kameo_macros::{messages, remote_message, Actor, RemoteActor, Reply};
pub use reply::Reply;
2 changes: 2 additions & 0 deletions src/remote/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use tokio::sync::{mpsc, oneshot};
use tokio_stream::StreamExt;
#[cfg(feature = "tracing")]
use tracing::trace;

use crate::{
Expand Down Expand Up @@ -514,6 +515,7 @@ impl SwarmActor {

fn handle_event(&mut self, event: SwarmEvent<BehaviourEvent>) {
match event {
#[cfg(feature = "tracing")]
SwarmEvent::NewListenAddr { address, .. } => {
trace!("listening on {address:?}");
}
Expand Down
5 changes: 3 additions & 2 deletions src/request/ask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<'a, A, M, Tm, Tr> AskRequest<LocalAskRequest<'a, A, A::Mailbox>, A::Mailbox
where
A: Actor,
{
#[cfg(debug_assertions)]
#[cfg(all(debug_assertions, feature = "tracing"))]
fn warn_deadlock(&self, msg: &'static str) {
use tracing::warn;

Expand All @@ -112,6 +112,8 @@ where
_ => {}
}
}
#[cfg(not(all(debug_assertions, feature = "tracing")))]
fn warn_deadlock(&self, _msg: &'static str) {}
}

#[cfg(feature = "remote")]
Expand Down Expand Up @@ -258,7 +260,6 @@ macro_rules! impl_message_trait {

#[inline]
$($async)? fn $method(self) -> Result<Self::Ok, Self::Error> {
#[cfg(debug_assertions)]
self.warn_deadlock("An actor is sending an `ask` request to itself, which will likely lead to a deadlock. To avoid this, use a `tell` request instead.");

let $req = self;
Expand Down
7 changes: 4 additions & 3 deletions src/request/tell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<'a, A, M, T> TellRequest<LocalTellRequest<'a, A, A::Mailbox>, A::Mailbox, M
where
A: Actor,
{
#[cfg(debug_assertions)]
#[cfg(all(debug_assertions, feature = "tracing"))]
fn warn_deadlock(&self, msg: &'static str) {
use tracing::warn;

Expand All @@ -103,6 +103,9 @@ where
}
}
}

#[cfg(not(all(debug_assertions, feature = "tracing")))]
fn warn_deadlock(&self, _msg: &'static str) {}
}

#[cfg(feature = "remote")]
Expand Down Expand Up @@ -295,7 +298,6 @@ macro_rules! impl_message_trait {
// === MessageSend === //
/////////////////////////
impl_message_trait!(local, async => MessageSend::send, WithoutRequestTimeout, |req| {
#[cfg(debug_assertions)]
req.warn_deadlock("An actor is sending a `tell` request to itself using a bounded mailbox without a timeout, which may lead to a deadlock. To avoid this, use a timeout or switch to `.try_send()`.");

Ok(req.location
Expand Down Expand Up @@ -449,7 +451,6 @@ impl_message_trait!(local, => TryMessageSendSync::try_send_sync, WithoutRequestT
// === BlockingMessageSend === //
////////////////////////////////
impl_message_trait!(local, => BlockingMessageSend::blocking_send, WithoutRequestTimeout, |req| {
#[cfg(debug_assertions)]
req.warn_deadlock("An actor is sending a blocking `tell` request to itself using a bounded mailbox, which may lead to a deadlock.");

Ok(req.location.mailbox.blocking_send(req.location.signal)?)
Expand Down
Loading