Skip to content

Commit

Permalink
fea: add "rt_glommio" (#252)
Browse files Browse the repository at this point in the history
* @2024-08-23 02:45+9:00

* next: update docs

* Add benches_glommio

* refactor around feature flags

* not DEBUG

* fix to pass CI
  • Loading branch information
kanarus authored Aug 23, 2024
1 parent 3c903d4 commit fa62a1b
Show file tree
Hide file tree
Showing 28 changed files with 314 additions and 178 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ resolver = "2"
members = [
"ohkami",
"ohkami_lib",
"ohkami_macros"
"ohkami_macros",
]
exclude = [
"benches",
"benches_glommio",
]

[workspace.dependencies]
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ Hello, your_name!

## Feature flags

### `"rt_tokio"`, `"rt_async-std"`
### `"rt_tokio"`, `"rt_async-std"`, `"rt_glommio"`:native async runtime

Select a native async runtime
- [tokio](https://github.com/tokio-rs/tokio)
- [async-std](https://github.com/async-rs/async-std)
- [glommio](https://github.com/DataDog/glommio)

### `"rt_worker"`:Cloudflare Workers

Expand Down
28 changes: 25 additions & 3 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ tasks:
- test_no_rt
- test_rt_tokio
- test_rt_async-std
- test_rt_glommio
- test_rt_worker

check:
deps:
- check_no_rt
- check_rt_tokio
- check_rt_async-std
- check_rt_glommio
- check_rt_worker

bench_dryrun:
dir: benches
cmds:
- cargo bench --features DEBUG --no-run
- cargo check --bin hello
- cd benches && cargo bench --features DEBUG --no-run
- cd benches && cargo check
- cd benches_glommio && cargo check

bench:
dir: benches
Expand Down Expand Up @@ -73,6 +75,15 @@ tasks:
- cargo test --lib --features rt_async-std,DEBUG,{{.MAYBE_NIGHTLY}}
- cargo test --lib --features rt_async-std,DEBUG,sse,ws,{{.MAYBE_NIGHTLY}}

test_rt_glommio:
vars:
MAYBE_NIGHTLY:
sh: cargo version | grep -q 'nightly' && echo 'nightly' || echo ''
dir: ohkami
cmds:
- cargo test --lib --features rt_glommio,DEBUG,{{.MAYBE_NIGHTLY}}
- cargo test --lib --features rt_glommio,DEBUG,sse,ws,{{.MAYBE_NIGHTLY}}

test_rt_worker:
vars:
MAYBE_NIGHTLY:
Expand Down Expand Up @@ -120,6 +131,17 @@ tasks:
- cargo check --lib --features rt_async-std,sse,{{.MAYBE_NIGHTLY}}
- cargo check --lib --features rt_async-std,sse,ws,{{.MAYBE_NIGHTLY}}

check_rt_glommio:
vars:
MAYBE_NIGHTLY:
sh: cargo version | grep -q 'nightly' && echo 'nightly' || echo ''
dir: ohkami
cmds:
- cargo check --lib --features rt_glommio,{{.MAYBE_NIGHTLY}}
- cargo check --lib --features rt_glommio,ip,{{.MAYBE_NIGHTLY}}
- cargo check --lib --features rt_glommio,sse,{{.MAYBE_NIGHTLY}}
- cargo check --lib --features rt_glommio,sse,ws,{{.MAYBE_NIGHTLY}}

check_rt_worker:
vars:
MAYBE_NIGHTLY:
Expand Down
2 changes: 1 addition & 1 deletion benches/src/bin/hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ohkami::prelude::*;
use ohkami::{serde::*, format::JSON};
use ohkami::format::JSON;

#[cfg(feature="DEBUG")]
#[derive(Clone)]
Expand Down
10 changes: 10 additions & 0 deletions benches_glommio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "ohkami_benches-with-glommio"
version = "0.0.0"
edition = "2021"
authors = ["kanarus <[email protected]>"]

[dependencies]
# set `default-features = false` to assure "DEBUG" feature be off even when DEBUGing `../ohkami`
ohkami = { path = "../ohkami", default-features = false, features = ["rt_glommio"] }
glommio = { version = "0.9" }
20 changes: 20 additions & 0 deletions benches_glommio/src/bin/param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use ohkami::prelude::*;
use ohkami::utils::num_cpus;
use glommio::{LocalExecutorPoolBuilder, PoolPlacement, CpuSet};


#[inline(always)]
async fn echo_id(id: String) -> String {
id
}

fn main() {
LocalExecutorPoolBuilder::new(PoolPlacement::MaxSpread(
dbg!(num_cpus::get()), dbg!(CpuSet::online().ok())
)).on_all_shards(|| {
Ohkami::new((
"/user/:id"
.GET(echo_id),
)).howl("0.0.0.0:3000")
}).unwrap().join_all();
}
16 changes: 13 additions & 3 deletions ohkami/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ohkami_macros = { version = "=0.8.0", path = "../ohkami_macros" }

tokio = { version = "1", optional = true, features = ["net", "rt", "io-util", "sync", "time"] }
async-std = { version = "1", optional = true }
glommio = { version = "0.9", optional = true }
worker = { version = "0.3", optional = true }

byte_reader = { workspace = true }
Expand All @@ -35,13 +36,17 @@ hmac = { version = "0.12", default-features = false }
sha2 = { version = "0.10", default-features = false }
sha1 = { version = "0.10", optional = true, default-features = false }

num_cpus = { version = "1.16", optional = true }
futures-util = { version = "0.3", optional = true, default-features = false, features = ["io"] }


[features]
default = ["testing"]

rt_tokio = ["dep:tokio"]
rt_async-std = ["dep:async-std"]
rt_worker = ["dep:worker", "ohkami_macros/worker"]
rt_tokio = ["__rt__", "__rt_native__", "dep:tokio"]
rt_async-std = ["__rt__", "__rt_native__", "dep:async-std"]
rt_glommio = ["__rt__", "__rt_native__", "dep:glommio", "dep:num_cpus", "dep:futures-util"]
rt_worker = ["__rt__", "dep:worker", "ohkami_macros/worker"]

nightly = []
testing = []
Expand All @@ -50,6 +55,10 @@ ws = ["dep:sha1"]
graceful = ["rt_tokio", "tokio/signal", "tokio/macros"]
ip = []

##### internal #####
__rt__ = []
__rt_native__ = []

##### DEBUG #####
DEBUG = [
"tokio?/macros",
Expand All @@ -64,6 +73,7 @@ DEBUG = [
# "ip",
# "rt_tokio",
# #"rt_async-std",
# #"rt_glommio",
# #"rt_worker",
# "DEBUG",
#]
4 changes: 2 additions & 2 deletions ohkami/src/fang/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use cors::CORS;
mod jwt;
pub use jwt::{JWT, JWTToken};

#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#[cfg(feature="__rt_native__")]
mod timeout;
#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#[cfg(feature="__rt_native__")]
pub use timeout::Timeout;
2 changes: 1 addition & 1 deletion ohkami/src/fang/builtin/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#![cfg(feature="__rt_native__")]

use std::time::Duration;

Expand Down
4 changes: 2 additions & 2 deletions ohkami/src/fang/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(any(feature="rt_tokio",feature="rt_async-std",feature="rt_worker"))]
#[cfg(any(feature="__rt_native__",feature="rt_worker"))]
mod handler;
#[cfg(any(feature="rt_tokio",feature="rt_async-std",feature="rt_worker"))]
#[cfg(any(feature="__rt_native__",feature="rt_worker"))]
pub(crate) use handler::{Handler, IntoHandler};

mod middleware;
Expand Down
2 changes: 1 addition & 1 deletion ohkami/src/header/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<const N: usize, Value> IndexMap<N, Value> {
}
}

#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#[allow(unused)]
#[inline]
pub(crate) fn clear(&mut self) {
for idx in &mut self.index {*idx = Self::NULL}
Expand Down
42 changes: 33 additions & 9 deletions ohkami/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@

#[cfg(any(
all(feature="rt_tokio", feature="rt_async-std"),
all(feature="rt_async-std", feature="rt_worker"),
all(feature="rt_async-std", feature="rt_glommio"),
all(feature="rt_glommio", feature="rt_worker"),
all(feature="rt_worker", feature="rt_tokio"),
all(feature="rt_tokio", feature="rt_glommio"),
all(feature="rt_async-std", feature="rt_worker"),
))] compile_error! {"
Can't activate multiple `rt_*` features at once!
"}
Expand Down Expand Up @@ -67,26 +70,36 @@ mod __rt__ {
pub(crate) use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
#[cfg(feature="rt_async-std")]
pub(crate) use async_std::net::{TcpListener, TcpStream, ToSocketAddrs};
#[cfg(feature="rt_glommio")]
pub(crate) use {glommio::net::{TcpListener, TcpStream}, std::net::ToSocketAddrs};

#[cfg(feature="rt_tokio")]
pub(crate) use tokio::task;
#[cfg(feature="rt_async-std")]
pub(crate) use async_std::task;
#[cfg(feature="rt_glommio")]
pub(crate) use glommio::task;

#[cfg(feature="rt_tokio")]
pub(crate) use tokio::time::sleep;
#[cfg(feature="rt_async-std")]
pub(crate) use async_std::task::sleep;
#[cfg(feature="rt_glommio")]
pub(crate) use glommio::timer::sleep;

#[cfg(feature="rt_tokio")]
pub(crate) use tokio::io::AsyncReadExt as AsyncReader;
#[cfg(feature="rt_async-std")]
pub(crate) use async_std::io::ReadExt as AsyncReader;
#[cfg(feature="rt_glommio")]
pub(crate) use futures_util::AsyncReadExt as AsyncReader;

#[cfg(feature="rt_tokio")]
pub(crate) use tokio::io::AsyncWriteExt as AsyncWriter;
#[cfg(feature="rt_async-std")]
pub(crate) use async_std::io::WriteExt as AsyncWriter;
#[cfg(feature="rt_glommio")]
pub(crate) use futures_util::AsyncWriteExt as AsyncWriter;
}


Expand All @@ -102,23 +115,25 @@ pub use fang::{Fang, FangProc};

pub mod format;

#[cfg(feature="__rt_native__")]
mod session;
#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#[cfg(feature="__rt_native__")]
use session::Session;

#[cfg(feature="__rt__")]
mod ohkami;
#[cfg(any(feature="rt_tokio",feature="rt_async-std",feature="rt_worker"))]
#[cfg(feature="__rt__")]
pub use ohkami::{Ohkami, Route};

pub mod header;

pub mod typed;

#[cfg(all(feature="ws", any(feature="rt_tokio",feature="rt_async-std")))]
#[cfg(all(feature="ws", feature="__rt_native__"))]
pub mod ws;

#[cfg(feature="testing")]
#[cfg(any(feature="rt_tokio",feature="rt_async-std",feature="rt_worker"))]
#[cfg(feature="__rt__")]
pub mod testing;

pub mod utils {
Expand Down Expand Up @@ -207,7 +222,7 @@ pub mod utils {
}
};

#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#[cfg(feature="__rt_native__")]
pub fn timeout_in<T>(
duration: std::time::Duration,
proc: impl std::future::Future<Output = T>
Expand All @@ -234,11 +249,20 @@ pub mod utils {
}
}

#[cfg(feature="rt_glommio")]
/* for fang::builtin::timeout::Timeout::Proc::bite to return Send Future */
/* SAFETY: proc and sleep are executed on the same thread in rt_glommio */
/* ( glommio::timer::sleep itself returns not-Send Future because it's not needed due to the architecture ) */
unsafe impl<Sleep, Proc> Send for Timeout<Sleep, Proc> {}

Timeout { proc, sleep: crate::__rt__::sleep(duration) }
}

#[cfg(feature="ip")]
pub(crate) const IP_0000: std::net::IpAddr = std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0));
pub const IP_0000: std::net::IpAddr = std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0));

#[cfg(feature="rt_glommio")]
pub use num_cpus;
}

#[cfg(feature="rt_worker")]
Expand All @@ -250,7 +274,7 @@ pub mod prelude {
pub use crate::serde::{Serialize, Deserialize};
pub use crate::format::JSON;

#[cfg(any(feature="rt_tokio",feature="rt_async-std",feature="rt_worker"))]
#[cfg(feature="__rt__")]
pub use crate::{Route, Ohkami};
}

Expand Down Expand Up @@ -296,7 +320,7 @@ pub mod __internal__ {

/* for benchmarks */
#[cfg(feature="DEBUG")]
#[cfg(any(feature="rt_tokio",feature="rt_async-std",feature="rt_worker"))]
#[cfg(feature="__rt__")]
pub use crate::{
request::{RequestHeader, RequestHeaders},
response::{ResponseHeader, ResponseHeaders},
Expand Down
Loading

0 comments on commit fa62a1b

Please sign in to comment.