Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
BCLog::LogRateLimiter Class Reference

Fixed window rate limiter for logging. More...

#include <logging.h>

Collaboration diagram for BCLog::LogRateLimiter:
[legend]

Classes

struct  Stats
 Keeps track of an individual source location and how many available bytes are left for logging from it. More...

Public Types

enum class  Status { UNSUPPRESSED , NEWLY_SUPPRESSED , STILL_SUPPRESSED }
 Suppression status of a source log location. More...
using SchedulerFunction = std::function<void(std::function<void()>, std::chrono::milliseconds)>

Public Member Functions

Status Consume (const SourceLocation &source_loc, const std::string &str) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
void Reset () EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Resets all usage to zero. Called periodically by the scheduler.
bool SuppressionsActive () const
 Returns true if any log locations are currently being suppressed.

Static Public Member Functions

static std::shared_ptr< LogRateLimiterCreate (SchedulerFunction &&scheduler_func, uint64_t max_bytes, std::chrono::seconds reset_window)

Public Attributes

const uint64_t m_max_bytes
 Maximum number of bytes logged per location per window.
const std::chrono::seconds m_reset_window
 Interval after which the window is reset.

Private Member Functions

std::unordered_map< SourceLocation, Stats, SourceLocationHasher, SourceLocationEqual > m_source_locations GUARDED_BY (m_mutex)
 Stats for each source location that has attempted to log something.
 LogRateLimiter (uint64_t max_bytes, std::chrono::seconds reset_window)

Private Attributes

StdMutex m_mutex
std::atomic< bool > m_suppression_active {false}
 Whether any log locations are suppressed. Cached view on m_source_locations for performance reasons.

Detailed Description

Fixed window rate limiter for logging.

Definition at line 68 of file logging.h.

Member Typedef Documentation

◆ SchedulerFunction

using BCLog::LogRateLimiter::SchedulerFunction = std::function<void(std::function<void()>, std::chrono::milliseconds)>

Definition at line 93 of file logging.h.

Member Enumeration Documentation

◆ Status

enum class BCLog::LogRateLimiter::Status
strong

Suppression status of a source log location.

Enumerator
UNSUPPRESSED 
NEWLY_SUPPRESSED 
STILL_SUPPRESSED 

Definition at line 111 of file logging.h.

Constructor & Destructor Documentation

◆ LogRateLimiter()

BCLog::LogRateLimiter::LogRateLimiter ( uint64_t max_bytes,
std::chrono::seconds reset_window )
private

Definition at line 376 of file logging.cpp.

Here is the caller graph for this function:

Member Function Documentation

◆ Consume()

BCLog::LogRateLimiter::Status BCLog::LogRateLimiter::Consume ( const SourceLocation & source_loc,
const std::string & str )
nodiscard

Consumes source_loc's available bytes corresponding to the size of the (formatted) str and returns its status.

Definition at line 391 of file logging.cpp.

◆ Create()

std::shared_ptr< BCLog::LogRateLimiter > BCLog::LogRateLimiter::Create ( SchedulerFunction && scheduler_func,
uint64_t max_bytes,
std::chrono::seconds reset_window )
static
Parameters
scheduler_funcCallable object used to schedule resetting the window. The first parameter is the function to be executed, and the second is the reset_window interval.
max_bytesMaximum number of bytes that can be logged for each source location.
reset_windowTime window after which the stats are reset.

Definition at line 379 of file logging.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GUARDED_BY()

std::unordered_map< SourceLocation, Stats, SourceLocationHasher, SourceLocationEqual > m_source_locations BCLog::LogRateLimiter::GUARDED_BY ( m_mutex )
private

Stats for each source location that has attempted to log something.

◆ Reset()

void BCLog::LogRateLimiter::Reset ( )

Resets all usage to zero. Called periodically by the scheduler.

Definition at line 555 of file logging.cpp.

Here is the call graph for this function:

◆ SuppressionsActive()

bool BCLog::LogRateLimiter::SuppressionsActive ( ) const
inline

Returns true if any log locations are currently being suppressed.

Definition at line 124 of file logging.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_max_bytes

const uint64_t BCLog::LogRateLimiter::m_max_bytes

Maximum number of bytes logged per location per window.

Definition at line 107 of file logging.h.

◆ m_mutex

StdMutex BCLog::LogRateLimiter::m_mutex
mutableprivate

Definition at line 84 of file logging.h.

◆ m_reset_window

const std::chrono::seconds BCLog::LogRateLimiter::m_reset_window

Interval after which the window is reset.

Definition at line 109 of file logging.h.

◆ m_suppression_active

std::atomic<bool> BCLog::LogRateLimiter::m_suppression_active {false}
private

Whether any log locations are suppressed. Cached view on m_source_locations for performance reasons.

Definition at line 89 of file logging.h.


The documentation for this class was generated from the following files: