tibba_runtime/app_state.rs
1// Copyright 2026 Tree xie.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
16use std::time::SystemTime;
17
18/// 线程安全的应用状态,包含服务标识、生命周期及流控所需的并发计数器。
19///
20/// 历史上的 `peak_processing` / `total_requests` / `error_requests` 等
21/// 统计字段已迁移到 Prometheus(见 `src/metrics.rs` 与 `tibba-middleware`),
22/// 这里只保留流控判断必须的 `processing` 原子计数。
23pub struct AppState {
24 /// 服务名称
25 name: String,
26 /// 语义化版本号(如 "1.2.3")
27 version: String,
28 /// Git 提交 ID
29 commit_id: String,
30 /// 最大并发请求数;负数表示不限制
31 processing_limit: i32,
32 /// 当前应用运行状态(运行中 / 已停止)
33 running: AtomicBool,
34 /// 当前正在处理的请求数;用于 `processing_limit` 流控判断。
35 /// 历史峰值、累计请求数等已迁移到 Prometheus,不再在此维护。
36 processing: AtomicI32,
37 /// 应用启动时间戳
38 started_at: SystemTime,
39}
40
41impl AppState {
42 /// 创建 AppState,传入最大并发限制和 Git 提交 ID,其余字段使用默认值。
43 pub fn new(processing_limit: i32, commit_id: impl Into<String>) -> Self {
44 Self {
45 name: String::new(),
46 version: String::new(),
47 commit_id: commit_id.into(),
48 processing_limit,
49 running: AtomicBool::new(false),
50 processing: AtomicI32::new(0),
51 started_at: SystemTime::now(),
52 }
53 }
54
55 /// 设置服务名称,支持链式调用。
56 #[must_use]
57 pub fn with_name(mut self, name: impl Into<String>) -> Self {
58 self.name = name.into();
59 self
60 }
61
62 /// 设置语义化版本号,支持链式调用。
63 #[must_use]
64 pub fn with_version(mut self, version: impl Into<String>) -> Self {
65 self.version = version.into();
66 self
67 }
68
69 /// 返回服务名称。
70 pub fn get_name(&self) -> &str {
71 &self.name
72 }
73
74 /// 返回语义化版本号。
75 pub fn get_version(&self) -> &str {
76 &self.version
77 }
78
79 /// 返回 Git 提交 ID。
80 pub fn get_commit_id(&self) -> &str {
81 &self.commit_id
82 }
83
84 /// 返回配置的最大并发请求数。
85 pub fn get_processing_limit(&self) -> i32 {
86 self.processing_limit
87 }
88
89 /// 原子性地递增处理计数器,返回递增后的当前并发数。
90 /// 仅用于 `processing_limit` 流控判断;统计指标由 Prometheus 负责。
91 pub fn inc_processing(&self) -> i32 {
92 self.processing.fetch_add(1, Ordering::Relaxed) + 1
93 }
94
95 /// 原子性地递减处理计数器,返回递减后的当前并发数。
96 pub fn dec_processing(&self) -> i32 {
97 self.processing.fetch_sub(1, Ordering::Relaxed) - 1
98 }
99
100 /// 返回当前正在处理的请求数。
101 pub fn get_processing(&self) -> i32 {
102 self.processing.load(Ordering::Relaxed)
103 }
104
105 /// 返回 `true` 表示应用当前处于运行状态。
106 pub fn is_running(&self) -> bool {
107 self.running.load(Ordering::Relaxed)
108 }
109
110 /// 将应用状态设为运行中。
111 pub fn run(&self) {
112 self.running.store(true, Ordering::Relaxed)
113 }
114
115 /// 将应用状态设为已停止。
116 pub fn stop(&self) {
117 self.running.store(false, Ordering::Relaxed)
118 }
119
120 /// 返回应用的启动时间。
121 pub fn get_started_at(&self) -> SystemTime {
122 self.started_at
123 }
124}