Skip to main content

tor_log_ratelim/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6#![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![allow(clippy::cognitive_complexity)] // See arti#2556
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![warn(clippy::or_fun_call)]
28#![deny(clippy::print_stderr)]
29#![deny(clippy::print_stdout)]
30#![warn(clippy::rc_buffer)]
31#![deny(clippy::ref_option_ref)]
32#![warn(clippy::semicolon_if_nothing_returned)]
33#![warn(clippy::trait_duplication_in_bounds)]
34#![deny(clippy::unchecked_time_subtraction)]
35#![deny(clippy::unnecessary_wraps)]
36#![warn(clippy::unseparated_literal_suffix)]
37#![deny(clippy::unwrap_used)]
38#![deny(clippy::mod_module_files)]
39#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
40#![allow(clippy::uninlined_format_args)]
41#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
42#![allow(clippy::result_large_err)] // temporary workaround for arti#587
43#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
44#![allow(clippy::needless_lifetimes)] // See arti#1765
45#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
46#![allow(clippy::collapsible_if)] // See arti#2342
47#![deny(clippy::unused_async)]
48#![deny(clippy::string_slice)] // See arti#2571
49#![allow(recursion_depth_exceeding_limit)] // arti#2715, rust/issues/159228
50//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
51
52/// Implementation notes
53///
54/// We build our logging in a few layers.
55///
56/// At the lowest level, there is a [`Loggable`] trait, for events which can
57/// accumulate and eventually be flushed; this combines with the
58/// [`RateLim`](ratelim::RateLim) structure, which is responsible for managing
59/// the decision of when to flush these [`Loggable`]s.
60///
61/// The role of RateLim is to decide
62/// when to flush the information in a `Loggable`,
63/// and to flush the `Loggable` as needed.
64/// The role of a `Loggable` is to
65/// accumulate information
66/// and to know how to flush that information as a log message
67/// when it is told to do so.
68///
69/// One layer up, there is [`LogState`](logstate::LogState), which is used to to
70/// implement `Loggable` as used by [`log_ratelim!`].
71/// It can remember the name of an activity, accumulate
72/// successes and failures, and remember an error and associated message.
73///
74/// The highest layer is the [`log_ratelim!`] macro, which uses
75/// [`RateLim`](ratelim::RateLim) and [`LogState`](logstate::LogState) to record
76/// successes and failures, and launch background tasks as needed.
77mod implementation_notes {}
78
79mod logstate;
80mod macros;
81mod ratelim;
82
83use std::time::Duration;
84
85pub use ratelim::rt::{InstallRuntimeError, install_runtime};
86
87/// Re-exports for macros.
88#[doc(hidden)]
89pub mod macro_prelude {
90    pub use crate::{
91        Activity, Loggable,
92        logstate::LogState,
93        ratelim::{RateLim, rt::rt_support},
94    };
95    pub use std::sync::LazyLock;
96    pub use std::sync::{Arc, Mutex, Weak};
97    pub use tor_error::ErrorReport;
98    pub use tracing;
99
100    /// Alias to force use of RandomState, regardless of features enabled in `weak_tables`.
101    ///
102    /// See <https://github.com/tov/weak-table-rs/issues/23> for discussion.
103    pub type WeakValueHashMap<K, V> = weak_table::WeakValueHashMap<K, V, std::hash::RandomState>;
104}
105
106/// A group of events that can be logged singly or in a summary over a period of time.
107#[doc(hidden)]
108pub trait Loggable: 'static + Send {
109    /// Log these events immediately, if there is anything to log.
110    ///
111    /// The `summarizing` argument is the amount of time that this `Loggable``
112    /// has been accumulating information.
113    ///
114    /// Implementations should return `Active` if they have logged that
115    /// some activity happened, and `Dormant` if they had nothing to log, or
116    /// if they are logging "I didn't see that problem for a while."
117    ///
118    ///  After a `Loggable` has been dormant for a while, its timer will be reset.
119    fn flush(&mut self, summarizing: Duration) -> Activity;
120}
121
122/// A description of the whether a `Loggable` had something to say.
123#[doc(hidden)]
124#[derive(Clone, Copy, Debug, Eq, PartialEq)]
125#[allow(clippy::exhaustive_enums)] // okay, since this is doc(hidden).
126pub enum Activity {
127    /// There was a failure to report
128    Active,
129    /// There is nothing to report (no successes or failures).
130    Dormant,
131    /// There is a possible resolution to report (>0 successes and no failures).
132    AppearsResolved,
133}