reactor/
lib.rs

1// Library for concurrent I/O resource management using reactor pattern.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2021-2023 by
6//     Dr. Maxim Orlovsky <orlovsky@ubideco.org>
7//     Alexis Sellier <alexis@cloudhead.io>
8//
9// Copyright 2022-2023 UBIDECO Institute, Switzerland
10// Copyright 2021 Alexis Sellier <alexis@cloudhead.io>
11//
12// Licensed under the Apache License, Version 2.0 (the "License");
13// you may not use this file except in compliance with the License.
14// You may obtain a copy of the License at
15//
16//     http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the License is distributed on an "AS IS" BASIS,
20// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21// See the License for the specific language governing permissions and
22// limitations under the License.
23
24#![deny(
25    non_upper_case_globals,
26    non_camel_case_types,
27    non_snake_case,
28    unused_mut,
29    unused_imports,
30    dead_code,
31    missing_docs
32)]
33#![cfg_attr(docsrs, feature(doc_auto_cfg))]
34
35//! ### _Concurrent I/O without rust async problems_
36//!
37//! This repository provides a set of libraries for concurrent access to I/O resources (file,
38//! network, devices etc) which uses reactor pattern with pluggable `poll` syscall engines. This
39//! allows to handle situations like multiple network connections within the scope of a single
40//! thread (see [C10k problem]).
41//!
42//! The crate can be used for building concurrent microservice architectures without polling all
43//! APIs with `async`s.
44//!
45//! The reactor design pattern is an event handling pattern for handling service requests delivered
46//! concurrently to a service handler by one or more inputs. The service handler then demultiplexes
47//! the incoming requests and dispatches them synchronously to the associated request handlers[^1].
48//!
49//! Core concepts:
50//! - **[`Resource`]**: any resource that can provide input to or consume output from the system.
51//! - **[`Runtime`]**: runs an event loop to block on all resources. Sends the resource service when
52//!   it is possible to start a synchronous operation on a resource without blocking (Example: a
53//!   synchronous call to read() will block if there is no data to read.
54//! - **Service**: custom business logic provided by the application for a given set of resources.
55//!   Service exposes a **[`Handler`] API** for the reactor [`Runtime`]. It is also responsible for
56//!   the creation and destruction of the resources.
57//!
58//! [`Reactor`] exposes a high-level API and manages multiple resources of two main kinds listeners
59//! and sessions. It uses a dedicated thread blocked on I/O events from all of these resources and
60//! than calls the [`Resource::handle_io`] to process the I/O and generate resource-specific event.
61//!
62//! Once a resource generates an event, reactor calls [`Handler`] API (in the context of the runtime
63//! thread) to process each of the events.
64//!
65//! All resources under poll reactor must be representable as file descriptors.
66//!
67//! [C10k problem]: https://en.wikipedia.org/wiki/C10k_problem
68//! [^1]: Schmidt, Douglas et al. Pattern-Oriented Software Architecture Volume 2: Patterns for
69//!       Concurrent and Networked Objects. Volume 2. Wiley, 2000.
70
71#[macro_use]
72extern crate amplify;
73
74pub mod poller;
75mod reactor;
76mod resource;
77mod timeouts;
78
79pub use resource::{
80    Io, Resource, ResourceId, ResourceIdGenerator, ResourceType, WriteAtomic, WriteError,
81};
82pub use timeouts::{Timer, Timestamp};
83
84pub use self::reactor::{Action, Controller, Error, Handler, Reactor, Runtime};