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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
|
// 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/socket/client_socket_pool_base.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/stl_util-inl.h"
#include "base/time.h"
#include "net/base/net_errors.h"
#include "net/socket/client_socket_handle.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 {
bool ClientSocketPoolBase::g_late_binding = false;
ConnectJob::ConnectJob(const std::string& group_name,
const ClientSocketHandle* key_handle,
base::TimeDelta timeout_duration,
Delegate* delegate)
: group_name_(group_name),
key_handle_(key_handle),
timeout_duration_(timeout_duration),
delegate_(delegate),
load_state_(LOAD_STATE_IDLE) {
DCHECK(!group_name.empty());
DCHECK(key_handle);
DCHECK(delegate);
}
ConnectJob::~ConnectJob() {}
int ConnectJob::Connect() {
if (timeout_duration_ != base::TimeDelta())
timer_.Start(timeout_duration_, this, &ConnectJob::OnTimeout);
return ConnectInternal();
}
void ConnectJob::OnTimeout() {
// Make sure the socket is NULL before calling into |delegate|.
set_socket(NULL);
// The delegate will delete |this|.
Delegate *delegate = delegate_;
delegate_ = NULL;
delegate->OnConnectJobComplete(ERR_TIMED_OUT, this);
}
ClientSocketPoolBase::ClientSocketPoolBase(
int max_sockets,
int max_sockets_per_group,
ConnectJobFactory* connect_job_factory)
: idle_socket_count_(0),
connecting_socket_count_(0),
handed_out_socket_count_(0),
max_sockets_(max_sockets),
max_sockets_per_group_(max_sockets_per_group),
may_have_stalled_group_(false),
connect_job_factory_(connect_job_factory) {
DCHECK_LE(0, max_sockets_per_group);
DCHECK_LE(max_sockets_per_group, max_sockets);
}
ClientSocketPoolBase::~ClientSocketPoolBase() {
if (g_late_binding)
CancelAllConnectJobs();
// 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());
DCHECK(connect_job_map_.empty());
}
// InsertRequestIntoQueue inserts the request into the queue based on
// priority. Highest priorities are closest to the front. Older requests are
// prioritized over requests of equal priority.
//
// static
void ClientSocketPoolBase::InsertRequestIntoQueue(
const Request& r, RequestQueue* pending_requests) {
RequestQueue::iterator it = pending_requests->begin();
while (it != pending_requests->end() && r.priority <= it->priority)
++it;
pending_requests->insert(it, r);
}
int ClientSocketPoolBase::RequestSocket(
const std::string& group_name,
const HostResolver::RequestInfo& resolve_info,
int priority,
ClientSocketHandle* handle,
CompletionCallback* callback) {
DCHECK(!resolve_info.hostname().empty());
DCHECK_GE(priority, 0);
DCHECK(callback);
Group& group = group_map_[group_name];
// Can we make another active socket now?
if (ReachedMaxSocketsLimit() ||
!group.HasAvailableSocketSlot(max_sockets_per_group_)) {
if (ReachedMaxSocketsLimit()) {
// We could check if we really have a stalled group here, but it requires
// a scan of all groups, so just flip a flag here, and do the check later.
may_have_stalled_group_ = true;
}
CHECK(callback);
Request r(handle, callback, priority, resolve_info);
InsertRequestIntoQueue(r, &group.pending_requests);
return ERR_IO_PENDING;
}
while (!group.idle_sockets.empty()) {
IdleSocket idle_socket = group.idle_sockets.back();
group.idle_sockets.pop_back();
DecrementIdleCount();
if (idle_socket.socket->IsConnectedAndIdle()) {
// We found one we can reuse!
HandOutSocket(idle_socket.socket, idle_socket.used, handle, &group);
return OK;
}
delete idle_socket.socket;
}
// We couldn't find a socket to reuse, so allocate and connect a new one.
CHECK(callback);
Request r(handle, callback, priority, resolve_info);
scoped_ptr<ConnectJob> connect_job(
connect_job_factory_->NewConnectJob(group_name, r, this));
int rv = connect_job->Connect();
if (rv == OK) {
HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
handle, &group);
} else if (rv == ERR_IO_PENDING) {
connecting_socket_count_++;
ConnectJob* job = connect_job.release();
if (g_late_binding) {
CHECK(!ContainsKey(connect_job_map_, handle));
InsertRequestIntoQueue(r, &group.pending_requests);
} else {
group.connecting_requests[handle] = r;
CHECK(!ContainsKey(connect_job_map_, handle));
connect_job_map_[handle] = job;
}
group.jobs.insert(job);
} else if (group.IsEmpty()) {
group_map_.erase(group_name);
}
return rv;
}
void ClientSocketPoolBase::CancelRequest(const std::string& group_name,
const ClientSocketHandle* handle) {
CHECK(ContainsKey(group_map_, group_name));
Group& group = group_map_[group_name];
// Search pending_requests for matching handle.
RequestQueue::iterator it = group.pending_requests.begin();
for (; it != group.pending_requests.end(); ++it) {
if (it->handle == handle) {
group.pending_requests.erase(it);
if (g_late_binding &&
group.jobs.size() > group.pending_requests.size() + 1) {
// TODO(willchan): Cancel the job in the earliest LoadState.
RemoveConnectJob(handle, *group.jobs.begin(), &group);
OnAvailableSocketSlot(group_name, &group);
}
return;
}
}
if (!g_late_binding) {
// It's invalid to cancel a non-existent request.
CHECK(ContainsKey(group.connecting_requests, handle));
RequestMap::iterator map_it = group.connecting_requests.find(handle);
if (map_it != group.connecting_requests.end()) {
RemoveConnectJob(handle, NULL, &group);
OnAvailableSocketSlot(group_name, &group);
}
}
}
void ClientSocketPoolBase::ReleaseSocket(const std::string& group_name,
ClientSocket* socket) {
// 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, &ClientSocketPoolBase::DoReleaseSocket, group_name, socket));
}
void ClientSocketPoolBase::CloseIdleSockets() {
CleanupIdleSockets(true);
}
int ClientSocketPoolBase::IdleSocketCountInGroup(
const std::string& group_name) const {
GroupMap::const_iterator i = group_map_.find(group_name);
CHECK(i != group_map_.end());
return i->second.idle_sockets.size();
}
LoadState ClientSocketPoolBase::GetLoadState(
const std::string& group_name,
const ClientSocketHandle* handle) const {
if (!ContainsKey(group_map_, group_name)) {
NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
<< " for handle: " << handle;
return LOAD_STATE_IDLE;
}
// Can't use operator[] since it is non-const.
const Group& group = group_map_.find(group_name)->second;
// Search connecting_requests for matching handle.
RequestMap::const_iterator map_it = group.connecting_requests.find(handle);
if (map_it != group.connecting_requests.end()) {
ConnectJobMap::const_iterator job_it = connect_job_map_.find(handle);
if (job_it == connect_job_map_.end()) {
NOTREACHED();
return LOAD_STATE_IDLE;
}
return job_it->second->load_state();
}
// Search pending_requests for matching handle.
RequestQueue::const_iterator it = group.pending_requests.begin();
for (size_t i = 0; it != group.pending_requests.end(); ++it, ++i) {
if (it->handle == handle) {
if (g_late_binding && i < group.jobs.size()) {
LoadState max_state = LOAD_STATE_IDLE;
for (ConnectJobSet::const_iterator job_it = group.jobs.begin();
job_it != group.jobs.end(); ++job_it) {
max_state = std::max(max_state, (*job_it)->load_state());
}
return max_state;
} else {
// TODO(wtc): Add a state for being on the wait list.
// See http://www.crbug.com/5077.
return LOAD_STATE_IDLE;
}
}
}
NOTREACHED();
return LOAD_STATE_IDLE;
}
bool ClientSocketPoolBase::IdleSocket::ShouldCleanup(
base::TimeTicks now) const {
bool timed_out = (now - start_time) >=
base::TimeDelta::FromSeconds(kIdleTimeout);
return timed_out ||
!(used ? socket->IsConnectedAndIdle() : socket->IsConnected());
}
void ClientSocketPoolBase::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->socket;
j = group.idle_sockets.erase(j);
DecrementIdleCount();
} else {
++j;
}
}
// Delete group if no longer needed.
if (group.IsEmpty()) {
group_map_.erase(i++);
} else {
++i;
}
}
}
void ClientSocketPoolBase::IncrementIdleCount() {
if (++idle_socket_count_ == 1)
timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this,
&ClientSocketPoolBase::OnCleanupTimerFired);
}
void ClientSocketPoolBase::DecrementIdleCount() {
if (--idle_socket_count_ == 0)
timer_.Stop();
}
void ClientSocketPoolBase::DoReleaseSocket(const std::string& group_name,
ClientSocket* socket) {
GroupMap::iterator i = group_map_.find(group_name);
CHECK(i != group_map_.end());
Group& group = i->second;
CHECK(handed_out_socket_count_ > 0);
handed_out_socket_count_--;
CHECK(group.active_socket_count > 0);
group.active_socket_count--;
const bool can_reuse = socket->IsConnectedAndIdle();
if (can_reuse) {
AddIdleSocket(socket, true /* used socket */, &group);
} else {
delete socket;
}
OnAvailableSocketSlot(group_name, &group);
}
// Search for the highest priority pending request, amongst the groups that
// are not at the |max_sockets_per_group_| limit. Note: for requests with
// the same priority, the winner is based on group hash ordering (and not
// insertion order).
int ClientSocketPoolBase::FindTopStalledGroup(Group** group,
std::string* group_name) {
Group* top_group = NULL;
const std::string* top_group_name = NULL;
int stalled_group_count = 0;
for (GroupMap::iterator i = group_map_.begin();
i != group_map_.end(); ++i) {
Group& group = i->second;
const RequestQueue& queue = group.pending_requests;
if (queue.empty())
continue;
bool has_slot = group.HasAvailableSocketSlot(max_sockets_per_group_);
if (has_slot)
stalled_group_count++;
bool has_higher_priority = !top_group ||
group.TopPendingPriority() > top_group->TopPendingPriority();
if (has_slot && has_higher_priority) {
top_group = &group;
top_group_name = &i->first;
}
}
if (top_group) {
*group = top_group;
*group_name = *top_group_name;
}
return stalled_group_count;
}
void ClientSocketPoolBase::OnConnectJobComplete(int result, ConnectJob* job) {
DCHECK_NE(ERR_IO_PENDING, result);
const std::string group_name = job->group_name();
GroupMap::iterator group_it = group_map_.find(group_name);
CHECK(group_it != group_map_.end());
Group& group = group_it->second;
const ClientSocketHandle* const key_handle = job->key_handle();
scoped_ptr<ClientSocket> socket(job->ReleaseSocket());
if (g_late_binding) {
RemoveConnectJob(key_handle, job, &group);
if (result == OK) {
DCHECK(socket.get());
if (!group.pending_requests.empty()) {
Request r = group.pending_requests.front();
group.pending_requests.pop_front();
HandOutSocket(
socket.release(), false /* unused socket */, r.handle, &group);
r.callback->Run(result);
} else {
AddIdleSocket(socket.release(), false /* unused socket */, &group);
OnAvailableSocketSlot(group_name, &group);
}
} else {
DCHECK(!socket.get());
if (!group.pending_requests.empty()) {
Request r = group.pending_requests.front();
group.pending_requests.pop_front();
r.callback->Run(result);
}
MaybeOnAvailableSocketSlot(group_name);
}
return;
}
RequestMap* request_map = &group.connecting_requests;
RequestMap::iterator it = request_map->find(key_handle);
CHECK(it != request_map->end());
ClientSocketHandle* const handle = it->second.handle;
CompletionCallback* const callback = it->second.callback;
RemoveConnectJob(key_handle, job, &group);
if (result != OK) {
DCHECK(!socket.get());
callback->Run(result); // |group| is not necessarily valid after this.
// |group| may be invalid after the callback, we need to search
// |group_map_| again.
MaybeOnAvailableSocketSlot(group_name);
} else {
DCHECK(socket.get());
HandOutSocket(socket.release(), false /* not reused */, handle, &group);
callback->Run(result);
}
}
void ClientSocketPoolBase::EnableLateBindingOfSockets(bool enabled) {
g_late_binding = enabled;
}
void ClientSocketPoolBase::RemoveConnectJob(
const ClientSocketHandle* handle, const ConnectJob *job, Group* group) {
CHECK(connecting_socket_count_ > 0);
connecting_socket_count_--;
if (g_late_binding) {
DCHECK(job);
delete job;
} else {
ConnectJobMap::iterator it = connect_job_map_.find(handle);
CHECK(it != connect_job_map_.end());
job = it->second;
delete job;
connect_job_map_.erase(it);
group->connecting_requests.erase(handle);
}
if (group) {
DCHECK(ContainsKey(group->jobs, job));
group->jobs.erase(job);
}
}
void ClientSocketPoolBase::MaybeOnAvailableSocketSlot(
const std::string& group_name) {
GroupMap::iterator it = group_map_.find(group_name);
if (it != group_map_.end()) {
Group& group = it->second;
if (group.HasAvailableSocketSlot(max_sockets_per_group_))
OnAvailableSocketSlot(group_name, &group);
}
}
void ClientSocketPoolBase::OnAvailableSocketSlot(const std::string& group_name,
Group* group) {
if (may_have_stalled_group_) {
std::string top_group_name;
Group* top_group;
int stalled_group_count = FindTopStalledGroup(&top_group, &top_group_name);
if (stalled_group_count <= 1)
may_have_stalled_group_ = false;
if (stalled_group_count >= 1)
ProcessPendingRequest(top_group_name, top_group);
} else if (!group->pending_requests.empty()) {
ProcessPendingRequest(group_name, group);
// |group| may no longer be valid after this point. Be careful not to
// access it again.
} else if (group->IsEmpty()) {
// Delete |group| if no longer needed. |group| will no longer be valid.
group_map_.erase(group_name);
}
}
void ClientSocketPoolBase::ProcessPendingRequest(const std::string& group_name,
Group* group) {
Request r = group->pending_requests.front();
group->pending_requests.pop_front();
int rv = RequestSocket(
group_name, r.resolve_info, r.priority, r.handle, r.callback);
if (rv != ERR_IO_PENDING) {
r.callback->Run(rv);
if (rv != OK) {
// |group| may be invalid after the callback, we need to search
// |group_map_| again.
MaybeOnAvailableSocketSlot(group_name);
}
}
}
void ClientSocketPoolBase::HandOutSocket(
ClientSocket* socket,
bool reused,
ClientSocketHandle* handle,
Group* group) {
DCHECK(socket);
handle->set_socket(socket);
handle->set_is_reused(reused);
handed_out_socket_count_++;
group->active_socket_count++;
}
void ClientSocketPoolBase::AddIdleSocket(
ClientSocket* socket, bool used, Group* group) {
DCHECK(socket);
IdleSocket idle_socket;
idle_socket.socket = socket;
idle_socket.start_time = base::TimeTicks::Now();
idle_socket.used = used;
group->idle_sockets.push_back(idle_socket);
IncrementIdleCount();
}
void ClientSocketPoolBase::CancelAllConnectJobs() {
for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
Group& group = i->second;
STLDeleteElements(&group.jobs);
// Delete group if no longer needed.
if (group.IsEmpty()) {
group_map_.erase(i++);
} else {
++i;
}
}
}
bool ClientSocketPoolBase::ReachedMaxSocketsLimit() const {
// Each connecting socket will eventually connect and be handed out.
int total = handed_out_socket_count_ + connecting_socket_count_;
DCHECK_LE(total, max_sockets_);
return total == max_sockets_;
}
} // namespace net
|