| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/base/client_socket_pool.h" | 5 #include "net/base/client_socket_pool.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "net/base/client_socket.h" | 8 #include "net/base/client_socket.h" |
| 9 #include "net/base/client_socket_handle.h" | 9 #include "net/base/client_socket_handle.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 | 11 |
| 12 using base::TimeDelta; | 12 using base::TimeDelta; |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // The timeout value, in seconds, used to clean up disconnected idle sockets. | 16 // The timeout value, in seconds, used to clean up idle sockets that can't be |
| 17 // reused. |
| 17 const int kCleanupInterval = 10; | 18 const int kCleanupInterval = 10; |
| 18 | 19 |
| 19 // The maximum duration, in seconds, to keep idle persistent sockets alive. | 20 // The maximum duration, in seconds, to keep idle persistent sockets alive. |
| 20 const int kIdleTimeout = 300; // 5 minutes. | 21 const int kIdleTimeout = 300; // 5 minutes. |
| 21 | 22 |
| 22 } // namespace | 23 } // namespace |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 | 26 |
| 26 ClientSocketPool::ClientSocketPool(int max_sockets_per_group) | 27 ClientSocketPool::ClientSocketPool(int max_sockets_per_group) |
| 27 : idle_socket_count_(0), | 28 : idle_socket_count_(0), |
| 28 max_sockets_per_group_(max_sockets_per_group) { | 29 max_sockets_per_group_(max_sockets_per_group) { |
| 29 } | 30 } |
| 30 | 31 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 47 DCHECK(callback); | 48 DCHECK(callback); |
| 48 r.callback = callback; | 49 r.callback = callback; |
| 49 group.pending_requests.push_back(r); | 50 group.pending_requests.push_back(r); |
| 50 return ERR_IO_PENDING; | 51 return ERR_IO_PENDING; |
| 51 } | 52 } |
| 52 | 53 |
| 53 // OK, we are going to activate one. | 54 // OK, we are going to activate one. |
| 54 group.active_socket_count++; | 55 group.active_socket_count++; |
| 55 | 56 |
| 56 // Use idle sockets in LIFO order because they're more likely to be | 57 // Use idle sockets in LIFO order because they're more likely to be |
| 57 // still connected. | 58 // still reusable. |
| 58 while (!group.idle_sockets.empty()) { | 59 while (!group.idle_sockets.empty()) { |
| 59 IdleSocket idle_socket = group.idle_sockets.back(); | 60 IdleSocket idle_socket = group.idle_sockets.back(); |
| 60 group.idle_sockets.pop_back(); | 61 group.idle_sockets.pop_back(); |
| 61 DecrementIdleCount(); | 62 DecrementIdleCount(); |
| 62 if ((*idle_socket.ptr)->IsConnected()) { | 63 if ((*idle_socket.ptr)->IsConnectedAndIdle()) { |
| 63 // We found one we can reuse! | 64 // We found one we can reuse! |
| 64 handle->socket_ = idle_socket.ptr; | 65 handle->socket_ = idle_socket.ptr; |
| 65 return OK; | 66 return OK; |
| 66 } | 67 } |
| 67 delete idle_socket.ptr; | 68 delete idle_socket.ptr; |
| 68 } | 69 } |
| 69 | 70 |
| 70 handle->socket_ = new ClientSocketPtr(); | 71 handle->socket_ = new ClientSocketPtr(); |
| 71 return OK; | 72 return OK; |
| 72 } | 73 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 96 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 97 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 97 this, &ClientSocketPool::DoReleaseSocket, handle->group_name_, | 98 this, &ClientSocketPool::DoReleaseSocket, handle->group_name_, |
| 98 handle->socket_)); | 99 handle->socket_)); |
| 99 } | 100 } |
| 100 | 101 |
| 101 void ClientSocketPool::CloseIdleSockets() { | 102 void ClientSocketPool::CloseIdleSockets() { |
| 102 CleanupIdleSockets(true); | 103 CleanupIdleSockets(true); |
| 103 } | 104 } |
| 104 | 105 |
| 105 bool ClientSocketPool::IdleSocket::ShouldCleanup(base::TimeTicks now) const { | 106 bool ClientSocketPool::IdleSocket::ShouldCleanup(base::TimeTicks now) const { |
| 106 bool timed_out = (now - start_time) >= | 107 bool timed_out = (now - start_time) >= |
| 107 base::TimeDelta::FromSeconds(kIdleTimeout); | 108 base::TimeDelta::FromSeconds(kIdleTimeout); |
| 108 return timed_out || !(*ptr)->IsConnected(); | 109 return timed_out || !(*ptr)->IsConnectedAndIdle(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 void ClientSocketPool::CleanupIdleSockets(bool force) { | 112 void ClientSocketPool::CleanupIdleSockets(bool force) { |
| 112 if (idle_socket_count_ == 0) | 113 if (idle_socket_count_ == 0) |
| 113 return; | 114 return; |
| 114 | 115 |
| 115 // Current time value. Retrieving it once at the function start rather than | 116 // Current time value. Retrieving it once at the function start rather than |
| 116 // inside the inner loop, since it shouldn't change by any meaningful amount. | 117 // inside the inner loop, since it shouldn't change by any meaningful amount. |
| 117 base::TimeTicks now = base::TimeTicks::Now(); | 118 base::TimeTicks now = base::TimeTicks::Now(); |
| 118 | 119 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 void ClientSocketPool::DoReleaseSocket(const std::string& group_name, | 156 void ClientSocketPool::DoReleaseSocket(const std::string& group_name, |
| 156 ClientSocketPtr* ptr) { | 157 ClientSocketPtr* ptr) { |
| 157 GroupMap::iterator i = group_map_.find(group_name); | 158 GroupMap::iterator i = group_map_.find(group_name); |
| 158 DCHECK(i != group_map_.end()); | 159 DCHECK(i != group_map_.end()); |
| 159 | 160 |
| 160 Group& group = i->second; | 161 Group& group = i->second; |
| 161 | 162 |
| 162 DCHECK(group.active_socket_count > 0); | 163 DCHECK(group.active_socket_count > 0); |
| 163 group.active_socket_count--; | 164 group.active_socket_count--; |
| 164 | 165 |
| 165 bool can_reuse = ptr->get() && (*ptr)->IsConnected(); | 166 bool can_reuse = ptr->get() && (*ptr)->IsConnectedAndIdle(); |
| 166 if (can_reuse) { | 167 if (can_reuse) { |
| 167 IdleSocket idle_socket; | 168 IdleSocket idle_socket; |
| 168 idle_socket.ptr = ptr; | 169 idle_socket.ptr = ptr; |
| 169 idle_socket.start_time = base::TimeTicks::Now(); | 170 idle_socket.start_time = base::TimeTicks::Now(); |
| 170 | 171 |
| 171 group.idle_sockets.push_back(idle_socket); | 172 group.idle_sockets.push_back(idle_socket); |
| 172 IncrementIdleCount(); | 173 IncrementIdleCount(); |
| 173 } else { | 174 } else { |
| 174 delete ptr; | 175 delete ptr; |
| 175 } | 176 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 186 | 187 |
| 187 // Delete group if no longer needed. | 188 // Delete group if no longer needed. |
| 188 if (group.active_socket_count == 0 && group.idle_sockets.empty()) { | 189 if (group.active_socket_count == 0 && group.idle_sockets.empty()) { |
| 189 DCHECK(group.pending_requests.empty()); | 190 DCHECK(group.pending_requests.empty()); |
| 190 group_map_.erase(i); | 191 group_map_.erase(i); |
| 191 } | 192 } |
| 192 } | 193 } |
| 193 | 194 |
| 194 } // namespace net | 195 } // namespace net |
| 195 | 196 |
| OLD | NEW |