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

Per-client connection limiter. More...

#include <rate_limiter.h>

Collaboration diagram for kcenon::network::per_client_connection_limiter:
Collaboration graph

Public Member Functions

 per_client_connection_limiter (size_t max_per_client=10, size_t max_total=1000)
 Construct limiter.
 
bool try_accept (std::string_view client_id)
 Try to accept a connection from a client.
 
void release (std::string_view client_id)
 Release a connection from a client.
 
size_t client_connections (std::string_view client_id) const
 Get connection count for a client.
 
size_t total_connections () const noexcept
 Get total connection count.
 

Private Attributes

size_t max_per_client_
 
connection_limiter total_limiter_
 
std::mutex mutex_
 
std::unordered_map< std::string, size_t > client_connections_
 

Detailed Description

Per-client connection limiter.

Limits connections per client identifier (e.g., IP address).

Definition at line 481 of file rate_limiter.h.

Constructor & Destructor Documentation

◆ per_client_connection_limiter()

kcenon::network::per_client_connection_limiter::per_client_connection_limiter ( size_t max_per_client = 10,
size_t max_total = 1000 )
inlineexplicit

Construct limiter.

Parameters
max_per_clientMaximum connections per client
max_totalMaximum total connections
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 488 of file rate_limiter.h.

Member Function Documentation

◆ client_connections()

size_t kcenon::network::per_client_connection_limiter::client_connections ( std::string_view client_id) const
inlinenodiscard

Get connection count for a client.

Parameters
client_idClient identifier
Returns
Number of connections from client
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 544 of file rate_limiter.h.

544 {
545 std::unique_lock<std::mutex> lock(mutex_);
546 auto it = client_connections_.find(std::string(client_id));
547 return (it != client_connections_.end()) ? it->second : 0;
548 }
std::unordered_map< std::string, size_t > client_connections_

References client_connections_, and mutex_.

◆ release()

void kcenon::network::per_client_connection_limiter::release ( std::string_view client_id)
inline

Release a connection from a client.

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

Definition at line 525 of file rate_limiter.h.

525 {
526 std::unique_lock<std::mutex> lock(mutex_);
527 std::string key(client_id);
528
529 auto it = client_connections_.find(key);
530 if (it != client_connections_.end() && it->second > 0) {
531 --it->second;
532 if (it->second == 0) {
533 client_connections_.erase(it);
534 }
536 }
537 }
void on_disconnect() noexcept
Unregister a connection.

References client_connections_, mutex_, kcenon::network::connection_limiter::on_disconnect(), and total_limiter_.

Here is the call graph for this function:

◆ total_connections()

size_t kcenon::network::per_client_connection_limiter::total_connections ( ) const
inlinenodiscardnoexcept

Get total connection count.

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

Definition at line 554 of file rate_limiter.h.

554 {
555 return total_limiter_.current();
556 }
size_t current() const noexcept
Get current connection count.

References kcenon::network::connection_limiter::current(), and total_limiter_.

Here is the call graph for this function:

◆ try_accept()

bool kcenon::network::per_client_connection_limiter::try_accept ( std::string_view client_id)
inlinenodiscard

Try to accept a connection from a client.

Parameters
client_idClient identifier
Returns
true if accepted
Examples
/home/runner/work/network_system/network_system/src/internal/utils/rate_limiter.h.

Definition at line 499 of file rate_limiter.h.

499 {
500 // First check total limit
501 if (!total_limiter_.can_accept()) {
502 return false;
503 }
504
505 std::unique_lock<std::mutex> lock(mutex_);
506 std::string key(client_id);
507
508 auto& count = client_connections_[key];
509 if (count >= max_per_client_) {
510 return false;
511 }
512
513 if (!total_limiter_.try_accept()) {
514 return false;
515 }
516
517 ++count;
518 return true;
519 }
bool try_accept() noexcept
Try to accept a connection.
bool can_accept() const noexcept
Check if a new connection can be accepted.

References kcenon::network::connection_limiter::can_accept(), client_connections_, max_per_client_, mutex_, total_limiter_, and kcenon::network::connection_limiter::try_accept().

Here is the call graph for this function:

Member Data Documentation

◆ client_connections_

std::unordered_map<std::string, size_t> kcenon::network::per_client_connection_limiter::client_connections_
private

◆ max_per_client_

size_t kcenon::network::per_client_connection_limiter::max_per_client_
private

◆ mutex_

std::mutex kcenon::network::per_client_connection_limiter::mutex_
mutableprivate

◆ total_limiter_

connection_limiter kcenon::network::per_client_connection_limiter::total_limiter_
private

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