跳到主要内容

BloomTokenLog

搜索

结构体 BloomTokenLog 

源代码
pub struct BloomTokenLog(/* private fields */);
展开描述

基于布隆过滤器的 TokenLog

可以参数化一个近似的最大分配字节数。最初将已使用的 token 存入一个哈希集合;一旦哈希集合过大,便会转换为一个布隆过滤器。这样可以实现内存占用随规模线性增长且有上限的形态。

基于 lifetime 将时间划分为若干时段,并在任意时刻为当前可能令未过期 token 过期的两个时段各维护一个过滤器。随着时间推移会不断轮换过滤器,以避免布隆过滤器的误判率随时间无限增长。

实现§

源代码§

impl BloomTokenLog

源代码

pub fn new_expected_items(max_bytes: usize, expected_hits: u64) -> BloomTokenLog

使用一个近似最大内存占用以及每个过期周期内预期的校验 token 使用次数进行构造

会自动计算出最优的布隆过滤器 k 值。

源代码

pub fn new(max_bytes: usize, k_num: u32) -> BloomTokenLog

使用一个近似最大内存占用以及一个 布隆过滤器 k 值进行构造

若选择自定义 k 值,请注意 BloomTokenLog 始终同时维护两个过滤器,并将 max_bytes 的分配预算在两者之间平均分配。因此,每个布隆过滤器将包含 max_bytes * 4 个比特位。

trait 实现§

源代码§

impl Default for BloomTokenLog

默认为最大 20 MiB 内存占用,并预期一百万次命中

在默认的 2 周校验 token 有效期内,这相当于每 1.21 秒使用一次 token。

源代码§

fn default() -> BloomTokenLog

Returns the “default value” for a type. 更多信息
源代码§

impl TokenLog for BloomTokenLog

源代码§

fn check_and_insert( &self, nonce: u128, issued: SystemTime, lifetime: Duration, ) -> Result<(), TokenReuseError>

Record that the token was used and, ideally, return a token reuse error if the token may have been already used previously 更多信息

自动 trait 实现§

blanket 实现§

源代码§

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

源代码§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
源代码§

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

源代码§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
源代码§

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

源代码§

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

Mutably borrows from an owned value. 更多信息
源代码§

impl<T> From<T> for T

源代码§

fn from(t: T) -> T

原样返回该参数。

源代码§

impl<T> Instrument for T

源代码§

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

Instruments this type with the provided Span, returning an Instrumented wrapper. 更多信息
源代码§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. 更多信息
源代码§

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

源代码§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换行为完全由 From<T> for U 的实现决定。

源代码§

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

源代码§

type Error = Infallible

The type returned in the event of a conversion error.
源代码§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
源代码§

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

源代码§

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

The type returned in the event of a conversion error.
源代码§

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

执行转换。
源代码§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

源代码§

fn vzip(self) -> V

源代码§

impl<T> WithSubscriber for T

源代码§

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

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. 更多信息
源代码§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. 更多信息
源代码§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

源代码§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

源代码§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,