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

Change with_tokio_rt to accept Arc<Runtime>. #599

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions actix-rt/examples/multi_thread_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
.worker_threads(2)
.enable_all()
.build()
.map(std::sync::Arc::new)
.unwrap()
})
.block_on(async_main());
Expand Down
2 changes: 1 addition & 1 deletion actix-rt/src/arbiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Arbiter {
#[cfg(not(all(target_os = "linux", feature = "io-uring")))]
pub fn with_tokio_rt<F>(runtime_factory: F) -> Arbiter
where
F: FnOnce() -> tokio::runtime::Runtime + Send + 'static,
F: FnOnce() -> std::sync::Arc<tokio::runtime::Runtime> + Send + 'static,
{
let sys = System::current();
let system_id = sys.id();
Expand Down
18 changes: 13 additions & 5 deletions actix-rt/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{future::Future, io};

use std::{sync::Arc, future::Future, io};
use tokio::task::{JoinHandle, LocalSet};

/// A Tokio-based runtime proxy.
Expand All @@ -9,14 +8,14 @@ use tokio::task::{JoinHandle, LocalSet};
#[derive(Debug)]
pub struct Runtime {
local: LocalSet,
rt: tokio::runtime::Runtime,
rt: Arc<tokio::runtime::Runtime>,
}

pub(crate) fn default_tokio_runtime() -> io::Result<tokio::runtime::Runtime> {
pub(crate) fn default_tokio_runtime() -> io::Result<Arc<tokio::runtime::Runtime>> {
tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.build().map(Arc::new)
}

impl Runtime {
Expand Down Expand Up @@ -141,6 +140,15 @@ impl Runtime {

impl From<tokio::runtime::Runtime> for Runtime {
fn from(rt: tokio::runtime::Runtime) -> Self {
Self {
local: LocalSet::new(),
rt: Arc::new(rt),
}
}
}

impl From<Arc<tokio::runtime::Runtime>> for Runtime {
fn from(rt: Arc<tokio::runtime::Runtime>) -> Self {
Self {
local: LocalSet::new(),
rt,
Expand Down
6 changes: 3 additions & 3 deletions actix-rt/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::{
io,
pin::Pin,
sync::atomic::{AtomicUsize, Ordering},
sync::Arc,
task::{Context, Poll},
};

use futures_core::ready;
use tokio::sync::{mpsc, oneshot};

Expand Down Expand Up @@ -48,7 +48,7 @@ impl System {
/// [tokio-runtime]: tokio::runtime::Runtime
pub fn with_tokio_rt<F>(runtime_factory: F) -> SystemRunner
where
F: FnOnce() -> tokio::runtime::Runtime,
F: FnOnce() -> Arc<tokio::runtime::Runtime>,
{
let (stop_tx, stop_rx) = oneshot::channel();
let (sys_tx, sys_rx) = mpsc::unbounded_channel();
Expand Down Expand Up @@ -87,7 +87,7 @@ impl System {
#[doc(hidden)]
pub fn with_tokio_rt<F>(_: F) -> SystemRunner
where
F: FnOnce() -> tokio::runtime::Runtime,
F: FnOnce() -> Arc<tokio::runtime::Runtime>,
{
unimplemented!("System::with_tokio_rt is not implemented for io-uring feature yet")
}
Expand Down
4 changes: 3 additions & 1 deletion actix-rt/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use actix_rt::{task::JoinError, Arbiter, System};
#[cfg(not(feature = "io-uring"))]
use {
std::{sync::mpsc::channel, thread},
std::{sync::Arc, sync::mpsc::channel, thread},
tokio::sync::oneshot,
};

Expand Down Expand Up @@ -250,6 +250,7 @@ fn new_system_with_tokio() {
.on_thread_start(|| {})
.on_thread_stop(|| {})
.build()
.map(Arc::new)
.unwrap()
})
.block_on(async {
Expand Down Expand Up @@ -282,6 +283,7 @@ fn new_arbiter_with_tokio() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map(Arc::new)
.unwrap()
});

Expand Down
1 change: 1 addition & 0 deletions actix-server/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ impl ServerWorker {
.enable_all()
.max_blocking_threads(config.max_blocking_threads)
.build()
.map(Arc::new)
.unwrap()
})
};
Expand Down
Loading