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