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
//! # monolake-core
//!
//! `monolake-core` is a foundational crate for building high-performance, thread-per-core network
//! services. It provides a robust framework for worker orchestration, service deployment, and
//! lifecycle management, supporting protocols such as HTTP and Thrift. This crate builds upon
//! concepts from the `service_async` crate to implement a thread-per-core worker system with
//! advanced service management capabilities.
//!
//! ## Key Features
//!
//! - **Network Service Foundation**: Core building blocks for creating efficient network services.
//! - **Thread-per-Core Architecture**: Maximizes performance on multi-core processors.
//! - **Service Lifecycle Management**: Seamless updates and deployments of service chains.
//! - **Flexible Deployment Models**: Support for both single-stage and two-stage service deployment
//! processes.
//! - **State Transfer**: Facilitate updates with state preservation between service versions.
//! - **Protocol Support**: Built-in support for HTTP and Thrift protocols.
//! - **Asynchronous Design**: Leverages Rust's async capabilities for efficient, non-blocking
//! operations.
//!
//! ## Service and Service Factory Concepts
//!
//! This crate builds upon the `service_async` crate, providing:
//!
//! - A refined [`Service`](service_async::Service) trait that leverages `impl Trait` for improved
//! performance and flexibility.
//! - The [`AsyncMakeService`](service_async::AsyncMakeService) trait for efficient creation and
//! updating of services, particularly useful for managing stateful resources across service
//! updates.
//!
//! `monolake-core` extends these concepts to provide a comprehensive system for managing network
//! services in a thread-per-core architecture.
//!
//! ## Pre-built Services
//!
//! While `monolake-core` provides the foundation, you can find pre-built services for common
//! protocols in the `monolake-services` crate. This includes ready-to-use implementations for:
//!
//! - HTTP services
//! - Thrift services
//!
//! These pre-built services can be easily integrated into your `monolake-core` based applications,
//! speeding up development for standard network service scenarios.
//!
//! ## Worker-Service Lifecycle Management
//!
//! The core of this crate is the worker-service lifecycle management system, implemented in the
//! [`orchestrator`] module. Key components include:
//!
//! - [`WorkerManager`](orchestrator::WorkerManager): Manages multiple worker threads, each running
//! on a dedicated CPU core.
//! - [`ServiceExecutor`](orchestrator::ServiceExecutor): Handles the lifecycle of services within a
//! single worker thread.
//! - [`ServiceDeploymentContainer`](orchestrator::ServiceDeploymentContainer): Manages individual
//! service instances, including precommitting and deployment.
//! - [`ServiceCommand`](orchestrator::ServiceCommand): Represents actions to be performed on
//! services, such as precommitting, updating, or removing.
//!
//! This system supports dynamic updating of deployed services:
//!
//! - You can update a currently deployed service with a new service chain.
//! - Existing connections continue to use the old service chain.
//! - New connections automatically use the latest service chain.
//!
//! This approach ensures smooth transitions during updates with minimal disruption to ongoing
//! operations.
//!
//! ## Deployment Models
//!
//! The system supports two deployment models:
//!
//! 1. **Two-Stage Deployment**: Ideal for updating services while preserving state.
//! - Precommit a service using [`Precommit`](orchestrator::ServiceCommand::Precommit).
//! - Update using [`Update`](orchestrator::ServiceCommand::Update) or commit using
//! [`Commit`](orchestrator::ServiceCommand::Commit).
//!
//! 2. **Single-Stage Deployment**: Suitable for initial deployments or when state preservation
//! isn't necessary.
//! - Create and deploy in one step using
//! [`PrepareAndCommit`](orchestrator::ServiceCommand::PrepareAndCommit).
//!
//! ## Protocol Handlers
//!
//! ### HTTP Handler
//!
//! The [`http`] module provides the [`HttpHandler`](http::HttpHandler) trait for
//! implementing HTTP request handlers. It supports context-aware handling and connection
//! management.
//!
//! ### Thrift Handler
//!
//! The [`thrift`] module offers the [`ThriftHandler`](thrift::ThriftHandler) trait for
//! implementing Thrift request handlers.
//!
//! Both handler traits are automatically implemented for types that implement the
//! [`Service`](service_async::Service) trait with appropriate request and response types.
//!
//! ## Usage Example
//!
//! ```ignore
//! let mut manager = WorkerManager::new(config.runtime);
//! let join_handlers = manager.spawn_workers_async();
//! for (name, ServiceConfig { listener, server }) in config.servers.into_iter() {
//! let lis_fac = ListenerBuilder::try_from(listener).expect("build listener failed");
//! let svc_fac = l7_factory(server);
//! manager
//! .dispatch_service_command(ServiceCommand::PrepareAndCommit(
//! Arc::new(name),
//! AsyncMakeServiceWrapper(svc_fac),
//! AsyncMakeServiceWrapper(Arc::new(lis_fac)),
//! ))
//! .await
//! .err()
//! .expect("apply init config failed");
//! }
//! ```
//!
//! ## Modules
//!
//! - [`orchestrator`]: Core functionality for worker management and service deployment.
//! - [`http`]: HTTP-specific implementations and utilities.
//! - [`thrift`]: Thrift protocol support and related functionalities.
//! - [`config`]: Configuration structures and utilities for the system.
//! - [`context`]: Context management for request processing.
//! - [`listener`]: Network listener implementations and abstractions.
//! - [`util`]: Various utility functions and helpers.
//!
//! ## Error Handling
//!
//! This crate uses [`AnyError`] as a type alias for `anyhow::Error`, providing flexible error
//! handling. The [`AnyResult`] type alias offers a convenient way to return results that can
//! contain any error type.
pub use ;
pub