monolake_core/
lib.rs

1//! # monolake-core
2//!
3//! `monolake-core` is a foundational crate for building high-performance, thread-per-core network
4//! services. It provides a robust framework for worker orchestration, service deployment, and
5//! lifecycle management, supporting protocols such as HTTP and Thrift. This crate builds upon
6//! concepts from the `service_async` crate to implement a thread-per-core worker system with
7//! advanced service management capabilities.
8//!
9//! ## Key Features
10//!
11//! - **Network Service Foundation**: Core building blocks for creating efficient network services.
12//! - **Thread-per-Core Architecture**: Maximizes performance on multi-core processors.
13//! - **Service Lifecycle Management**: Seamless updates and deployments of service chains.
14//! - **Flexible Deployment Models**: Support for both single-stage and two-stage service deployment
15//!   processes.
16//! - **State Transfer**: Facilitate updates with state preservation between service versions.
17//! - **Protocol Support**: Built-in support for HTTP and Thrift protocols.
18//! - **Asynchronous Design**: Leverages Rust's async capabilities for efficient, non-blocking
19//!   operations.
20//!
21//! ## Service and Service Factory Concepts
22//!
23//! This crate builds upon the `service_async` crate, providing:
24//!
25//! - A refined [`Service`](service_async::Service) trait that leverages `impl Trait` for improved
26//!   performance and flexibility.
27//! - The [`AsyncMakeService`](service_async::AsyncMakeService) trait for efficient creation and
28//!   updating of services, particularly useful for managing stateful resources across service
29//!   updates.
30//!
31//! `monolake-core` extends these concepts to provide a comprehensive system for managing network
32//! services in a thread-per-core architecture.
33//!
34//! ## Pre-built Services
35//!
36//! While `monolake-core` provides the foundation, you can find pre-built services for common
37//! protocols in the `monolake-services` crate. This includes ready-to-use implementations for:
38//!
39//! - HTTP services
40//! - Thrift services
41//!
42//! These pre-built services can be easily integrated into your `monolake-core` based applications,
43//! speeding up development for standard network service scenarios.
44//!
45//! ## Worker-Service Lifecycle Management
46//!
47//! The core of this crate is the worker-service lifecycle management system, implemented in the
48//! [`orchestrator`] module. Key components include:
49//!
50//! - [`WorkerManager`](orchestrator::WorkerManager): Manages multiple worker threads, each running
51//!   on a dedicated CPU core.
52//! - [`ServiceExecutor`](orchestrator::ServiceExecutor): Handles the lifecycle of services within a
53//!   single worker thread.
54//! - [`ServiceDeploymentContainer`](orchestrator::ServiceDeploymentContainer): Manages individual
55//!   service instances, including precommitting and deployment.
56//! - [`ServiceCommand`](orchestrator::ServiceCommand): Represents actions to be performed on
57//!   services, such as precommitting, updating, or removing.
58//!
59//! This system supports dynamic updating of deployed services:
60//!
61//! - You can update a currently deployed service with a new service chain.
62//! - Existing connections continue to use the old service chain.
63//! - New connections automatically use the latest service chain.
64//!
65//! This approach ensures smooth transitions during updates with minimal disruption to ongoing
66//! operations.
67//!
68//! ## Deployment Models
69//!
70//! The system supports two deployment models:
71//!
72//! 1. **Two-Stage Deployment**: Ideal for updating services while preserving state.
73//!    - Precommit a service using [`Precommit`](orchestrator::ServiceCommand::Precommit).
74//!    - Update using [`Update`](orchestrator::ServiceCommand::Update) or commit using
75//!      [`Commit`](orchestrator::ServiceCommand::Commit).
76//!
77//! 2. **Single-Stage Deployment**: Suitable for initial deployments or when state preservation
78//!    isn't necessary.
79//!    - Create and deploy in one step using
80//!      [`PrepareAndCommit`](orchestrator::ServiceCommand::PrepareAndCommit).
81//!
82//! ## Protocol Handlers
83//!
84//! ### HTTP Handler
85//!
86//! The [`http`] module provides the [`HttpHandler`](http::HttpHandler) trait for
87//! implementing HTTP request handlers. It supports context-aware handling and connection
88//! management.
89//!
90//! ### Thrift Handler
91//!
92//! The [`thrift`] module offers the [`ThriftHandler`](thrift::ThriftHandler) trait for
93//! implementing Thrift request handlers.
94//!
95//! Both handler traits are automatically implemented for types that implement the
96//! [`Service`](service_async::Service) trait with appropriate request and response types.
97//!
98//! ## Usage Example
99//!
100//! ```ignore
101//!    let mut manager = WorkerManager::new(config.runtime);
102//!    let join_handlers = manager.spawn_workers_async();
103//!    for (name, ServiceConfig { listener, server }) in config.servers.into_iter() {
104//!         let lis_fac = ListenerBuilder::try_from(listener).expect("build listener failed");
105//!         let svc_fac = l7_factory(server);
106//!         manager
107//!             .dispatch_service_command(ServiceCommand::PrepareAndCommit(
108//!                    Arc::new(name),
109//!                    AsyncMakeServiceWrapper(svc_fac),
110//!                    AsyncMakeServiceWrapper(Arc::new(lis_fac)),
111//!              ))
112//!              .await
113//!              .err()
114//!              .expect("apply init config failed");
115//!    }  
116//! ```
117//!
118//! ## Modules
119//!
120//! - [`orchestrator`]: Core functionality for worker management and service deployment.
121//! - [`http`]: HTTP-specific implementations and utilities.
122//! - [`thrift`]: Thrift protocol support and related functionalities.
123//! - [`config`]: Configuration structures and utilities for the system.
124//! - [`context`]: Context management for request processing.
125//! - [`listener`]: Network listener implementations and abstractions.
126//! - [`util`]: Various utility functions and helpers.
127//!
128//! ## Error Handling
129//!
130//! This crate uses [`AnyError`] as a type alias for `anyhow::Error`, providing flexible error
131//! handling. The [`AnyResult`] type alias offers a convenient way to return results that can
132//! contain any error type.
133#[macro_use]
134mod error;
135pub use error::{AnyError, AnyResult};
136
137pub mod config;
138pub mod context;
139pub mod http;
140pub mod listener;
141pub mod orchestrator;
142pub mod thrift;
143pub mod util;
144
145pub(crate) mod sealed {
146    #[allow(dead_code)]
147    pub trait Sealed {}
148    #[allow(dead_code)]
149    pub trait SealedT<T1, T2> {}
150}