pub struct Metering { /* private fields */ }Expand description
Appends Records to a log file, one JSON object per line.
Implementations§
Source§impl Metering
impl Metering
Sourcepub fn new(path: &Path) -> Self
pub fn new(path: &Path) -> Self
Opens a meter writing to path. The file is created on first append.
Sourcepub fn append(&self, record: &Record)
pub fn append(&self, record: &Record)
Appends one record.
Opens the file in append mode per record rather than holding a handle: the file may be rotated or truncated between requests, and a stale handle would silently keep writing to a deleted inode. A write failure cannot fail the request that has already been served, so it is logged to stderr instead of propagated.
Sourcepub fn usage_in_month(&self, key_id: &str, month: &str) -> MonthUsage
pub fn usage_in_month(&self, key_id: &str, month: &str) -> MonthUsage
Counts one key’s usage in one month by reading the log.
A missing log reads as zero usage, and a line that does not
parse is skipped: the log is append-only evidence written by
this process, so an unreadable line is a torn write, not a
record anyone was billed for. The scan is linear in the log’s
size — the file rotates with deployment (plan S5) — and only
runs for plans that carry a quota, plus the admin keys list.
Sourcepub fn query_count_reaches(&self, key_id: &str, month: &str, limit: u64) -> bool
pub fn query_count_reaches(&self, key_id: &str, month: &str, limit: u64) -> bool
True once the key’s query count in month has reached
limit. The scan stops at the limit: the quota check runs per
request, and past the limit the exact count no longer matters
(a refusal that needs the true number does one full
Metering::usage_in_month scan on its own cold path).
Sourcepub fn build(
&self,
key_id: &str,
plan: &str,
method: &str,
path: &str,
status: u16,
response_bytes: u64,
started: SystemTime,
) -> Record
pub fn build( &self, key_id: &str, plan: &str, method: &str, path: &str, status: u16, response_bytes: u64, started: SystemTime, ) -> Record
Builds a record with a freshly computed timestamp and duration.
One parameter per record field: the record is the parameter list, and a parameter struct would only rename the same eight values.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Metering
impl RefUnwindSafe for Metering
impl Send for Metering
impl Sync for Metering
impl Unpin for Metering
impl UnsafeUnpin for Metering
impl UnwindSafe for Metering
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more