1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/client_socket_pool.h"
#include "base/message_loop.h"
#include "net/base/client_socket.h"
#include "net/base/client_socket_handle.h"
#include "net/base/net_errors.h"
using base::TimeDelta;
namespace {
// The timeout value, in seconds, used to clean up idle sockets that can't be
// reused.
//
// Note: It's important to close idle sockets that have received data as soon
// as possible because the received data may cause BSOD on Windows XP under
// some conditions. See http://crbug.com/4606.
const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT.
// The maximum duration, in seconds, to keep idle persistent sockets alive.
const int kIdleTimeout = 300; // 5 minutes.
} // namespace
namespace net {
ClientSocketPool::ClientSocketPool(int max_sockets_per_group)
: idle_socket_count_(0),
max_sockets_per_group_(max_sockets_per_group) {
}
ClientSocketPool::~ClientSocketPool() {
// Clean up any idle sockets. Assert that we have no remaining active
// sockets or pending requests. They should have all been cleaned up prior
// to the manager being destroyed.
CloseIdleSockets();
DCHECK(group_map_.empty());
}
int ClientSocketPool::RequestSocket(ClientSocketHandle* handle,
CompletionCallback* callback) {
Group& group = group_map_[handle->group_name_];
// Can we make another active socket now?
if (group.active_socket_count == max_sockets_per_group_) {
Request r;
r.handle = handle;
DCHECK(callback);
r.callback = callback;
group.pending_requests.push_back(r);
return ERR_IO_PENDING;
}
// OK, we are going to activate one.
group.active_socket_count++;
// Use idle sockets in LIFO order because they're more likely to be
// still reusable.
while (!group.idle_sockets.empty()) {
IdleSocket idle_socket = group.idle_sockets.back();
group.idle_sockets.pop_back();
DecrementIdleCount();
if ((*idle_socket.ptr)->IsConnectedAndIdle()) {
// We found one we can reuse!
handle->socket_ = idle_socket.ptr;
return OK;
}
delete idle_socket.ptr;
}
handle->socket_ = new ClientSocketPtr();
return OK;
}
void ClientSocketPool::CancelRequest(ClientSocketHandle* handle) {
Group& group = group_map_[handle->group_name_];
// In order for us to be canceling a pending request, we must have active
// sockets equaling the limit. NOTE: The correctness of the code doesn't
// require this assertion.
DCHECK(group.active_socket_count == max_sockets_per_group_);
// Search pending_requests for matching handle.
std::deque<Request>::iterator it = group.pending_requests.begin();
for (; it != group.pending_requests.end(); ++it) {
if (it->handle == handle) {
group.pending_requests.erase(it);
break;
}
}
}
void ClientSocketPool::ReleaseSocket(ClientSocketHandle* handle) {
// Run this asynchronously to allow the caller to finish before we let
// another to begin doing work. This also avoids nasty recursion issues.
// NOTE: We cannot refer to the handle argument after this method returns.
MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
this, &ClientSocketPool::DoReleaseSocket, handle->group_name_,
handle->socket_));
}
void ClientSocketPool::CloseIdleSockets() {
CleanupIdleSockets(true);
}
bool ClientSocketPool::IdleSocket::ShouldCleanup(base::TimeTicks now) const {
bool timed_out = (now - start_time) >=
base::TimeDelta::FromSeconds(kIdleTimeout);
return timed_out || !(*ptr)->IsConnectedAndIdle();
}
void ClientSocketPool::CleanupIdleSockets(bool force) {
if (idle_socket_count_ == 0)
return;
// Current time value. Retrieving it once at the function start rather than
// inside the inner loop, since it shouldn't change by any meaningful amount.
base::TimeTicks now = base::TimeTicks::Now();
GroupMap::iterator i = group_map_.begin();
while (i != group_map_.end()) {
Group& group = i->second;
std::deque<IdleSocket>::iterator j = group.idle_sockets.begin();
while (j != group.idle_sockets.end()) {
if (force || j->ShouldCleanup(now)) {
delete j->ptr;
j = group.idle_sockets.erase(j);
DecrementIdleCount();
} else {
++j;
}
}
// Delete group if no longer needed.
if (group.active_socket_count == 0 && group.idle_sockets.empty()) {
DCHECK(group.pending_requests.empty());
group_map_.erase(i++);
} else {
++i;
}
}
}
void ClientSocketPool::IncrementIdleCount() {
if (++idle_socket_count_ == 1)
timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this,
&ClientSocketPool::OnCleanupTimerFired);
}
void ClientSocketPool::DecrementIdleCount() {
if (--idle_socket_count_ == 0)
timer_.Stop();
}
void ClientSocketPool::DoReleaseSocket(const std::string& group_name,
ClientSocketPtr* ptr) {
GroupMap::iterator i = group_map_.find(group_name);
DCHECK(i != group_map_.end());
Group& group = i->second;
DCHECK(group.active_socket_count > 0);
group.active_socket_count--;
bool can_reuse = ptr->get() && (*ptr)->IsConnectedAndIdle();
if (can_reuse) {
IdleSocket idle_socket;
idle_socket.ptr = ptr;
idle_socket.start_time = base::TimeTicks::Now();
group.idle_sockets.push_back(idle_socket);
IncrementIdleCount();
} else {
delete ptr;
}
// Process one pending request.
if (!group.pending_requests.empty()) {
Request r = group.pending_requests.front();
group.pending_requests.pop_front();
int rv = RequestSocket(r.handle, NULL);
DCHECK(rv == OK);
r.callback->Run(rv);
return;
}
// Delete group if no longer needed.
if (group.active_socket_count == 0 && group.idle_sockets.empty()) {
DCHECK(group.pending_requests.empty());
group_map_.erase(i);
}
}
} // namespace net
|