OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/tools/quic/quic_time_wait_list_manager.h" | 5 #include "net/tools/quic/quic_time_wait_list_manager.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 QuicPacketWriter* writer, | 81 QuicPacketWriter* writer, |
82 QuicServerSessionVisitor* visitor, | 82 QuicServerSessionVisitor* visitor, |
83 QuicConnectionHelperInterface* helper, | 83 QuicConnectionHelperInterface* helper, |
84 const QuicVersionVector& supported_versions) | 84 const QuicVersionVector& supported_versions) |
85 : time_wait_period_( | 85 : time_wait_period_( |
86 QuicTime::Delta::FromSeconds(FLAGS_quic_time_wait_list_seconds)), | 86 QuicTime::Delta::FromSeconds(FLAGS_quic_time_wait_list_seconds)), |
87 connection_id_clean_up_alarm_( | 87 connection_id_clean_up_alarm_( |
88 helper->CreateAlarm(new ConnectionIdCleanUpAlarm(this))), | 88 helper->CreateAlarm(new ConnectionIdCleanUpAlarm(this))), |
89 clock_(helper->GetClock()), | 89 clock_(helper->GetClock()), |
90 writer_(writer), | 90 writer_(writer), |
91 visitor_(visitor) { | 91 visitor_(visitor), |
| 92 num_connections_(0) { |
92 SetConnectionIdCleanUpAlarm(); | 93 SetConnectionIdCleanUpAlarm(); |
93 } | 94 } |
94 | 95 |
95 QuicTimeWaitListManager::~QuicTimeWaitListManager() { | 96 QuicTimeWaitListManager::~QuicTimeWaitListManager() { |
96 connection_id_clean_up_alarm_->Cancel(); | 97 connection_id_clean_up_alarm_->Cancel(); |
97 STLDeleteElements(&pending_packets_queue_); | 98 STLDeleteElements(&pending_packets_queue_); |
98 for (ConnectionIdMap::iterator it = connection_id_map_.begin(); | 99 for (ConnectionIdMap::iterator it = connection_id_map_.begin(); |
99 it != connection_id_map_.end(); | 100 it != connection_id_map_.end(); |
100 ++it) { | 101 ++it) { |
101 delete it->second.close_packet; | 102 delete it->second.close_packet; |
102 } | 103 } |
103 } | 104 } |
104 | 105 |
105 void QuicTimeWaitListManager::AddConnectionIdToTimeWait( | 106 void QuicTimeWaitListManager::AddConnectionIdToTimeWait( |
106 QuicConnectionId connection_id, | 107 QuicConnectionId connection_id, |
107 QuicVersion version, | 108 QuicVersion version, |
108 bool connection_rejected_statelessly, | 109 bool connection_rejected_statelessly, |
109 QuicEncryptedPacket* close_packet) { | 110 QuicEncryptedPacket* close_packet) { |
110 DCHECK(!connection_rejected_statelessly || !close_packet) | 111 DCHECK(!connection_rejected_statelessly || !close_packet) |
111 << "Connections that were rejected statelessly should not " | 112 << "Connections that were rejected statelessly should not " |
112 << "have a close packet. connection_id = " << connection_id; | 113 << "have a close packet. connection_id = " << connection_id; |
113 int num_packets = 0; | 114 int num_packets = 0; |
114 ConnectionIdMap::iterator it = connection_id_map_.find(connection_id); | 115 ConnectionIdMap::iterator it = connection_id_map_.find(connection_id); |
115 const bool new_connection_id = it == connection_id_map_.end(); | 116 const bool new_connection_id = it == connection_id_map_.end(); |
116 if (!new_connection_id) { // Replace record if it is reinserted. | 117 if (!new_connection_id) { // Replace record if it is reinserted. |
117 num_packets = it->second.num_packets; | 118 num_packets = it->second.num_packets; |
118 delete it->second.close_packet; | 119 delete it->second.close_packet; |
119 connection_id_map_.erase(it); | 120 connection_id_map_.erase(it); |
| 121 --num_connections_; |
120 } | 122 } |
121 TrimTimeWaitListIfNeeded(); | 123 TrimTimeWaitListIfNeeded(); |
122 DCHECK_LT(num_connections(), | 124 DCHECK_LT(num_connections(), |
123 static_cast<size_t>(FLAGS_quic_time_wait_list_max_connections)); | 125 static_cast<size_t>(FLAGS_quic_time_wait_list_max_connections)); |
124 ConnectionIdData data(num_packets, version, clock_->ApproximateNow(), | 126 ConnectionIdData data(num_packets, version, clock_->ApproximateNow(), |
125 close_packet, connection_rejected_statelessly); | 127 close_packet, connection_rejected_statelessly); |
126 connection_id_map_.insert(std::make_pair(connection_id, data)); | 128 connection_id_map_.insert(std::make_pair(connection_id, data)); |
| 129 ++num_connections_; |
127 if (new_connection_id) { | 130 if (new_connection_id) { |
128 visitor_->OnConnectionAddedToTimeWaitList(connection_id); | 131 visitor_->OnConnectionAddedToTimeWaitList(connection_id); |
129 } | 132 } |
130 } | 133 } |
131 | 134 |
132 bool QuicTimeWaitListManager::IsConnectionIdInTimeWait( | 135 bool QuicTimeWaitListManager::IsConnectionIdInTimeWait( |
133 QuicConnectionId connection_id) const { | 136 QuicConnectionId connection_id) const { |
134 return ContainsKey(connection_id_map_, connection_id); | 137 return ContainsKey(connection_id_map_, connection_id); |
135 } | 138 } |
136 | 139 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 ConnectionIdMap::iterator it = connection_id_map_.begin(); | 284 ConnectionIdMap::iterator it = connection_id_map_.begin(); |
282 QuicTime oldest_connection_id_time = it->second.time_added; | 285 QuicTime oldest_connection_id_time = it->second.time_added; |
283 if (oldest_connection_id_time > expiration_time) { | 286 if (oldest_connection_id_time > expiration_time) { |
284 // Too recent, don't retire. | 287 // Too recent, don't retire. |
285 return false; | 288 return false; |
286 } | 289 } |
287 // This connection_id has lived its age, retire it now. | 290 // This connection_id has lived its age, retire it now. |
288 const QuicConnectionId connection_id = it->first; | 291 const QuicConnectionId connection_id = it->first; |
289 delete it->second.close_packet; | 292 delete it->second.close_packet; |
290 connection_id_map_.erase(it); | 293 connection_id_map_.erase(it); |
| 294 --num_connections_; |
291 visitor_->OnConnectionRemovedFromTimeWaitList(connection_id); | 295 visitor_->OnConnectionRemovedFromTimeWaitList(connection_id); |
292 return true; | 296 return true; |
293 } | 297 } |
294 | 298 |
295 void QuicTimeWaitListManager::CleanUpOldConnectionIds() { | 299 void QuicTimeWaitListManager::CleanUpOldConnectionIds() { |
296 QuicTime now = clock_->ApproximateNow(); | 300 QuicTime now = clock_->ApproximateNow(); |
297 QuicTime expiration = now.Subtract(time_wait_period_); | 301 QuicTime expiration = now.Subtract(time_wait_period_); |
298 | 302 |
299 while (MaybeExpireOldestConnection(expiration)) { | 303 while (MaybeExpireOldestConnection(expiration)) { |
300 } | 304 } |
301 | 305 |
302 SetConnectionIdCleanUpAlarm(); | 306 SetConnectionIdCleanUpAlarm(); |
303 } | 307 } |
304 | 308 |
305 void QuicTimeWaitListManager::TrimTimeWaitListIfNeeded() { | 309 void QuicTimeWaitListManager::TrimTimeWaitListIfNeeded() { |
306 if (FLAGS_quic_time_wait_list_max_connections < 0) { | 310 if (FLAGS_quic_time_wait_list_max_connections < 0) { |
307 return; | 311 return; |
308 } | 312 } |
309 while (num_connections() >= | 313 while (num_connections() >= |
310 static_cast<size_t>(FLAGS_quic_time_wait_list_max_connections)) { | 314 static_cast<size_t>(FLAGS_quic_time_wait_list_max_connections)) { |
311 MaybeExpireOldestConnection(QuicTime::Infinite()); | 315 MaybeExpireOldestConnection(QuicTime::Infinite()); |
312 } | 316 } |
313 } | 317 } |
314 | 318 |
315 } // namespace tools | 319 } // namespace tools |
316 } // namespace net | 320 } // namespace net |
OLD | NEW |