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

io_uring support #577

Merged
merged 177 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
177 commits
Select commit Hold shift + click to select a range
77c496b
Add a10::Ring to Coordinator
Thomasdezeeuw Mar 17, 2023
20e52a9
Add I/O uring to RuntimeInternals
Thomasdezeeuw Mar 18, 2023
9c19b28
Add I/O uring to shared runtime internals
Thomasdezeeuw Mar 18, 2023
7b5a503
Add submission_queue method to rt::Access
Thomasdezeeuw Mar 18, 2023
233d4e6
Add io module
Thomasdezeeuw Mar 18, 2023
88fe4ce
Add BufWrapper
Thomasdezeeuw Mar 18, 2023
d0ca2ff
Add wrappers around A10's I/O Futures
Thomasdezeeuw Mar 18, 2023
f0d190d
Register I/O uring in test Worker
Thomasdezeeuw Mar 18, 2023
98730e9
Ignore interrupts while polling epoll
Thomasdezeeuw Mar 18, 2023
2c162a3
Add WriteAll and WriteAllVectored Future wrappers
Thomasdezeeuw Mar 19, 2023
1eb8953
Fix doc link
Thomasdezeeuw Mar 19, 2023
06cea55
Update A10 version
Thomasdezeeuw Mar 20, 2023
114dc68
Add ReadN and ReadNVectored Futures
Thomasdezeeuw Mar 20, 2023
c3491fb
Add BufMutSlice::total_spare_capacity
Thomasdezeeuw Mar 20, 2023
b1863c8
Use I/O uring for the Unix pipe implementation
Thomasdezeeuw Mar 20, 2023
9f038e6
Fix incorrect token in registering shared I/O uring
Thomasdezeeuw Mar 20, 2023
ee75028
Fix some docs
Thomasdezeeuw Mar 21, 2023
48123dc
Update rustc version
Thomasdezeeuw Apr 6, 2023
3d231f2
Move (Un)Connected to net module
Thomasdezeeuw Apr 6, 2023
fde669f
Add UnixDatagram
Thomasdezeeuw Apr 6, 2023
1949531
Manually implement fmt::Debug for UnixDatagram
Thomasdezeeuw Apr 6, 2023
1249d55
Reexport (Un)Connected from net::uds
Thomasdezeeuw Apr 6, 2023
ff9806c
Take ownership of address in UnixDatagram::bind
Thomasdezeeuw Apr 6, 2023
af4d61a
Use net::UnixDatagram in systemd module
Thomasdezeeuw Apr 6, 2023
17f0a75
Expand UnixDatagram docs
Thomasdezeeuw Apr 7, 2023
3d1ed4a
Use io_uring for the UdpSocket
Thomasdezeeuw Apr 9, 2023
33463ac
Expand UnixDatagram API
Thomasdezeeuw Apr 9, 2023
e598359
Make Future types in net module private
Thomasdezeeuw Apr 9, 2023
cfac2ba
Use async function in pipe module
Thomasdezeeuw Apr 9, 2023
4b9b1bc
Make io Future types private
Thomasdezeeuw Apr 9, 2023
b527d4a
Remove notes about actor::Bound trait in pipe module
Thomasdezeeuw Apr 9, 2023
a60fe08
Make net::SockAddr private
Thomasdezeeuw Apr 9, 2023
56cc737
Use io_uring for TcpListener
Thomasdezeeuw Apr 9, 2023
bd3c31e
Reregister A10 ring with mio::Poll after event
Thomasdezeeuw Apr 9, 2023
c4c5461
Set CPU affinity in UnboundTcpStream
Thomasdezeeuw Apr 9, 2023
d13ce5f
Rewrite TcpServer as async function
Thomasdezeeuw Apr 9, 2023
784557d
Implement Buf for Arc<[u8]>
Thomasdezeeuw Apr 9, 2023
bf818e4
Change TcpStream to use io_uring
Thomasdezeeuw Apr 10, 2023
e2b922c
Update to socket2 v0.5
Thomasdezeeuw Apr 11, 2023
a7a1a40
Ignore the clipy::future_not_send lint
Thomasdezeeuw Apr 11, 2023
4d3aa5e
Fix Clippy lints
Thomasdezeeuw Apr 11, 2023
73c3b07
Add Safety sections to private buf traits
Thomasdezeeuw Apr 11, 2023
aa9bb51
Remove Cirrus CI setup
Thomasdezeeuw Apr 11, 2023
6ddd11b
Remove macOS from GitHub Actions
Thomasdezeeuw Apr 11, 2023
b99f06a
Update NewActor::map_arg example
Thomasdezeeuw Apr 11, 2023
fc0fa4b
Change method to be by reference on I/O types
Thomasdezeeuw Apr 11, 2023
57d7110
Update Heph-RT to v0.5
Thomasdezeeuw Apr 11, 2023
b822eed
Update remote crate to new heph_rt::net API
Thomasdezeeuw Apr 11, 2023
4ee68c2
Implement Spawn::spawn using try_spawn
Thomasdezeeuw Apr 11, 2023
36c20fe
Remove PrivateSpawn trait
Thomasdezeeuw Apr 11, 2023
3cdcef0
Remove ActorOptions.ready
Thomasdezeeuw Apr 11, 2023
4e0d412
Add size assertions for timer types
Thomasdezeeuw Apr 12, 2023
0aaf29a
Make timers lazy
Thomasdezeeuw Apr 12, 2023
0c5814c
Change shared timers to use task::Wakers
Thomasdezeeuw Apr 14, 2023
ad79c94
Change local timers to use task::Wakers
Thomasdezeeuw Apr 14, 2023
a78f3cd
Merge timers modules
Thomasdezeeuw Apr 14, 2023
d7d51e6
Derive Default for ActorOptions
Thomasdezeeuw Apr 14, 2023
40ea26b
Fix and ignore Clippy lints
Thomasdezeeuw Apr 14, 2023
54501b3
Remove actor::Bound trait
Thomasdezeeuw Apr 14, 2023
6a93beb
Add Limited buffer wrapper
Thomasdezeeuw Apr 14, 2023
05febaf
Remove the bytes module
Thomasdezeeuw Apr 14, 2023
3133ac2
Implement Buf for Box<[u8]>
Thomasdezeeuw Apr 14, 2023
42c9fad
Add io::{stdin, stdout, stderr}
Thomasdezeeuw Apr 14, 2023
56a5d11
Update the buffer tests
Thomasdezeeuw Apr 14, 2023
6155fd9
Remove unused methods from PrivateAccess trait
Thomasdezeeuw Apr 14, 2023
50d4bef
Couple of small cleanups in trace module
Thomasdezeeuw Apr 15, 2023
3cd7c25
Don't log panics using std-logger
Thomasdezeeuw Apr 15, 2023
15be122
Move coordinator into it's own directory
Thomasdezeeuw Apr 15, 2023
4d07ceb
Allow the coordinator to run Futures
Thomasdezeeuw Apr 15, 2023
6317c1a
Reuse io_uring kernel resources
Thomasdezeeuw Apr 16, 2023
3209300
Cleanup some error related documentation
Thomasdezeeuw Apr 16, 2023
3792af2
Don't use wildcard imports
Thomasdezeeuw Apr 18, 2023
7169555
Use ActorFuture for thread-safe actors
Thomasdezeeuw Apr 18, 2023
eb1e911
Use ActorFuture for thead-local actors
Thomasdezeeuw Apr 18, 2023
12e433a
Rewrite process module
Thomasdezeeuw Apr 18, 2023
8090541
Add ActorFuture::name
Thomasdezeeuw Apr 18, 2023
be83d01
Allow name to be called on unsized actors
Thomasdezeeuw Apr 18, 2023
9961b30
Rework adding process to shared scheduler
Thomasdezeeuw Apr 18, 2023
e430f31
Rework adding processes to local scheduler
Thomasdezeeuw Apr 18, 2023
b0d5cc6
Fuxp scheduler (related) tests
Thomasdezeeuw Apr 18, 2023
28a6b5a
Catch panics in FutureProcess
Thomasdezeeuw Apr 18, 2023
ed380f6
Update format_name
Thomasdezeeuw Apr 18, 2023
d6ca7e8
Fix unused code warning
Thomasdezeeuw Apr 18, 2023
e288679
Create scheduler module
Thomasdezeeuw Apr 18, 2023
b406cc6
Move shared Scheduler into scheduler module
Thomasdezeeuw Apr 18, 2023
bc99fd6
Fix a couple of Clippy lints
Thomasdezeeuw Apr 18, 2023
6bcafff
Ignore doc-markdown Clippy lint
Thomasdezeeuw Apr 18, 2023
8a7d21e
Add some tests for ActorFuture
Thomasdezeeuw Apr 18, 2023
062244f
Fix a number of internal doc links
Thomasdezeeuw Apr 19, 2023
4d13821
Improve the docs of the access and spawn modules
Thomasdezeeuw Apr 19, 2023
a1d309e
Some more doc improvements
Thomasdezeeuw Apr 19, 2023
d7fbe98
Remove Option from shared task::Waker implementation
Thomasdezeeuw Apr 19, 2023
bf1b1c6
Allow more runtimes during testing
Thomasdezeeuw Apr 19, 2023
cbc3d1d
Move shared::waker to waker::share module
Thomasdezeeuw Apr 19, 2023
7137be5
Change wakers implementation
Thomasdezeeuw Apr 19, 2023
0e8f1ea
Rename crate::waker to wakers
Thomasdezeeuw Apr 19, 2023
3dc89f5
Start Heph v0.5 developement
Thomasdezeeuw Apr 19, 2023
737e40e
Use Access trait directly
Thomasdezeeuw Apr 19, 2023
d5e01b1
Move coordinator waker implementation to waker module
Thomasdezeeuw Apr 20, 2023
9b2abe6
Move coordinator to src/coordinator.rs
Thomasdezeeuw Apr 20, 2023
3abedc5
Use io_uring based shutdown
Thomasdezeeuw Apr 20, 2023
e26a1ba
Update rustc version
Thomasdezeeuw Apr 21, 2023
457fafb
Fix bitmap based waker implementation
Thomasdezeeuw Apr 21, 2023
1e2af24
Update memory stress test example
Thomasdezeeuw Apr 21, 2023
519f4df
Don't use a different amount of MAX_RUNTIMES in testing
Thomasdezeeuw Apr 21, 2023
a088b8c
Use actor::Context pid as tracing substream id
Thomasdezeeuw Apr 24, 2023
d655788
Remove pid from Thread{Local,Safe}
Thomasdezeeuw Apr 24, 2023
2d7a737
Don't assume pid is equal to the pointer of the process
Thomasdezeeuw Apr 27, 2023
6677254
Add pid to actor::Context
Thomasdezeeuw Apr 27, 2023
93883b3
Simplify adding of processes
Thomasdezeeuw Apr 27, 2023
b674869
Fix some Clippy lints
Thomasdezeeuw Apr 27, 2023
6d0a637
Define a single assert_size function
Thomasdezeeuw Apr 27, 2023
682b223
Fix tracing example instructions
Thomasdezeeuw Apr 27, 2023
5b533c2
Add UnixAddr::as_pathname
Thomasdezeeuw Apr 27, 2023
1dc2605
Add UnixStream
Thomasdezeeuw Apr 27, 2023
3a4ee71
Add UnixListener
Thomasdezeeuw Apr 27, 2023
87ee750
Cleanup net::uds module and expand docs
Thomasdezeeuw Apr 27, 2023
b84a1a4
Fix example tests
Thomasdezeeuw Apr 27, 2023
3a29ac8
Enable running_actors test again
Thomasdezeeuw Apr 27, 2023
ab3cb71
Remove ptr_as_usize
Thomasdezeeuw Apr 27, 2023
2831b3b
Small improvement to Signal docs
Thomasdezeeuw Apr 27, 2023
0a05bab
Use upper case SAFETY convention
Thomasdezeeuw Apr 27, 2023
ffda172
Switch to use pub(crate) everywhere
Thomasdezeeuw Apr 27, 2023
52d52ac
Use SOMAXCONN in calls to listen
Thomasdezeeuw Apr 27, 2023
5ab3cc3
Use no-op task::Waker in test::poll_* functions
Thomasdezeeuw Apr 27, 2023
007c120
Don't expose test::runtime
Thomasdezeeuw Apr 27, 2023
bc72bd4
Simplify test::init_(local_)actor
Thomasdezeeuw Apr 27, 2023
a26c3df
Add TestAssertUnmovedNewActor to test module
Thomasdezeeuw Apr 27, 2023
60a9555
Update rustc version for heph-remote
Thomasdezeeuw Apr 28, 2023
6733c1f
Fix size in example
Thomasdezeeuw Apr 28, 2023
a56c79b
Reduce size of example
Thomasdezeeuw Apr 28, 2023
ec14e0c
Update A10
Thomasdezeeuw May 8, 2023
9a74c23
Don't drop copy types
Thomasdezeeuw May 14, 2023
e1941f8
Reduce a10::Ring sizes
Thomasdezeeuw May 14, 2023
465846d
Add BufMut::extend_from_slice
Thomasdezeeuw May 23, 2023
27ecdbe
Add BufMutSlice::extend_from_slice
Thomasdezeeuw May 24, 2023
7d50fbe
Add BufSlice::as_io_slices
Thomasdezeeuw May 24, 2023
631f0f7
Add BufSlice::limit
Thomasdezeeuw May 24, 2023
56868fe
Add BufMutSlice::limit
Thomasdezeeuw May 24, 2023
86b30a1
Implement Buf for Cow
Thomasdezeeuw May 24, 2023
2cec4e9
Update Heph-http
Thomasdezeeuw May 25, 2023
a169ce0
Impl From<DeadlinePassed> for {Request,Response}Error
Thomasdezeeuw May 25, 2023
4b4853d
Fix Heph-HTTP examples
Thomasdezeeuw May 25, 2023
635e9d2
Add test::block_on_local_actor
Thomasdezeeuw May 25, 2023
19e3b2e
Add test::block_on_actor
Thomasdezeeuw May 25, 2023
e60804e
Add fs module
Thomasdezeeuw May 26, 2023
b283d4d
Show panic message in fmt::Debug for BlockOnError
Thomasdezeeuw May 26, 2023
c3ac0ab
Ignore Clippy lint
Thomasdezeeuw May 26, 2023
9e0def8
Enable HTTP in Makefile
Thomasdezeeuw May 26, 2023
0199533
Fix a couple of warnings
Thomasdezeeuw May 26, 2023
0993086
Remove unused any attribute configuration
Thomasdezeeuw May 26, 2023
7dad410
Fix Clippy lints in HTTP crate
Thomasdezeeuw May 26, 2023
3ac2e5a
Don't allow single-match-else Clippy lint
Thomasdezeeuw May 26, 2023
e66f7bb
Fix Clippy manual-let-else warnings
Thomasdezeeuw May 26, 2023
c661be6
Mark various functions as const
Thomasdezeeuw May 26, 2023
86722ee
Remove double Arc from SyncWaker
Thomasdezeeuw May 29, 2023
1d0c82e
Rename test::block_on to block_on_future
Thomasdezeeuw May 29, 2023
51bda13
Update license years
Thomasdezeeuw May 29, 2023
cd2da65
Update head module based on RFC 9110
Thomasdezeeuw May 30, 2023
096a491
Fix HTTP tests block_on -> block_on_future
Thomasdezeeuw May 30, 2023
ae680e4
Derive Debug for {Request,Response}Head
Thomasdezeeuw May 30, 2023
b2fb82d
Derive Clone for {Request,Response}Head
Thomasdezeeuw May 30, 2023
2879fe5
Update known headers
Thomasdezeeuw May 30, 2023
de64f6e
Small doc fixes
Thomasdezeeuw May 31, 2023
1b3bc09
Implement NewActor for !
Thomasdezeeuw Jun 9, 2023
0f3b80a
Return BlockOnError for test::block_on_future
Thomasdezeeuw Jun 9, 2023
79376fc
Improve test module docs
Thomasdezeeuw Jun 10, 2023
4d8e504
Don't use init_actor in some tests
Thomasdezeeuw Jun 10, 2023
bea425f
Don't use BlockOnError for test::block_on_future
Thomasdezeeuw Jun 11, 2023
6d5aab2
Remove NewActor impl for !
Thomasdezeeuw Jun 11, 2023
4b0a2ae
Handle block_on_future errors in HTTP tests
Thomasdezeeuw Jun 18, 2023
b1e8d21
Update rustc version
Thomasdezeeuw Jun 24, 2023
c2f28e7
Update rustc and clippy version
Thomasdezeeuw Jul 1, 2023
c954e2c
Use A10 v0.1.0
Thomasdezeeuw Jul 10, 2023
92ee77d
Ignore arc_with_non_send_sync in ActorRef
Thomasdezeeuw Jul 10, 2023
ea5bf6e
Fix Clippy warnings
Thomasdezeeuw Jul 10, 2023
bd39579
Use function as TCP server supervisor in example
Thomasdezeeuw Jul 10, 2023
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
18 changes: 0 additions & 18 deletions .cirrus.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@nightly
Expand Down
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "heph"
description = "Heph is an actor framework based on asynchronous functions."
version = "0.4.0"
version = "0.5.0"
publish = false # In development.
authors = ["Thomas de Zeeuw <thomasdezeeuw@gmail.com>"]
license = "MIT"
documentation = "https://docs.rs/heph"
Expand All @@ -18,15 +19,15 @@ test = ["getrandom"]

[dependencies]
heph-inbox = { version = "0.2.3", path = "./inbox", default-features = false }
log = { version = "0.4.16", default-features = false, features = ["kv_unstable", "kv_unstable_std"] }
log = { version = "0.4.17", default-features = false, features = ["kv_unstable", "kv_unstable_std"] }

# Optional dependencies, enabled by features.
# Required by the `test` feature.
getrandom = { version = "0.2.2", default-features = false, features = ["std"], optional = true }

[dev-dependencies]
getrandom = { version = "0.2.2", default-features = false, features = ["std"] }
heph-rt = { version = "0.4.0", path = "./rt", default-features = false, features = ["test"] }
heph-rt = { version = "0.5.0", path = "./rt", default-features = false, features = ["test"] }
std-logger = { version = "0.5.0", default-features = false, features = ["log-panic", "nightly"] }

[[test]]
Expand All @@ -42,7 +43,7 @@ required-features = ["test"]

[workspace]
members = [
#"http", # Stuck on 2021-11-01, also enable in Makefile.
"http",
"inbox",
"remote",
"rt",
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (C) 2017-2022 Thomas de Zeeuw
Copyright (C) 2017-2023 Thomas de Zeeuw


Permission is hereby granted, free of charge, to any person obtaining a copy of
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include Makefile.include

# Crates in this repo.
CRATES := ./ inbox rt remote # http, stuck on old nightly.
CRATES := ./ inbox rt remote http
# Target that run the target in all $CRATES.
TARGETS := test_all test_sanitizers_all test_sanitizer_all check_all clippy_all

Expand Down
11 changes: 6 additions & 5 deletions Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ $(TARGETS):

# Reasons to allow lints:
# `cargo-common-metadata`: for `benches` and `tools`.
# `doc-markdown`: too many false positives.
# `equatable-if-let`: bad lint.
# `manual-let-else`: not really a fan of this.
# `match-bool`, `single-match-else`: often less lines of code and I find that
# use `match` generally strictly better then `if`s.
# `future-not-send`: we don't want to require all generic parameters to be `Send`.
# `match-bool`: often less lines of code and I find that use `match` generally
# strictly better then `if`s.
# `missing-const-for-fn`: See https://github.com/rust-lang/rust-clippy/issues/4979.
# `module-name-repetitions`: we re-export various names.
# `needless-lifetimes`: lifetime serves as documentation.
Expand All @@ -68,9 +69,10 @@ clippy:
--deny clippy::nursery \
--deny clippy::cargo \
--allow clippy::cargo-common-metadata \
--allow clippy::doc-markdown \
--allow clippy::enum-glob-use \
--allow clippy::equatable-if-let \
--allow clippy::manual-let-else \
--allow clippy::future-not-send \
--allow clippy::match-bool \
--allow clippy::missing-const-for-fn \
--allow clippy::missing-errors-doc \
Expand All @@ -81,7 +83,6 @@ clippy:
--allow clippy::option-if-let-else \
--allow clippy::redundant-pub-crate \
--allow clippy::significant-drop-tightening \
--allow clippy::single-match-else \
--allow clippy::use-self \

doc:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Second, Heph needs to be added as a dependency.

```toml
[dependencies]
heph = "0.4.0"
heph = "0.5.0"
```

Now, you're ready to starting writing your application! Next, you can look at
Expand Down
17 changes: 12 additions & 5 deletions http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "heph-http"
description = "Heph-HTTP is a HTTP library build on top of Heph."
version = "0.1.0"
publish = false # In development.
authors = ["Thomas de Zeeuw <thomasdezeeuw@gmail.com>"]
license = "MIT"
documentation = "https://docs.rs/heph-http"
Expand All @@ -13,15 +14,21 @@ include = ["/Cargo.toml", "/src/**/*.rs", "/README.md", "/LICENSE"]
edition = "2021"

[dependencies]
heph = { version = "0.4.0", path = "../", default-features = false, features = ["runtime"] }
httparse = { version = "1.5.1", default-features = false }
httpdate = { version = "1.0.0", default-features = false }
log = { version = "0.4.8", default-features = false }
itoa = { version = "0.4.7", default-features = false }
heph = { version = "0.5.0", default-features = false, path = "../" }
heph-rt = { version = "0.5.0", default-features = false, path = "../rt" }
httparse = { version = "1.8.0", default-features = false }
httpdate = { version = "1.0.2", default-features = false }
log = { version = "0.4.17", default-features = false }
itoa = { version = "1.0.6", default-features = false }

[dev-dependencies]
std-logger = { version = "0.5.0", default-features = false, features = ["log-panic", "nightly"] }

[dev-dependencies.heph]
path = "../"
features = ["test"]


[dev-dependencies.heph-rt]
path = "../rt"
features = ["test"]
2 changes: 1 addition & 1 deletion http/LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (C) 2021 Thomas de Zeeuw
Copyright (C) 2021-2023 Thomas de Zeeuw


Permission is hereby granted, free of charge, to any person obtaining a copy of
Expand Down
76 changes: 31 additions & 45 deletions http/examples/my_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@

use std::borrow::Cow;
use std::io;
use std::net::SocketAddr;
use std::time::Duration;

use heph::actor::{self, Actor, NewActor};
use heph::net::TcpStream;
use heph::rt::{self, Runtime, ThreadLocal};
use heph::supervisor::{Supervisor, SupervisorStrategy};
use heph::timer::Deadline;
use heph::actor;
use heph::supervisor::SupervisorStrategy;
use heph_http::body::OneshotBody;
use heph_http::{self as http, Header, HeaderName, Headers, HttpServer, Method, StatusCode};
use heph_http::{self as http, server, Header, HeaderName, Headers, Method, StatusCode};
use heph_rt::net::TcpStream;
use heph_rt::spawn::options::{ActorOptions, Priority};
use heph_rt::timer::Deadline;
use heph_rt::{Runtime, ThreadLocal};
use log::{debug, error, info, warn};

fn main() -> Result<(), rt::Error> {
fn main() -> Result<(), heph_rt::Error> {
std_logger::Config::logfmt().init();

let actor = http_actor as fn(_, _, _) -> _;
let actor = http_actor as fn(_, _) -> _;
let address = "127.0.0.1:7890".parse().unwrap();
let server = HttpServer::setup(address, conn_supervisor, actor, ActorOptions::default())
.map_err(rt::Error::setup)?;
let server = server::setup(address, conn_supervisor, actor, ActorOptions::default())
.map_err(heph_rt::Error::setup)?;

let mut runtime = Runtime::setup().use_all_cores().build()?;
runtime.run_on_workers(move |mut runtime_ref| -> io::Result<()> {
let options = ActorOptions::default().with_priority(Priority::LOW);
let server_ref = runtime_ref.try_spawn_local(ServerSupervisor, server, (), options)?;
let server_ref = runtime_ref.spawn_local(server_supervisor, server, (), options);

runtime_ref.receive_signals(server_ref.try_map());
Ok(())
Expand All @@ -35,37 +34,20 @@ fn main() -> Result<(), rt::Error> {
runtime.start()
}

/// Our supervisor for the TCP server.
#[derive(Copy, Clone, Debug)]
struct ServerSupervisor;

impl<NA> Supervisor<NA> for ServerSupervisor
where
NA: NewActor<Argument = (), Error = io::Error>,
NA::Actor: Actor<Error = http::server::Error<!>>,
{
fn decide(&mut self, err: http::server::Error<!>) -> SupervisorStrategy<()> {
use http::server::Error::*;
match err {
Accept(err) => {
error!("error accepting new connection: {err}");
SupervisorStrategy::Restart(())
}
NewActor(_) => unreachable!(),
fn server_supervisor(err: server::Error<!>) -> SupervisorStrategy<()> {
match err {
// When we hit an error accepting a connection we'll drop the old
// server and create a new one.
server::Error::Accept(err) => {
error!("error accepting new connection: {err}");
SupervisorStrategy::Restart(())
}
}

fn decide_on_restart_error(&mut self, err: io::Error) -> SupervisorStrategy<()> {
error!("error restarting the TCP server: {err}");
SupervisorStrategy::Stop
}

fn second_restart_error(&mut self, err: io::Error) {
error!("error restarting the actor a second time: {err}");
// Async function never return an error creating a new actor.
server::Error::NewActor(_) => unreachable!(),
}
}

fn conn_supervisor(err: io::Error) -> SupervisorStrategy<(TcpStream, SocketAddr)> {
fn conn_supervisor(err: io::Error) -> SupervisorStrategy<TcpStream> {
error!("error handling connection: {err}");
SupervisorStrategy::Stop
}
Expand All @@ -75,18 +57,22 @@ const ALIVE_TIMEOUT: Duration = Duration::from_secs(120);
const WRITE_TIMEOUT: Duration = Duration::from_secs(10);

async fn http_actor(
mut ctx: actor::Context<!, ThreadLocal>,
ctx: actor::Context<!, ThreadLocal>,
mut connection: http::Connection,
address: SocketAddr,
) -> io::Result<()> {
let address = connection.peer_addr()?;
info!("accepted connection: source={address}");
connection.set_nodelay(true)?;

let mut read_timeout = READ_TIMEOUT;
let mut headers = Headers::EMPTY;
loop {
let fut = Deadline::after(&mut ctx, read_timeout, connection.next_request());
let (code, body, should_close) = match fut.await? {
let fut = Deadline::after(
ctx.runtime_ref().clone(),
read_timeout,
connection.next_request(),
);
let (code, body, should_close) = match fut.await {
Ok(Some(request)) => {
info!("received request: {request:?}: source={address}");
if request.path() != "/" {
Expand Down Expand Up @@ -121,9 +107,9 @@ async fn http_actor(
}

debug!("sending response: code={code}, body='{body}', source={address}");
let body = OneshotBody::new(body.as_bytes());
let body = OneshotBody::new(body);
let write_response = connection.respond(code, &headers, body);
Deadline::after(&mut ctx, WRITE_TIMEOUT, write_response).await?;
Deadline::after(ctx.runtime_ref().clone(), WRITE_TIMEOUT, write_response).await?;

if should_close {
warn!("closing connection: source={address}");
Expand Down
Loading
Loading