Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::connection_limiter Class Reference

Connection count limiter. More...

#include <rate_limiter.h>

Collaboration diagram for kcenon::network::connection_limiter:
Collaboration graph

Public Member Functions

 connection_limiter (size_t max_connections=1000)
 Construct connection limiter.
 
bool can_accept () const noexcept
 Check if a new connection can be accepted.
 
bool try_accept () noexcept
 Try to accept a connection.
 
void on_connect () noexcept
 Register a new connection.
 
void on_disconnect () noexcept
 Unregister a connection.
 
size_t current () const noexcept
 Get current connection count.
 
size_t max () const noexcept
 Get maximum connection limit.
 
void set_max (size_t max_connections) noexcept
 Set maximum connection limit.
 
size_t available () const noexcept
 Get available connection slots.
 
bool at_capacity () const noexcept
 Check if at capacity.
 

Private Attributes

size_t max_connections_
 
std::atomic< size_t > current_connections_
 

Detailed Description

Connection count limiter.

Limits the number of concurrent connections to prevent resource exhaustion attacks.

Thread Safety

This class is thread-safe. All public methods can be called from multiple threads concurrently.

Definition at line 305 of file rate_limiter.h.

Constructor & Destructor Documentation

◆ connection_limiter()

kcenon::network::connection_limiter::connection_limiter ( size_t max_connections = 1000)
inlineexplicit

Construct connection limiter.

Parameters
max_connectionsMaximum allowed concurrent connections
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 311 of file rate_limiter.h.

312 : max_connections_(max_connections)
std::atomic< size_t > current_connections_

Member Function Documentation

◆ at_capacity()

bool kcenon::network::connection_limiter::at_capacity ( ) const
inlinenodiscardnoexcept

Check if at capacity.

Returns
true if at maximum connections
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 397 of file rate_limiter.h.

397 {
398 return current_connections_.load(std::memory_order_acquire) >= max_connections_;
399 }

References current_connections_, and max_connections_.

◆ available()

size_t kcenon::network::connection_limiter::available ( ) const
inlinenodiscardnoexcept

Get available connection slots.

Returns
Number of available slots
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 388 of file rate_limiter.h.

388 {
389 size_t current = current_connections_.load(std::memory_order_acquire);
391 }
size_t current() const noexcept
Get current connection count.

References current(), current_connections_, and max_connections_.

Here is the call graph for this function:

◆ can_accept()

bool kcenon::network::connection_limiter::can_accept ( ) const
inlinenodiscardnoexcept

Check if a new connection can be accepted.

Returns
true if connection can be accepted
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 319 of file rate_limiter.h.

319 {
320 return current_connections_.load(std::memory_order_acquire) < max_connections_;
321 }

References current_connections_, and max_connections_.

Referenced by kcenon::network::per_client_connection_limiter::try_accept().

Here is the caller graph for this function:

◆ current()

size_t kcenon::network::connection_limiter::current ( ) const
inlinenodiscardnoexcept

Get current connection count.

Returns
Current number of connections
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 364 of file rate_limiter.h.

364 {
365 return current_connections_.load(std::memory_order_acquire);
366 }

References current_connections_.

Referenced by available(), kcenon::network::per_client_connection_limiter::total_connections(), and try_accept().

Here is the caller graph for this function:

◆ max()

size_t kcenon::network::connection_limiter::max ( ) const
inlinenodiscardnoexcept

Get maximum connection limit.

Returns
Maximum allowed connections
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 372 of file rate_limiter.h.

372 {
373 return max_connections_;
374 }

References max_connections_.

◆ on_connect()

void kcenon::network::connection_limiter::on_connect ( )
inlinenoexcept

Register a new connection.

Note
Use try_accept() instead when possible to atomically check and increment.
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 345 of file rate_limiter.h.

345 {
346 current_connections_.fetch_add(1, std::memory_order_release);
347 }

References current_connections_.

◆ on_disconnect()

void kcenon::network::connection_limiter::on_disconnect ( )
inlinenoexcept

Unregister a connection.

Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 352 of file rate_limiter.h.

352 {
353 size_t prev = current_connections_.fetch_sub(1, std::memory_order_release);
354 // Prevent underflow (shouldn't happen in correct usage)
355 if (prev == 0) {
356 current_connections_.fetch_add(1, std::memory_order_release);
357 }
358 }

References current_connections_.

Referenced by kcenon::network::connection_guard::release(), and kcenon::network::per_client_connection_limiter::release().

Here is the caller graph for this function:

◆ set_max()

void kcenon::network::connection_limiter::set_max ( size_t max_connections)
inlinenoexcept

Set maximum connection limit.

Parameters
max_connectionsNew maximum
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 380 of file rate_limiter.h.

380 {
381 max_connections_ = max_connections;
382 }

References max_connections_.

◆ try_accept()

bool kcenon::network::connection_limiter::try_accept ( )
inlinenodiscardnoexcept

Try to accept a connection.

Returns
true if connection was accepted, false if limit reached
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 327 of file rate_limiter.h.

327 {
328 size_t current = current_connections_.load(std::memory_order_acquire);
329 while (current < max_connections_) {
330 if (current_connections_.compare_exchange_weak(
331 current, current + 1,
332 std::memory_order_acq_rel,
333 std::memory_order_acquire)) {
334 return true;
335 }
336 }
337 return false;
338 }

References current(), current_connections_, and max_connections_.

Referenced by kcenon::network::per_client_connection_limiter::try_accept().

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

Member Data Documentation

◆ current_connections_

std::atomic<size_t> kcenon::network::connection_limiter::current_connections_
private

◆ max_connections_

size_t kcenon::network::connection_limiter::max_connections_
private

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