actix-rt 2.16.0

Tokio-based single-threaded async runtime for the Actix ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::{
    cell::RefCell,
    fmt,
    future::Future,
    io,
    pin::Pin,
    sync::atomic::{AtomicUsize, Ordering},
    task::{Context, Poll},
    thread,
};

use futures_core::ready;
use tokio::sync::mpsc;

use crate::system::{System, SystemCommand};

pub(crate) static COUNT: AtomicUsize = AtomicUsize::new(0);

thread_local!(
    static HANDLE: RefCell<Option<ArbiterHandle>> = const { RefCell::new(None) };
);

pub(crate) enum ArbiterCommand {
    Stop,
    Execute(Pin<Box<dyn Future<Output = ()> + Send>>),
}

impl fmt::Debug for ArbiterCommand {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ArbiterCommand::Stop => write!(f, "ArbiterCommand::Stop"),
            ArbiterCommand::Execute(_) => write!(f, "ArbiterCommand::Execute"),
        }
    }
}

/// A handle for sending spawn and stop messages to an [Arbiter].
#[derive(Debug, Clone)]
pub struct ArbiterHandle {
    tx: mpsc::UnboundedSender<ArbiterCommand>,
}

impl ArbiterHandle {
    pub(crate) fn new(tx: mpsc::UnboundedSender<ArbiterCommand>) -> Self {
        Self { tx }
    }

    /// Send a future to the [Arbiter]'s thread and spawn it.
    ///
    /// If you require a result, include a response channel in the future.
    ///
    /// Returns true if future was sent successfully and false if the [Arbiter] has died.
    pub fn spawn<Fut>(&self, future: Fut) -> bool
    where
        Fut: Future<Output = ()> + Send + 'static,
    {
        self.tx
            .send(ArbiterCommand::Execute(Box::pin(future)))
            .is_ok()
    }

    /// Send a function to the [Arbiter]'s thread and execute it.
    ///
    /// Any result from the function is discarded. If you require a result, include a response
    /// channel in the function.
    ///
    /// Returns true if function was sent successfully and false if the [Arbiter] has died.
    pub fn spawn_fn<F>(&self, f: F) -> bool
    where
        F: FnOnce() + Send + 'static,
    {
        self.spawn(async { f() })
    }

    /// Returns true if the [Arbiter]'s command channel is still open.
    ///
    /// The Arbiter can stop after this check, so this does not guarantee that a subsequent spawn
    /// call will succeed.
    pub fn alive(&self) -> bool {
        !self.tx.is_closed()
    }

    /// Instruct [Arbiter] to stop processing it's event loop.
    ///
    /// Returns true if stop message was sent successfully and false if the [Arbiter] has
    /// been dropped.
    pub fn stop(&self) -> bool {
        self.tx.send(ArbiterCommand::Stop).is_ok()
    }
}

/// An Arbiter represents a thread that provides an asynchronous execution environment for futures
/// and functions.
///
/// When an arbiter is created, it spawns a new [OS thread](thread), and hosts an event loop.
#[derive(Debug)]
pub struct Arbiter {
    tx: mpsc::UnboundedSender<ArbiterCommand>,
    thread_handle: thread::JoinHandle<()>,
}

impl Arbiter {
    /// Create an [ArbiterBuilder] to configure and spawn a new Arbiter thread.
    pub fn builder() -> ArbiterBuilder {
        ArbiterBuilder::new()
    }

    /// Spawn a new Arbiter thread and start its event loop.
    ///
    /// # Panics
    /// Panics if a [System] is not registered on the current thread, or if creating the Arbiter's
    /// thread or Tokio runtime fails.
    #[allow(clippy::new_without_default)]
    pub fn new() -> Arbiter {
        Self::try_new().expect("Failed to create new Arbiter")
    }

    /// Try to spawn a new Arbiter thread and start its event loop with the default Tokio runtime.
    ///
    /// # Panics
    /// Panics if a [System] is not registered on the current thread.
    ///
    /// # Errors
    /// Returns an `io::Error` if creating the underlying OS thread or Tokio runtime fails.
    pub fn try_new() -> io::Result<Arbiter> {
        Self::try_with_tokio_rt(crate::runtime::default_tokio_runtime)
    }

    /// Spawn a new Arbiter using the [Tokio Runtime](tokio-runtime) returned from a closure.
    ///
    /// The closure may return any type that can be converted into [`Runtime`], such as
    /// `tokio::runtime::Runtime`, `Arc<tokio::runtime::Runtime>`, or
    /// `&'static tokio::runtime::Runtime`.
    ///
    /// # Panics
    /// Panics if a [System] is not registered on the current thread, or if creating the Arbiter's
    /// thread or Tokio runtime fails.
    ///
    /// [tokio-runtime]: tokio::runtime::Runtime
    /// [`Runtime`]: crate::Runtime
    pub fn with_tokio_rt<F, R>(runtime_factory: F) -> Arbiter
    where
        F: FnOnce() -> R + Send + 'static,
        R: Into<crate::runtime::Runtime> + Send + 'static,
    {
        Self::try_with_tokio_rt(|| Ok(runtime_factory())).expect("Failed to create new Arbiter")
    }

    /// Try to spawn a new Arbiter using the [Tokio Runtime](tokio-runtime) returned from a closure.
    ///
    /// The closure may return any `Result` whose success value can be converted into [`Runtime`],
    /// such as `tokio::runtime::Runtime`, `Arc<tokio::runtime::Runtime>`, or
    /// `&'static tokio::runtime::Runtime`.
    ///
    /// # Panics
    /// Panics if a [System] is not registered on the current thread.
    ///
    /// # Errors
    /// Returns an `io::Error` if creating the underlying OS thread or Tokio runtime fails.
    ///
    /// [tokio-runtime]: tokio::runtime::Runtime
    /// [`Runtime`]: crate::Runtime
    pub fn try_with_tokio_rt<F, R>(runtime_factory: F) -> io::Result<Arbiter>
    where
        F: FnOnce() -> io::Result<R> + Send + 'static,
        R: Into<crate::runtime::Runtime> + Send + 'static,
    {
        ArbiterBuilder::new().runtime(runtime_factory).try_build()
    }

    /// Sets up an Arbiter runner in a new System using the environment's local set.
    pub(crate) fn in_new_system() -> ArbiterHandle {
        let (tx, rx) = mpsc::unbounded_channel();

        let hnd = ArbiterHandle::new(tx);

        HANDLE.with(|cell| *cell.borrow_mut() = Some(hnd.clone()));

        crate::spawn(ArbiterRunner { rx });

        hnd
    }

    /// Return a handle to the this Arbiter's message sender.
    pub fn handle(&self) -> ArbiterHandle {
        ArbiterHandle::new(self.tx.clone())
    }

    /// Return a handle to the current thread's Arbiter's message sender.
    ///
    /// # Panics
    /// Panics if no Arbiter is running on the current thread.
    pub fn current() -> ArbiterHandle {
        HANDLE.with(|cell| match *cell.borrow() {
            Some(ref hnd) => hnd.clone(),
            None => panic!("Arbiter is not running."),
        })
    }

    /// Try to get current running arbiter handle.
    ///
    /// Returns `None` if no Arbiter has been started.
    ///
    /// Unlike [`current`](Self::current), this never panics.
    pub fn try_current() -> Option<ArbiterHandle> {
        HANDLE.with(|cell| cell.borrow().clone())
    }

    /// Stop Arbiter from continuing it's event loop.
    ///
    /// Returns true if stop message was sent successfully and false if the Arbiter has been dropped.
    pub fn stop(&self) -> bool {
        self.tx.send(ArbiterCommand::Stop).is_ok()
    }

    /// Send a future to the Arbiter's thread and spawn it.
    ///
    /// If you require a result, include a response channel in the future.
    ///
    /// Returns true if future was sent successfully and false if the Arbiter has died.
    #[track_caller]
    pub fn spawn<Fut>(&self, future: Fut) -> bool
    where
        Fut: Future<Output = ()> + Send + 'static,
    {
        self.tx
            .send(ArbiterCommand::Execute(Box::pin(future)))
            .is_ok()
    }

    /// Send a function to the Arbiter's thread and execute it.
    ///
    /// Any result from the function is discarded. If you require a result, include a response
    /// channel in the function.
    ///
    /// Returns true if function was sent successfully and false if the Arbiter has died.
    #[track_caller]
    pub fn spawn_fn<F>(&self, f: F) -> bool
    where
        F: FnOnce() + Send + 'static,
    {
        self.spawn(async { f() })
    }

    /// Returns true if the Arbiter's command channel is still open.
    ///
    /// The Arbiter can stop after this check, so this does not guarantee that a subsequent spawn
    /// call will succeed.
    pub fn alive(&self) -> bool {
        !self.tx.is_closed()
    }

    /// Wait for Arbiter's event loop to complete.
    ///
    /// Joins the underlying OS thread handle. See [`JoinHandle::join`](thread::JoinHandle::join).
    pub fn join(self) -> thread::Result<()> {
        self.thread_handle.join()
    }
}

/// A persistent future that processes [Arbiter] commands.
struct ArbiterRunner {
    rx: mpsc::UnboundedReceiver<ArbiterCommand>,
}

impl Future for ArbiterRunner {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        // process all items currently buffered in channel
        loop {
            match ready!(self.rx.poll_recv(cx)) {
                // channel closed; no more messages can be received
                None => return Poll::Ready(()),

                // process arbiter command
                Some(item) => match item {
                    ArbiterCommand::Stop => {
                        return Poll::Ready(());
                    }
                    ArbiterCommand::Execute(task_fut) => {
                        tokio::task::spawn_local(task_fut);
                    }
                },
            }
        }
    }
}

/// A builder for configuring and spawning a new [Arbiter] thread.
pub struct ArbiterBuilder {
    name_factory: Option<Box<dyn Fn(usize, usize) -> String + 'static>>,
    runtime_factory:
        Option<Box<dyn FnOnce() -> io::Result<crate::runtime::Runtime> + Send + 'static>>,
}

impl ArbiterBuilder {
    /// Create a new [ArbiterBuilder].
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self {
            name_factory: None,
            runtime_factory: None,
        }
    }

    /// Specify a factory function for generating the name of the Arbiter thread.
    ///
    /// Defaults to `actix-rt|system:<system_id>|arbiter:<arb_id>`
    ///
    /// # Example
    ///
    /// ```no_run
    /// let _ = actix_rt::System::new();
    /// actix_rt::ArbiterBuilder::new()
    ///     .name(|system_id, arb_id| {
    ///         format!("some-prefix|system:{}|arbiter:{}", system_id, arb_id)
    ///     })
    ///     .build();
    /// ```
    pub fn name<F>(mut self, name_factory: F) -> ArbiterBuilder
    where
        F: Fn(usize, usize) -> String + 'static,
    {
        self.name_factory = Some(Box::new(name_factory));
        self
    }

    /// Specify a factory function for generating the [Tokio Runtime](tokio-runtime) used by the Arbiter.
    ///
    /// [tokio-runtime]: tokio::runtime::Runtime
    pub fn runtime<F, R>(mut self, runtime_factory: F) -> ArbiterBuilder
    where
        F: FnOnce() -> io::Result<R> + Send + 'static,
        R: Into<crate::runtime::Runtime> + Send + 'static,
    {
        self.runtime_factory = Some(Box::new(move || runtime_factory().map(|rt| rt.into())));
        self
    }

    /// Spawn a new Arbiter thread and start its event loop.
    ///
    /// # Panics
    /// Panics if a [System] is not registered on the current thread.
    pub fn build(self) -> Arbiter {
        self.try_build()
            .unwrap_or_else(|err| panic!("Cannot create new Arbiter: {err:?}"))
    }

    /// Try spawning a new Arbiter thread and start its event loop.
    pub fn try_build(self) -> io::Result<Arbiter> {
        let sys = System::current();
        let system_id = sys.id();
        let arb_id = COUNT.fetch_add(1, Ordering::Relaxed);

        let name = match self.name_factory {
            Some(nf) => nf(system_id, arb_id),
            None => format!("actix-rt|system:{}|arbiter:{}", system_id, arb_id),
        };
        let runtime_factory = self.runtime_factory.unwrap_or_else(|| {
            Box::new(|| crate::runtime::default_tokio_runtime().map(|rt| rt.into()))
        });
        let (tx, rx) = mpsc::unbounded_channel();

        let (ready_tx, ready_rx) = std::sync::mpsc::channel::<io::Result<()>>();

        let thread_handle = thread::Builder::new().name(name.clone()).spawn({
            let tx = tx.clone();
            move || {
                let rt = match runtime_factory() {
                    Ok(rt) => rt,
                    Err(err) => {
                        let _ = ready_tx.send(Err(err));
                        return;
                    }
                };

                let hnd = ArbiterHandle::new(tx);

                System::set_current(sys);

                HANDLE.with(|cell| *cell.borrow_mut() = Some(hnd.clone()));

                // register arbiter
                let _ = System::current()
                    .tx()
                    .send(SystemCommand::RegisterArbiter(arb_id, hnd));

                if ready_tx.send(Ok(())).is_err() {
                    unreachable!("Arbiter ready signal receiver should not be dropped before send");
                }

                // run arbiter event processing loop
                rt.block_on(ArbiterRunner { rx });

                // deregister arbiter
                let _ = System::current()
                    .tx()
                    .send(SystemCommand::DeregisterArbiter(arb_id));
            }
        })?;

        match ready_rx.recv() {
            Ok(Ok(())) => Ok(Arbiter { tx, thread_handle }),
            Ok(Err(err)) => {
                let _ = thread_handle.join();
                Err(err)
            }
            Err(_) => {
                let _ = thread_handle.join();
                Err(io::Error::other(format!(
                    "Arbiter thread {name} panicked during initialization"
                )))
            }
        }
    }
}