forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_socket_pool_base.cc
1466 lines (1262 loc) · 49.1 KB
/
client_socket_pool_base.cc
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 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 <algorithm>
#include <utility>
#include "base/compiler_specific.h"
#include "base/format_macros.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event.h"
#include "base/values.h"
#include "net/base/net_errors.h"
#include "net/base/trace_constants.h"
#include "net/log/net_log.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_source.h"
using base::TimeDelta;
namespace net {
namespace {
// Indicate whether or not we should establish a new transport layer connection
// after a certain timeout has passed without receiving an ACK.
bool g_connect_backup_jobs_enabled = true;
void SetSocketMotivation(StreamSocket* socket,
HttpRequestInfo::RequestMotivation motivation) {
if (motivation == HttpRequestInfo::PRECONNECT_MOTIVATED)
socket->SetSubresourceSpeculation();
else if (motivation == HttpRequestInfo::OMNIBOX_MOTIVATED)
socket->SetOmniboxSpeculation();
// TODO(mbelshe): Add other motivations (like EARLY_LOAD_MOTIVATED).
}
} // namespace
ConnectJob::ConnectJob(const std::string& group_name,
base::TimeDelta timeout_duration,
RequestPriority priority,
const SocketTag& socket_tag,
ClientSocketPool::RespectLimits respect_limits,
Delegate* delegate,
const NetLogWithSource& net_log)
: group_name_(group_name),
timeout_duration_(timeout_duration),
priority_(priority),
socket_tag_(socket_tag),
respect_limits_(respect_limits),
delegate_(delegate),
net_log_(net_log),
motivation_(HttpRequestInfo::NORMAL_MOTIVATION),
idle_(true) {
DCHECK(!group_name.empty());
DCHECK(delegate);
net_log.BeginEvent(NetLogEventType::SOCKET_POOL_CONNECT_JOB,
NetLog::StringCallback("group_name", &group_name_));
}
ConnectJob::~ConnectJob() {
net_log().EndEvent(NetLogEventType::SOCKET_POOL_CONNECT_JOB);
}
std::unique_ptr<StreamSocket> ConnectJob::PassSocket() {
return std::move(socket_);
}
int ConnectJob::Connect() {
if (!timeout_duration_.is_zero())
timer_.Start(FROM_HERE, timeout_duration_, this, &ConnectJob::OnTimeout);
idle_ = false;
LogConnectStart();
int rv = ConnectInternal();
if (rv != ERR_IO_PENDING) {
LogConnectCompletion(rv);
delegate_ = NULL;
}
return rv;
}
void ConnectJob::SetSocket(std::unique_ptr<StreamSocket> socket) {
if (socket) {
net_log().AddEvent(NetLogEventType::CONNECT_JOB_SET_SOCKET,
socket->NetLog().source().ToEventParametersCallback());
}
socket_ = std::move(socket);
}
void ConnectJob::NotifyDelegateOfCompletion(int rv) {
TRACE_EVENT0(kNetTracingCategory, "ConnectJob::NotifyDelegateOfCompletion");
// The delegate will own |this|.
Delegate* delegate = delegate_;
delegate_ = NULL;
if (socket_)
SetSocketMotivation(socket_.get(), motivation_);
LogConnectCompletion(rv);
delegate->OnConnectJobComplete(rv, this);
}
void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
timer_.Stop();
timer_.Start(FROM_HERE, remaining_time, this, &ConnectJob::OnTimeout);
}
void ConnectJob::LogConnectStart() {
connect_timing_.connect_start = base::TimeTicks::Now();
net_log().BeginEvent(NetLogEventType::SOCKET_POOL_CONNECT_JOB_CONNECT);
}
void ConnectJob::LogConnectCompletion(int net_error) {
connect_timing_.connect_end = base::TimeTicks::Now();
net_log().EndEventWithNetErrorCode(
NetLogEventType::SOCKET_POOL_CONNECT_JOB_CONNECT, net_error);
}
void ConnectJob::OnTimeout() {
// Make sure the socket is NULL before calling into |delegate|.
SetSocket(std::unique_ptr<StreamSocket>());
net_log_.AddEvent(NetLogEventType::SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
NotifyDelegateOfCompletion(ERR_TIMED_OUT);
}
namespace internal {
ClientSocketPoolBaseHelper::Request::Request(
ClientSocketHandle* handle,
const CompletionCallback& callback,
RequestPriority priority,
const SocketTag& socket_tag,
ClientSocketPool::RespectLimits respect_limits,
Flags flags,
const NetLogWithSource& net_log)
: handle_(handle),
callback_(callback),
priority_(priority),
respect_limits_(respect_limits),
flags_(flags),
net_log_(net_log),
socket_tag_(socket_tag) {
if (respect_limits_ == ClientSocketPool::RespectLimits::DISABLED)
DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
}
ClientSocketPoolBaseHelper::Request::~Request() {
liveness_ = DEAD;
}
void ClientSocketPoolBaseHelper::Request::CrashIfInvalid() const {
CHECK_EQ(liveness_, ALIVE);
}
ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
HigherLayeredPool* pool,
int max_sockets,
int max_sockets_per_group,
base::TimeDelta unused_idle_socket_timeout,
base::TimeDelta used_idle_socket_timeout,
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),
unused_idle_socket_timeout_(unused_idle_socket_timeout),
used_idle_socket_timeout_(used_idle_socket_timeout),
connect_job_factory_(connect_job_factory),
connect_backup_jobs_enabled_(false),
pool_generation_number_(0),
pool_(pool),
weak_factory_(this) {
DCHECK_LE(0, max_sockets_per_group);
DCHECK_LE(max_sockets_per_group, max_sockets);
NetworkChangeNotifier::AddIPAddressObserver(this);
}
ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
// Clean up any idle sockets and pending connect jobs. Assert that we have no
// remaining active sockets or pending requests. They should have all been
// cleaned up prior to |this| being destroyed.
FlushWithError(ERR_ABORTED);
DCHECK(group_map_.empty());
DCHECK(pending_callback_map_.empty());
DCHECK_EQ(0, connecting_socket_count_);
CHECK(higher_pools_.empty());
NetworkChangeNotifier::RemoveIPAddressObserver(this);
// Remove from lower layer pools.
for (std::set<LowerLayeredPool*>::iterator it = lower_pools_.begin();
it != lower_pools_.end();
++it) {
(*it)->RemoveHigherLayeredPool(pool_);
}
}
ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair()
: result(OK) {
}
ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair(
const CompletionCallback& callback_in, int result_in)
: callback(callback_in),
result(result_in) {
}
ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair(
const CallbackResultPair& other) = default;
ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() = default;
bool ClientSocketPoolBaseHelper::IsStalled() const {
// If a lower layer pool is stalled, consider |this| stalled as well.
for (std::set<LowerLayeredPool*>::const_iterator it = lower_pools_.begin();
it != lower_pools_.end();
++it) {
if ((*it)->IsStalled())
return true;
}
// If fewer than |max_sockets_| are in use, then clearly |this| is not
// stalled.
if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
return false;
// So in order to be stalled, |this| must be using at least |max_sockets_| AND
// |this| must have a request that is actually stalled on the global socket
// limit. To find such a request, look for a group that has more requests
// than jobs AND where the number of sockets is less than
// |max_sockets_per_group_|. (If the number of sockets is equal to
// |max_sockets_per_group_|, then the request is stalled on the group limit,
// which does not count.)
for (GroupMap::const_iterator it = group_map_.begin();
it != group_map_.end(); ++it) {
if (it->second->CanUseAdditionalSocketSlot(max_sockets_per_group_))
return true;
}
return false;
}
void ClientSocketPoolBaseHelper::AddLowerLayeredPool(
LowerLayeredPool* lower_pool) {
DCHECK(pool_);
CHECK(!base::ContainsKey(lower_pools_, lower_pool));
lower_pools_.insert(lower_pool);
lower_pool->AddHigherLayeredPool(pool_);
}
void ClientSocketPoolBaseHelper::AddHigherLayeredPool(
HigherLayeredPool* higher_pool) {
CHECK(higher_pool);
CHECK(!base::ContainsKey(higher_pools_, higher_pool));
higher_pools_.insert(higher_pool);
}
void ClientSocketPoolBaseHelper::RemoveHigherLayeredPool(
HigherLayeredPool* higher_pool) {
CHECK(higher_pool);
CHECK(base::ContainsKey(higher_pools_, higher_pool));
higher_pools_.erase(higher_pool);
}
int ClientSocketPoolBaseHelper::RequestSocket(
const std::string& group_name,
std::unique_ptr<Request> request) {
CHECK(!request->callback().is_null());
CHECK(request->handle());
// Cleanup any timed-out idle sockets.
CleanupIdleSockets(false);
request->net_log().BeginEvent(NetLogEventType::SOCKET_POOL);
Group* group = GetOrCreateGroup(group_name);
int rv = RequestSocketInternal(group_name, *request,
HttpRequestInfo::NORMAL_MOTIVATION);
if (rv != ERR_IO_PENDING) {
if (rv == OK) {
request->handle()->socket()->ApplySocketTag(request->socket_tag());
}
request->net_log().EndEventWithNetErrorCode(NetLogEventType::SOCKET_POOL,
rv);
CHECK(!request->handle()->is_initialized());
request.reset();
} else {
group->InsertPendingRequest(std::move(request));
// Have to do this asynchronously, as closing sockets in higher level pools
// call back in to |this|, which will cause all sorts of fun and exciting
// re-entrancy issues if the socket pool is doing something else at the
// time.
if (group->CanUseAdditionalSocketSlot(max_sockets_per_group_)) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(
&ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools,
weak_factory_.GetWeakPtr()));
}
}
return rv;
}
void ClientSocketPoolBaseHelper::RequestSockets(
const std::string& group_name,
const Request& request,
int num_sockets,
HttpRequestInfo::RequestMotivation motivation) {
DCHECK(request.callback().is_null());
DCHECK(!request.handle());
// Cleanup any timed-out idle sockets.
CleanupIdleSockets(false);
if (num_sockets > max_sockets_per_group_) {
num_sockets = max_sockets_per_group_;
}
request.net_log().BeginEvent(
NetLogEventType::SOCKET_POOL_CONNECTING_N_SOCKETS,
NetLog::IntCallback("num_sockets", num_sockets));
Group* group = GetOrCreateGroup(group_name);
// RequestSocketsInternal() may delete the group.
bool deleted_group = false;
int rv = OK;
for (int num_iterations_left = num_sockets;
group->NumActiveSocketSlots() < num_sockets &&
num_iterations_left > 0 ; num_iterations_left--) {
rv = RequestSocketInternal(group_name, request, motivation);
if (rv < 0 && rv != ERR_IO_PENDING) {
// We're encountering a synchronous error. Give up.
if (!base::ContainsKey(group_map_, group_name))
deleted_group = true;
break;
}
if (!base::ContainsKey(group_map_, group_name)) {
// Unexpected. The group should only be getting deleted on synchronous
// error.
NOTREACHED();
deleted_group = true;
break;
}
}
if (!deleted_group && group->IsEmpty())
RemoveGroup(group_name);
if (rv == ERR_IO_PENDING)
rv = OK;
request.net_log().EndEventWithNetErrorCode(
NetLogEventType::SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
}
int ClientSocketPoolBaseHelper::RequestSocketInternal(
const std::string& group_name,
const Request& request,
HttpRequestInfo::RequestMotivation motivation) {
ClientSocketHandle* const handle = request.handle();
const bool preconnecting = !handle;
Group* group = GetOrCreateGroup(group_name);
if (!(request.flags() & NO_IDLE_SOCKETS)) {
// Try to reuse a socket.
if (AssignIdleSocketToRequest(request, group))
return OK;
}
// If there are more ConnectJobs than pending requests, don't need to do
// anything. Can just wait for the extra job to connect, and then assign it
// to the request.
if (!preconnecting && group->TryToUseUnassignedConnectJob())
return ERR_IO_PENDING;
// Can we make another active socket now?
if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
request.respect_limits() == ClientSocketPool::RespectLimits::ENABLED) {
// TODO(willchan): Consider whether or not we need to close a socket in a
// higher layered group. I don't think this makes sense since we would just
// reuse that socket then if we needed one and wouldn't make it down to this
// layer.
request.net_log().AddEvent(
NetLogEventType::SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
return ERR_IO_PENDING;
}
if (ReachedMaxSocketsLimit() &&
request.respect_limits() == ClientSocketPool::RespectLimits::ENABLED) {
// NOTE(mmenke): Wonder if we really need different code for each case
// here. Only reason for them now seems to be preconnects.
if (idle_socket_count() > 0) {
// There's an idle socket in this pool. Either that's because there's
// still one in this group, but we got here due to preconnecting bypassing
// idle sockets, or because there's an idle socket in another group.
bool closed = CloseOneIdleSocketExceptInGroup(group);
if (preconnecting && !closed)
return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
} else {
// 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.
request.net_log().AddEvent(
NetLogEventType::SOCKET_POOL_STALLED_MAX_SOCKETS);
return ERR_IO_PENDING;
}
}
// We couldn't find a socket to reuse, and there's space to allocate one,
// so allocate and connect a new one.
std::unique_ptr<ConnectJob> connect_job(
connect_job_factory_->NewConnectJob(group_name, request, this));
connect_job->set_motivation(motivation);
int rv = connect_job->Connect();
if (rv == OK) {
LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
if (!preconnecting) {
HandOutSocket(connect_job->PassSocket(), ClientSocketHandle::UNUSED,
connect_job->connect_timing(), handle, base::TimeDelta(),
group, request.net_log());
} else {
AddIdleSocket(connect_job->PassSocket(), group);
}
} else if (rv == ERR_IO_PENDING) {
// If we don't have any sockets in this group, set a timer for potentially
// creating a new one. If the SYN is lost, this backup socket may complete
// before the slow socket, improving end user latency.
if (connect_backup_jobs_enabled_ && group->IsEmpty()) {
group->StartBackupJobTimer(group_name, this);
}
connecting_socket_count_++;
group->AddJob(std::move(connect_job), preconnecting);
} else {
LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
std::unique_ptr<StreamSocket> error_socket;
if (!preconnecting) {
DCHECK(handle);
connect_job->GetAdditionalErrorState(handle);
error_socket = connect_job->PassSocket();
}
if (error_socket) {
HandOutSocket(std::move(error_socket), ClientSocketHandle::UNUSED,
connect_job->connect_timing(), handle, base::TimeDelta(),
group, request.net_log());
} else if (group->IsEmpty()) {
RemoveGroup(group_name);
}
}
return rv;
}
bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest(
const Request& request, Group* group) {
std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
// Iterate through the idle sockets forwards (oldest to newest)
// * Delete any disconnected ones.
// * If we find a used idle socket, assign to |idle_socket|. At the end,
// the |idle_socket_it| will be set to the newest used idle socket.
for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
it != idle_sockets->end();) {
// Check whether socket is usable. Note that it's unlikely that the socket
// is not usuable because this function is always invoked after a
// reusability check, but in theory socket can be closed asynchronously.
if (!it->IsUsable()) {
DecrementIdleCount();
delete it->socket;
it = idle_sockets->erase(it);
continue;
}
if (it->socket->WasEverUsed()) {
// We found one we can reuse!
idle_socket_it = it;
}
++it;
}
// If we haven't found an idle socket, that means there are no used idle
// sockets. Pick the oldest (first) idle socket (FIFO).
if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
idle_socket_it = idle_sockets->begin();
if (idle_socket_it != idle_sockets->end()) {
DecrementIdleCount();
base::TimeDelta idle_time =
base::TimeTicks::Now() - idle_socket_it->start_time;
IdleSocket idle_socket = *idle_socket_it;
idle_sockets->erase(idle_socket_it);
// TODO(davidben): If |idle_time| is under some low watermark, consider
// treating as UNUSED rather than UNUSED_IDLE. This will avoid
// HttpNetworkTransaction retrying on some errors.
ClientSocketHandle::SocketReuseType reuse_type =
idle_socket.socket->WasEverUsed() ?
ClientSocketHandle::REUSED_IDLE :
ClientSocketHandle::UNUSED_IDLE;
// If this socket took multiple attempts to obtain, don't report those
// every time it's reused, just to the first user.
if (idle_socket.socket->WasEverUsed())
idle_socket.socket->ClearConnectionAttempts();
HandOutSocket(std::unique_ptr<StreamSocket>(idle_socket.socket), reuse_type,
LoadTimingInfo::ConnectTiming(), request.handle(), idle_time,
group, request.net_log());
return true;
}
return false;
}
// static
void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
const NetLogSource& connect_job_source,
const Request& request) {
request.net_log().AddEvent(NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB,
connect_job_source.ToEventParametersCallback());
}
void ClientSocketPoolBaseHelper::SetPriority(const std::string& group_name,
ClientSocketHandle* handle,
RequestPriority priority) {
GroupMap::iterator group_it = group_map_.find(group_name);
if (group_it == group_map_.end()) {
DCHECK(base::ContainsKey(pending_callback_map_, handle));
// The Request has already completed and been destroyed; nothing to
// reprioritize.
return;
}
group_it->second->SetPriority(handle, priority);
}
void ClientSocketPoolBaseHelper::CancelRequest(
const std::string& group_name, ClientSocketHandle* handle) {
PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
if (callback_it != pending_callback_map_.end()) {
int result = callback_it->second.result;
pending_callback_map_.erase(callback_it);
std::unique_ptr<StreamSocket> socket = handle->PassSocket();
if (socket) {
if (result != OK)
socket->Disconnect();
ReleaseSocket(handle->group_name(), std::move(socket), handle->id());
}
return;
}
CHECK(base::ContainsKey(group_map_, group_name));
Group* group = GetOrCreateGroup(group_name);
// Search pending_requests for matching handle.
std::unique_ptr<Request> request = group->FindAndRemovePendingRequest(handle);
if (request) {
request->net_log().AddEvent(NetLogEventType::CANCELLED);
request->net_log().EndEvent(NetLogEventType::SOCKET_POOL);
// We let the job run, unless we're at the socket limit and there is
// not another request waiting on the job.
if (group->jobs().size() > group->pending_request_count() &&
ReachedMaxSocketsLimit()) {
RemoveConnectJob(group->jobs().begin()->get(), group);
CheckForStalledSocketGroups();
}
}
}
bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
return base::ContainsKey(group_map_, group_name);
}
void ClientSocketPoolBaseHelper::CloseIdleSockets() {
CleanupIdleSockets(true);
DCHECK_EQ(0, idle_socket_count_);
}
void ClientSocketPoolBaseHelper::CloseIdleSocketsInGroup(
const std::string& group_name) {
if (idle_socket_count_ == 0)
return;
GroupMap::iterator it = group_map_.find(group_name);
if (it == group_map_.end())
return;
CleanupIdleSocketsInGroup(true, it->second, base::TimeTicks::Now());
if (it->second->IsEmpty())
RemoveGroup(it);
}
int ClientSocketPoolBaseHelper::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 ClientSocketPoolBaseHelper::GetLoadState(
const std::string& group_name,
const ClientSocketHandle* handle) const {
if (base::ContainsKey(pending_callback_map_, handle))
return LOAD_STATE_CONNECTING;
GroupMap::const_iterator group_it = group_map_.find(group_name);
if (group_it == group_map_.end()) {
// TODO(mmenke): This is actually reached in the wild, for unknown reasons.
// Would be great to understand why, and if it's a bug, fix it. If not,
// should have a test for that case.
NOTREACHED();
return LOAD_STATE_IDLE;
}
const Group& group = *group_it->second;
if (group.HasConnectJobForHandle(handle)) {
// Just return the state of the oldest ConnectJob.
return (*group.jobs().begin())->GetLoadState();
}
if (group.CanUseAdditionalSocketSlot(max_sockets_per_group_))
return LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL;
return LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET;
}
std::unique_ptr<base::DictionaryValue>
ClientSocketPoolBaseHelper::GetInfoAsValue(const std::string& name,
const std::string& type) const {
auto dict = std::make_unique<base::DictionaryValue>();
dict->SetString("name", name);
dict->SetString("type", type);
dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
dict->SetInteger("connecting_socket_count", connecting_socket_count_);
dict->SetInteger("idle_socket_count", idle_socket_count_);
dict->SetInteger("max_socket_count", max_sockets_);
dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
dict->SetInteger("pool_generation_number", pool_generation_number_);
if (group_map_.empty())
return dict;
auto all_groups_dict = std::make_unique<base::DictionaryValue>();
for (GroupMap::const_iterator it = group_map_.begin();
it != group_map_.end(); it++) {
const Group* group = it->second;
auto group_dict = std::make_unique<base::DictionaryValue>();
group_dict->SetInteger("pending_request_count",
group->pending_request_count());
if (group->has_pending_requests()) {
group_dict->SetString(
"top_pending_priority",
RequestPriorityToString(group->TopPendingPriority()));
}
group_dict->SetInteger("active_socket_count", group->active_socket_count());
auto idle_socket_list = std::make_unique<base::ListValue>();
std::list<IdleSocket>::const_iterator idle_socket;
for (idle_socket = group->idle_sockets().begin();
idle_socket != group->idle_sockets().end();
idle_socket++) {
int source_id = idle_socket->socket->NetLog().source().id;
idle_socket_list->AppendInteger(source_id);
}
group_dict->Set("idle_sockets", std::move(idle_socket_list));
auto connect_jobs_list = std::make_unique<base::ListValue>();
for (auto job = group->jobs().begin(); job != group->jobs().end(); job++) {
int source_id = (*job)->net_log().source().id;
connect_jobs_list->AppendInteger(source_id);
}
group_dict->Set("connect_jobs", std::move(connect_jobs_list));
group_dict->SetBoolean("is_stalled", group->CanUseAdditionalSocketSlot(
max_sockets_per_group_));
group_dict->SetBoolean("backup_job_timer_is_running",
group->BackupJobTimerIsRunning());
all_groups_dict->SetWithoutPathExpansion(it->first, std::move(group_dict));
}
dict->Set("groups", std::move(all_groups_dict));
return dict;
}
void ClientSocketPoolBaseHelper::DumpMemoryStats(
base::trace_event::ProcessMemoryDump* pmd,
const std::string& parent_dump_absolute_name) const {
size_t socket_count = 0;
size_t total_size = 0;
size_t buffer_size = 0;
size_t cert_count = 0;
size_t cert_size = 0;
for (const auto& kv : group_map_) {
for (const auto& socket : kv.second->idle_sockets()) {
StreamSocket::SocketMemoryStats stats;
socket.socket->DumpMemoryStats(&stats);
total_size += stats.total_size;
buffer_size += stats.buffer_size;
cert_count += stats.cert_count;
cert_size += stats.cert_size;
++socket_count;
}
}
// Only create a MemoryAllocatorDump if there is at least one idle socket
if (socket_count > 0) {
base::trace_event::MemoryAllocatorDump* socket_pool_dump =
pmd->CreateAllocatorDump(base::StringPrintf(
"%s/socket_pool", parent_dump_absolute_name.c_str()));
socket_pool_dump->AddScalar(
base::trace_event::MemoryAllocatorDump::kNameSize,
base::trace_event::MemoryAllocatorDump::kUnitsBytes, total_size);
socket_pool_dump->AddScalar(
base::trace_event::MemoryAllocatorDump::kNameObjectCount,
base::trace_event::MemoryAllocatorDump::kUnitsObjects, socket_count);
socket_pool_dump->AddScalar(
"buffer_size", base::trace_event::MemoryAllocatorDump::kUnitsBytes,
buffer_size);
socket_pool_dump->AddScalar(
"cert_count", base::trace_event::MemoryAllocatorDump::kUnitsObjects,
cert_count);
socket_pool_dump->AddScalar(
"cert_size", base::trace_event::MemoryAllocatorDump::kUnitsBytes,
cert_size);
}
}
bool ClientSocketPoolBaseHelper::IdleSocket::IsUsable() const {
if (socket->WasEverUsed())
return socket->IsConnectedAndIdle();
return socket->IsConnected();
}
void ClientSocketPoolBaseHelper::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();
for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
Group* group = i->second;
CleanupIdleSocketsInGroup(force, group, now);
// Delete group if no longer needed.
if (group->IsEmpty()) {
GroupMap::iterator old = i++;
RemoveGroup(old);
} else {
++i;
}
}
}
void ClientSocketPoolBaseHelper::CleanupIdleSocketsInGroup(
bool force,
Group* group,
const base::TimeTicks& now) {
auto idle_socket_it = group->mutable_idle_sockets()->begin();
while (idle_socket_it != group->idle_sockets().end()) {
base::TimeDelta timeout = idle_socket_it->socket->WasEverUsed()
? used_idle_socket_timeout_
: unused_idle_socket_timeout_;
bool timed_out = (now - idle_socket_it->start_time) >= timeout;
bool should_clean_up = force || timed_out || !idle_socket_it->IsUsable();
if (should_clean_up) {
delete idle_socket_it->socket;
idle_socket_it = group->mutable_idle_sockets()->erase(idle_socket_it);
DecrementIdleCount();
} else {
++idle_socket_it;
}
}
}
ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
const std::string& group_name) {
GroupMap::iterator it = group_map_.find(group_name);
if (it != group_map_.end())
return it->second;
Group* group = new Group;
group_map_[group_name] = group;
return group;
}
void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
GroupMap::iterator it = group_map_.find(group_name);
CHECK(it != group_map_.end());
RemoveGroup(it);
}
void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
delete it->second;
group_map_.erase(it);
}
// static
bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
return g_connect_backup_jobs_enabled;
}
// static
bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
bool old_value = g_connect_backup_jobs_enabled;
g_connect_backup_jobs_enabled = enabled;
return old_value;
}
void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
}
void ClientSocketPoolBaseHelper::IncrementIdleCount() {
++idle_socket_count_;
}
void ClientSocketPoolBaseHelper::DecrementIdleCount() {
--idle_socket_count_;
}
void ClientSocketPoolBaseHelper::ReleaseSocket(
const std::string& group_name,
std::unique_ptr<StreamSocket> socket,
int id) {
GroupMap::iterator i = group_map_.find(group_name);
CHECK(i != group_map_.end());
Group* group = i->second;
CHECK_GT(handed_out_socket_count_, 0);
handed_out_socket_count_--;
CHECK_GT(group->active_socket_count(), 0);
group->DecrementActiveSocketCount();
const bool can_reuse = socket->IsConnectedAndIdle() &&
id == pool_generation_number_;
if (can_reuse) {
// Add it to the idle list.
AddIdleSocket(std::move(socket), group);
OnAvailableSocketSlot(group_name, group);
} else {
socket.reset();
}
CheckForStalledSocketGroups();
}
void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
// Loop until there's nothing more to do.
while (true) {
// If we have idle sockets, see if we can give one to the top-stalled group.
std::string top_group_name;
Group* top_group = NULL;
if (!FindTopStalledGroup(&top_group, &top_group_name)) {
// There may still be a stalled group in a lower level pool.
for (std::set<LowerLayeredPool*>::iterator it = lower_pools_.begin();
it != lower_pools_.end(); ++it) {
if ((*it)->IsStalled()) {
CloseOneIdleSocket();
break;
}
}
return;
}
if (ReachedMaxSocketsLimit()) {
if (idle_socket_count() > 0) {
CloseOneIdleSocket();
} else {
// We can't activate more sockets since we're already at our global
// limit.
return;
}
}
// Note that this may delete top_group.
OnAvailableSocketSlot(top_group_name, top_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).
bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
Group** group,
std::string* group_name) const {
CHECK((group && group_name) || (!group && !group_name));
Group* top_group = NULL;
const std::string* top_group_name = NULL;
bool has_stalled_group = false;
for (GroupMap::const_iterator i = group_map_.begin();
i != group_map_.end(); ++i) {
Group* curr_group = i->second;
if (!curr_group->has_pending_requests())
continue;
if (curr_group->CanUseAdditionalSocketSlot(max_sockets_per_group_)) {
if (!group)
return true;
has_stalled_group = true;
bool has_higher_priority = !top_group ||
curr_group->TopPendingPriority() > top_group->TopPendingPriority();
if (has_higher_priority) {
top_group = curr_group;
top_group_name = &i->first;
}
}
}
if (top_group) {
CHECK(group);
*group = top_group;
*group_name = *top_group_name;
} else {
CHECK(!has_stalled_group);
}
return has_stalled_group;
}
void ClientSocketPoolBaseHelper::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;
std::unique_ptr<StreamSocket> socket = job->PassSocket();
// Copies of these are needed because |job| may be deleted before they are
// accessed.
NetLogWithSource job_log = job->net_log();
LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing();
// RemoveConnectJob(job, _) must be called by all branches below;
// otherwise, |job| will be leaked.
if (result == OK) {
DCHECK(socket.get());
RemoveConnectJob(job, group);
std::unique_ptr<Request> request = group->PopNextPendingRequest();
if (request) {
LogBoundConnectJobToRequest(job_log.source(), *request);
HandOutSocket(std::move(socket), ClientSocketHandle::UNUSED,
connect_timing, request->handle(), base::TimeDelta(), group,
request->net_log());
request->net_log().EndEvent(NetLogEventType::SOCKET_POOL);
InvokeUserCallbackLater(request->handle(), request->callback(), result,
request->socket_tag());
} else {
AddIdleSocket(std::move(socket), group);
OnAvailableSocketSlot(group_name, group);
CheckForStalledSocketGroups();
}
} else {
// If we got a socket, it must contain error information so pass that
// up so that the caller can retrieve it.
bool handed_out_socket = false;
std::unique_ptr<Request> request = group->PopNextPendingRequest();
if (request) {
LogBoundConnectJobToRequest(job_log.source(), *request);
job->GetAdditionalErrorState(request->handle());
RemoveConnectJob(job, group);
if (socket.get()) {
handed_out_socket = true;
HandOutSocket(std::move(socket), ClientSocketHandle::UNUSED,
connect_timing, request->handle(), base::TimeDelta(),
group, request->net_log());
}
request->net_log().EndEventWithNetErrorCode(NetLogEventType::SOCKET_POOL,
result);
InvokeUserCallbackLater(request->handle(), request->callback(), result,
request->socket_tag());