Kea  2.3.9
tcp_connection_pool.cc
Go to the documentation of this file.
1 // Copyright (C) 2022 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 
13 using namespace isc::asiolink;
14 
15 namespace isc {
16 namespace tcp {
17 
18 std::atomic<uint64_t>
19 TcpConnectionPool::started_counter_(0);
20 
21 std::atomic<uint64_t>
22 TcpConnectionPool::stopped_counter_(0);
23 
24 std::atomic<uint64_t>
25 TcpConnectionPool::rejected_counter_(0);
26 
27 void
28 TcpConnectionPool::start(const TcpConnectionPtr& connection) {
29  if (util::MultiThreadingMgr::instance().getMode()) {
30  std::lock_guard<std::mutex> lk(mutex_);
31  connections_.insert(connections_.end(), connection);
32  started_counter_ += 1;
33  } else {
34  connections_.insert(connections_.end(), connection);
35  started_counter_ += 1;
36  }
37 
38  connection->asyncAccept();
39 }
40 
41 void
42 TcpConnectionPool::stop(const TcpConnectionPtr& connection) {
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 
59 void
60 TcpConnectionPool::shutdown(const TcpConnectionPtr& connection) {
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 
77 void
78 TcpConnectionPool::stopAll() {
79  if (util::MultiThreadingMgr::instance().getMode()) {
80  std::lock_guard<std::mutex> lk(mutex_);
81  stopAllInternal();
82  } else {
83  stopAllInternal();
84  }
85 }
86 
87 void
88 TcpConnectionPool::stopAllInternal() {
89  for (auto connection = connections_.begin();
90  connection != connections_.end();
91  ++connection) {
92  (*connection)->close();
93  }
94 
95  size_t cnt = connections_.size();
96  connections_.clear();
97  stopped_counter_ += cnt;
98 }
99 
100 size_t
101 TcpConnectionPool::usedByRemoteIp(const IOAddress& remote_ip,
102  size_t& total_connections) {
103  if (util::MultiThreadingMgr::instance().getMode()) {
104  std::lock_guard<std::mutex> lk(mutex_);
105  return (usedByRemoteIpInternal(remote_ip, total_connections));
106  } else {
107  return (usedByRemoteIpInternal(remote_ip, total_connections));
108  }
109 }
110 
111 size_t
112 TcpConnectionPool::usedByRemoteIpInternal(const IOAddress& remote_ip,
113  size_t& total_connections) {
114  total_connections = connections_.size();
115  size_t cnt = 0;
116  for (const auto& conn : connections_) {
117  const auto& ep = conn->getRemoteEndpoint();
118  if ((ep != TcpConnection::NO_ENDPOINT()) &&
119  (IOAddress(ep.address()) == remote_ip)) {
120  ++cnt;
121  }
122  }
123  return (cnt);
124 }
125 
126 }
127 }
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.