Bitcoin Core 29.99.0
P2P Digital Currency
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
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 std::source_location &source_loc, const std::string &str) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Consumes source_loc's available bytes corresponding to the size of the (formatted) str and returns its status. More...
 
void Reset () EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Resets all usage to zero. Called periodically by the scheduler. More...
 
bool SuppressionsActive () const
 Returns true if any log locations are currently being suppressed. More...
 

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. More...
 
const std::chrono::seconds m_reset_window
 Interval after which the window is reset. More...
 

Private Member Functions

std::unordered_map< std::source_location, Stats, SourceLocationHasher, SourceLocationEqual > m_source_locations GUARDED_BY (m_mutex)
 Stats for each source location that has attempted to log something. More...
 
 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. More...
 

Detailed Description

Fixed window rate limiter for logging.

Definition at line 113 of file logging.h.

Member Typedef Documentation

◆ SchedulerFunction

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

Definition at line 138 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 156 of file logging.h.

Constructor & Destructor Documentation

◆ LogRateLimiter()

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

Definition at line 374 of file logging.cpp.

Member Function Documentation

◆ Consume()

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

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

Definition at line 389 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 377 of file logging.cpp.

Here is the caller graph for this function:

◆ GUARDED_BY()

std::unordered_map< std::source_location, 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 553 of file logging.cpp.

◆ SuppressionsActive()

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

Returns true if any log locations are currently being suppressed.

Definition at line 169 of file logging.h.

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 152 of file logging.h.

◆ m_mutex

StdMutex BCLog::LogRateLimiter::m_mutex
mutableprivate

Definition at line 129 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 154 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 134 of file logging.h.


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