tibba-runtime 0.2.7

process runtime state, metrics and lifecycle for tibba
Documentation
// Copyright 2026 Tree xie.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::time::SystemTime;

/// 线程安全的应用状态,包含服务标识、生命周期及流控所需的并发计数器。
///
/// 历史上的 `peak_processing` / `total_requests` / `error_requests` 等
/// 统计字段已迁移到 Prometheus(见 `src/metrics.rs` 与 `tibba-middleware`),
/// 这里只保留流控判断必须的 `processing` 原子计数。
pub struct AppState {
    /// 服务名称
    name: String,
    /// 语义化版本号(如 "1.2.3")
    version: String,
    /// Git 提交 ID
    commit_id: String,
    /// 最大并发请求数;负数表示不限制
    processing_limit: i32,
    /// 当前应用运行状态(运行中 / 已停止)
    running: AtomicBool,
    /// 当前正在处理的请求数;用于 `processing_limit` 流控判断。
    /// 历史峰值、累计请求数等已迁移到 Prometheus,不再在此维护。
    processing: AtomicI32,
    /// 应用启动时间戳
    started_at: SystemTime,
}

impl AppState {
    /// 创建 AppState,传入最大并发限制和 Git 提交 ID,其余字段使用默认值。
    pub fn new(processing_limit: i32, commit_id: impl Into<String>) -> Self {
        Self {
            name: String::new(),
            version: String::new(),
            commit_id: commit_id.into(),
            processing_limit,
            running: AtomicBool::new(false),
            processing: AtomicI32::new(0),
            started_at: SystemTime::now(),
        }
    }

    /// 设置服务名称,支持链式调用。
    #[must_use]
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// 设置语义化版本号,支持链式调用。
    #[must_use]
    pub fn with_version(mut self, version: impl Into<String>) -> Self {
        self.version = version.into();
        self
    }

    /// 返回服务名称。
    pub fn get_name(&self) -> &str {
        &self.name
    }

    /// 返回语义化版本号。
    pub fn get_version(&self) -> &str {
        &self.version
    }

    /// 返回 Git 提交 ID。
    pub fn get_commit_id(&self) -> &str {
        &self.commit_id
    }

    /// 返回配置的最大并发请求数。
    pub fn get_processing_limit(&self) -> i32 {
        self.processing_limit
    }

    /// 原子性地递增处理计数器,返回递增后的当前并发数。
    /// 仅用于 `processing_limit` 流控判断;统计指标由 Prometheus 负责。
    pub fn inc_processing(&self) -> i32 {
        self.processing.fetch_add(1, Ordering::Relaxed) + 1
    }

    /// 原子性地递减处理计数器,返回递减后的当前并发数。
    pub fn dec_processing(&self) -> i32 {
        self.processing.fetch_sub(1, Ordering::Relaxed) - 1
    }

    /// 返回当前正在处理的请求数。
    pub fn get_processing(&self) -> i32 {
        self.processing.load(Ordering::Relaxed)
    }

    /// 返回 `true` 表示应用当前处于运行状态。
    pub fn is_running(&self) -> bool {
        self.running.load(Ordering::Relaxed)
    }

    /// 将应用状态设为运行中。
    pub fn run(&self) {
        self.running.store(true, Ordering::Relaxed)
    }

    /// 将应用状态设为已停止。
    pub fn stop(&self) {
        self.running.store(false, Ordering::Relaxed)
    }

    /// 返回应用的启动时间。
    pub fn get_started_at(&self) -> SystemTime {
        self.started_at
    }
}