Skip to main content

Guard

Struct Guard 

Source
pub struct Guard { /* private fields */ }
Expand description

出口码率 + 媒体准入控制器。线程安全;整个进程共用一个(见 global)。

Implementations§

Source§

impl Guard

Source

pub fn admit_verification(&self, peer: IpAddr, identity: Option<&str>) -> bool

昂贵凭证校验(Argon2)的准入:先按来源 IP,再按身份(用户名或密钥 id)。

只应在缓存未命中、即将做一次昂贵校验时调用:正常客户端每连接/ 每分钟只付一次(结果被缓存命中),因此不会被限流;不断更换凭证的 来源会持续消耗额度,从而无法用 CPU 耗尽运行时。按身份计量可以挡住 「大量来源轮换同一账号/密钥」的情况,按 IP 计量可以挡住「同一来源 轮换大量身份」的情况。

Source§

impl Guard

Source

pub fn new(config: GuardConfig) -> Self

Source

pub fn config(&self) -> GuardConfig

Source

pub fn set_config(&self, config: GuardConfig)

运行期替换策略(测试放宽、应用热更新)。已累计的封禁/令牌桶状态保留。

Source

pub fn note_egress(&self, bytes: u64)

累计出口字节(单调递增)。

Source

pub fn egress_total(&self) -> u64

Source

pub fn egress_bps(&self) -> u64

当前出口码率(字节/秒);窗口 1 秒,取最近若干次测量与当前值的最大值。

Source

pub fn egress_limited(&self) -> bool

出口是否已达阈值(此时应拒绝新的媒体连接)。

Source

pub fn check_egress(&self) -> Result<(), &'static str>

只要出口准入(HLS 分片这类不占长连接的请求用)。

Source

pub fn admit_media( &self, peer: IpAddr, kind: MediaKind, ) -> Result<(), &'static str>

媒体连接准入:出口码率 / 单 IP 并发 / 建连速率 / 封禁。

Source

pub fn release_media(&self, peer: IpAddr, kind: MediaKind)

释放一个媒体名额(与 admit_media 配对)。

Source

pub fn snapshot(&self) -> GuardSnapshot

Auto Trait Implementations§

§

impl !Freeze for Guard

§

impl Forget for Guard

§

impl Move for Guard

§

impl RefUnwindSafe for Guard

§

impl Send for Guard
where RwLock<GuardConfig>: Send, Atomic<bool>: Send, Atomic<u64>: Send, Mutex<[u32; 4]>: Send, Mutex<Window>: Send, Mutex<Bucket>: Send, Mutex<HashMap<IpAddr, IpState>>: Send, Mutex<HashMap<Box<str>, IdentityBucket>>: Send,

§

impl Sync for Guard
where RwLock<GuardConfig>: Sync, Atomic<bool>: Sync, Atomic<u64>: Sync, Mutex<[u32; 4]>: Sync, Mutex<Window>: Sync, Mutex<Bucket>: Sync, Mutex<HashMap<IpAddr, IpState>>: Sync, Mutex<HashMap<Box<str>, IdentityBucket>>: Sync,

§

impl Unpin for Guard
where Align4<u32>: Unpin, Align1<u8>: Unpin, Align8<u64>: Unpin, *const u8 is !null: Unpin, Box<str>: Unpin,

§

impl UnsafeUnpin for Guard
where Align4<u32>: UnsafeUnpin, Align1<u8>: UnsafeUnpin, Align8<u64>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<(IpAddr, IpState)>: UnsafeUnpin, PhantomData<(Box<str>, IdentityBucket)>: UnsafeUnpin,

§

impl UnwindSafe for Guard
where RwLock<GuardConfig>: UnwindSafe, Align1<u8>: UnwindSafe, Align8<u64>: UnwindSafe, Mutex<[u32; 4]>: UnwindSafe, Mutex<Window>: UnwindSafe, Mutex<Bucket>: UnwindSafe, Mutex<HashMap<IpAddr, IpState>>: UnwindSafe, Mutex<HashMap<Box<str>, IdentityBucket>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more