flow_guard/
lib.rs

1/* * Created and Developed by: Cleiton Augusto Correa Bezerra
2 * Project: FlowGuard - Adaptive Concurrency Control & Backpressure
3 * License: MIT / Apache-2.0
4 */
5
6//! # FlowGuard
7//!
8//! `flow-guard` é uma biblioteca de controle de concorrência adaptativo.
9//! Desenvolvida por Cleiton Augusto Correa Bezerra, ela implementa algoritmos
10//! de backpressure dinâmico para proteger sistemas de alta carga.
11
12// 1. Declaração dos módulos internos
13pub mod error;
14pub mod limiter;
15mod semaphore;
16pub mod strategy;
17
18#[cfg(feature = "tower")]
19pub mod integration;
20
21pub use error::FlowError;
22pub use limiter::FlowGuard;
23pub use strategy::VegasStrategy;
24
25#[cfg(feature = "tower")]
26pub use integration::FlowGuardLayer;
27
28use std::time::Duration;
29
30/// Trait fundamental para definir como o limite de requisições deve se comportar.
31///
32/// Implementado por estratégias como `VegasStrategy`.
33pub trait LimitStrategy: Send + Sync {
34    /// Retorna o limite de concorrência atual permitido pela estratégia.
35    fn current_limit(&self) -> usize;
36
37    /// Chamado após uma execução bem-sucedida para atualizar a latência.
38    fn on_success(&self, latency: Duration);
39
40    /// Chamado quando ocorre um erro para que a estratégia possa reduzir a carga.
41    fn on_error(&self);
42}
43
44impl<S: LimitStrategy + ?Sized> LimitStrategy for std::sync::Arc<S> {
45    fn current_limit(&self) -> usize {
46        (**self).current_limit()
47    }
48    fn on_success(&self, latency: std::time::Duration) {
49        (**self).on_success(latency)
50    }
51    fn on_error(&self) {
52        (**self).on_error()
53    }
54}