Kea 2.5.8
tcp_connection_pool.cc
Go to the documentation of this file.
1// Copyright (C) 2022-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
12
13using namespace isc::asiolink;
14
15namespace isc {
16namespace tcp {
17
18std::atomic<uint64_t>
20
21std::atomic<uint64_t>
23
24std::atomic<uint64_t>
26
27void
29 if (util::MultiThreadingMgr::instance().getMode()) {
30 std::lock_guard<std::mutex> lk(mutex_);
31 connections_.insert(connections_.end(), connection);
33 } else {
34 connections_.insert(connections_.end(), connection);
36 }
37
38 connection->asyncAccept();
39}
40
41void
43 if (util::MultiThreadingMgr::instance().getMode()) {
44 std::lock_guard<std::mutex> lk(mutex_);
45 size_t before = connections_.size();
46 connections_.remove(connection);
47 size_t after = connections_.size();
48 stopped_counter_ += before - after;
49 } else {
50 size_t before = connections_.size();
51 connections_.remove(connection);
52 size_t after = connections_.size();
53 stopped_counter_ += before - after;
54 }
55
56 connection->close();
57}
58
59void
61 if (util::MultiThreadingMgr::instance().getMode()) {
62 std::lock_guard<std::mutex> lk(mutex_);
63 size_t before = connections_.size();
64 connections_.remove(connection);
65 size_t after = connections_.size();
66 stopped_counter_ += before - after;
67 } else {
68 size_t before = connections_.size();
69 connections_.remove(connection);
70 size_t after = connections_.size();
71 stopped_counter_ += before - after;
72 }
73
74 connection->shutdown();
75}
76
77void
79 if (util::MultiThreadingMgr::instance().getMode()) {
80 std::lock_guard<std::mutex> lk(mutex_);
82 } else {
84 }
85}
86
87void
89 for (auto const& connection : connections_) {
90 connection->close();
91 }
92
93 size_t cnt = connections_.size();
94 connections_.clear();
95 stopped_counter_ += cnt;
96}
97
98size_t
100 size_t& total_connections) {
101 if (util::MultiThreadingMgr::instance().getMode()) {
102 std::lock_guard<std::mutex> lk(mutex_);
103 return (usedByRemoteIpInternal(remote_ip, total_connections));
104 } else {
105 return (usedByRemoteIpInternal(remote_ip, total_connections));
106 }
107}
108
109size_t
111 size_t& total_connections) {
112 total_connections = connections_.size();
113 size_t cnt = 0;
114 for (auto const& conn : connections_) {
115 auto const& ep = conn->getRemoteEndpoint();
116 if ((ep != TcpConnection::NO_ENDPOINT()) &&
117 (IOAddress(ep.address()) == remote_ip)) {
118 ++cnt;
119 }
120 }
121 return (cnt);
122}
123
124}
125}
void stopAll()
Stops all connections and removes them from the pool.
void stop(const TcpConnectionPtr &connection)
Removes a connection from the pool and stops it.
void stopAllInternal()
Stops all connections and removes them from the pool.
size_t usedByRemoteIp(const asiolink::IOAddress &remote_ip, size_t &total_connections)
Returns the number of connections using a given remote IP address.
static std::atomic< uint64_t > rejected_counter_
Class/static rejected (by the accept filter) connection counter.
size_t usedByRemoteIpInternal(const asiolink::IOAddress &remote_ip, size_t &total_connections)
Returns the number of connections using a given remote IP address.
static std::atomic< uint64_t > stopped_counter_
Class/static stopped (i.e. removed from pool) connection counter.
void shutdown(const TcpConnectionPtr &connection)
Removes a connection from the pool and shutdown it.
std::mutex mutex_
Mutex to protect the internal state.
void start(const TcpConnectionPtr &connection)
Start new connection.
static std::atomic< uint64_t > started_counter_
Class/static started (i.e. added to pool) connection counter.
TcpConnectionList connections_
Set of connections.
static const boost::asio::ip::tcp::endpoint & NO_ENDPOINT()
Returns an empty end point.
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
boost::shared_ptr< TcpConnection > TcpConnectionPtr
Pointer to the TcpConnection.
Defines the logger used by the top-level component of kea-lfc.