Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: net/socket/client_socket_pool_base.cc

Issue 10026024: Attempting to re-land a small portion of this change... Simply add links from (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix willchan's nit Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/socket/client_socket_pool_base.h ('k') | net/socket/client_socket_pool_base_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/socket/client_socket_pool_base.h" 5 #include "net/socket/client_socket_pool_base.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 } 200 }
201 201
202 ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() { 202 ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
203 // Clean up any idle sockets and pending connect jobs. Assert that we have no 203 // Clean up any idle sockets and pending connect jobs. Assert that we have no
204 // remaining active sockets or pending requests. They should have all been 204 // remaining active sockets or pending requests. They should have all been
205 // cleaned up prior to |this| being destroyed. 205 // cleaned up prior to |this| being destroyed.
206 Flush(); 206 Flush();
207 DCHECK(group_map_.empty()); 207 DCHECK(group_map_.empty());
208 DCHECK(pending_callback_map_.empty()); 208 DCHECK(pending_callback_map_.empty());
209 DCHECK_EQ(0, connecting_socket_count_); 209 DCHECK_EQ(0, connecting_socket_count_);
210 CHECK(higher_layer_pools_.empty());
210 211
211 NetworkChangeNotifier::RemoveIPAddressObserver(this); 212 NetworkChangeNotifier::RemoveIPAddressObserver(this);
212 } 213 }
213 214
214 ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {} 215 ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {}
215 216
216 // InsertRequestIntoQueue inserts the request into the queue based on 217 // InsertRequestIntoQueue inserts the request into the queue based on
217 // priority. Highest priorities are closest to the front. Older requests are 218 // priority. Highest priorities are closest to the front. Older requests are
218 // prioritized over requests of equal priority. 219 // prioritized over requests of equal priority.
219 // 220 //
(...skipping 11 matching lines...) Expand all
231 ClientSocketPoolBaseHelper::RemoveRequestFromQueue( 232 ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
232 const RequestQueue::iterator& it, Group* group) { 233 const RequestQueue::iterator& it, Group* group) {
233 const Request* req = *it; 234 const Request* req = *it;
234 group->mutable_pending_requests()->erase(it); 235 group->mutable_pending_requests()->erase(it);
235 // If there are no more requests, we kill the backup timer. 236 // If there are no more requests, we kill the backup timer.
236 if (group->pending_requests().empty()) 237 if (group->pending_requests().empty())
237 group->CleanupBackupJob(); 238 group->CleanupBackupJob();
238 return req; 239 return req;
239 } 240 }
240 241
242 void ClientSocketPoolBaseHelper::AddLayeredPool(LayeredPool* pool) {
243 CHECK(pool);
244 CHECK(!ContainsKey(higher_layer_pools_, pool));
245 higher_layer_pools_.insert(pool);
246 }
247
248 void ClientSocketPoolBaseHelper::RemoveLayeredPool(LayeredPool* pool) {
249 CHECK(pool);
250 CHECK(ContainsKey(higher_layer_pools_, pool));
251 higher_layer_pools_.erase(pool);
252 }
253
241 int ClientSocketPoolBaseHelper::RequestSocket( 254 int ClientSocketPoolBaseHelper::RequestSocket(
242 const std::string& group_name, 255 const std::string& group_name,
243 const Request* request) { 256 const Request* request) {
244 CHECK(!request->callback().is_null()); 257 CHECK(!request->callback().is_null());
245 CHECK(request->handle()); 258 CHECK(request->handle());
246 259
247 // Cleanup any timed-out idle sockets if no timer is used. 260 // Cleanup any timed-out idle sockets if no timer is used.
248 if (!use_cleanup_timer_) 261 if (!use_cleanup_timer_)
249 CleanupIdleSockets(false); 262 CleanupIdleSockets(false);
250 263
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 if (AssignIdleSocketToGroup(request, group)) 342 if (AssignIdleSocketToGroup(request, group))
330 return OK; 343 return OK;
331 } 344 }
332 345
333 if (!preconnecting && group->TryToUsePreconnectConnectJob()) 346 if (!preconnecting && group->TryToUsePreconnectConnectJob())
334 return ERR_IO_PENDING; 347 return ERR_IO_PENDING;
335 348
336 // Can we make another active socket now? 349 // Can we make another active socket now?
337 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) && 350 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
338 !request->ignore_limits()) { 351 !request->ignore_limits()) {
352 // TODO(willchan): Consider whether or not we need to close a socket in a
353 // higher layered group. I don't think this makes sense since we would just
354 // reuse that socket then if we needed one and wouldn't make it down to this
355 // layer.
339 request->net_log().AddEvent( 356 request->net_log().AddEvent(
340 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL); 357 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL);
341 return ERR_IO_PENDING; 358 return ERR_IO_PENDING;
342 } 359 }
343 360
344 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) { 361 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
362 // NOTE(mmenke): Wonder if we really need different code for each case
363 // here. Only reason for them now seems to be preconnects.
345 if (idle_socket_count() > 0) { 364 if (idle_socket_count() > 0) {
365 // There's an idle socket in this pool. Either that's because there's
366 // still one in this group, but we got here due to preconnecting bypassing
367 // idle sockets, or because there's an idle socket in another group.
346 bool closed = CloseOneIdleSocketExceptInGroup(group); 368 bool closed = CloseOneIdleSocketExceptInGroup(group);
347 if (preconnecting && !closed) 369 if (preconnecting && !closed)
348 return ERR_PRECONNECT_MAX_SOCKET_LIMIT; 370 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
349 } else { 371 } else {
350 // We could check if we really have a stalled group here, but it requires 372 // We could check if we really have a stalled group here, but it requires
351 // a scan of all groups, so just flip a flag here, and do the check later. 373 // a scan of all groups, so just flip a flag here, and do the check later.
352 request->net_log().AddEvent( 374 request->net_log().AddEvent(
353 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS, NULL); 375 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS, NULL);
354 return ERR_IO_PENDING; 376 return ERR_IO_PENDING;
355 } 377 }
356 } 378 }
357 379
358 // We couldn't find a socket to reuse, so allocate and connect a new one. 380 // We couldn't find a socket to reuse, and there's space to allocate one,
381 // so allocate and connect a new one.
359 scoped_ptr<ConnectJob> connect_job( 382 scoped_ptr<ConnectJob> connect_job(
360 connect_job_factory_->NewConnectJob(group_name, *request, this)); 383 connect_job_factory_->NewConnectJob(group_name, *request, this));
361 384
362 connect_job->Initialize(preconnecting); 385 connect_job->Initialize(preconnecting);
363 int rv = connect_job->Connect(); 386 int rv = connect_job->Connect();
364 if (rv == OK) { 387 if (rv == OK) {
365 LogBoundConnectJobToRequest(connect_job->net_log().source(), request); 388 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
366 if (!preconnecting) { 389 if (!preconnecting) {
367 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */, 390 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
368 handle, base::TimeDelta(), group, request->net_log()); 391 handle, base::TimeDelta(), group, request->net_log());
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 633
611 ListValue* connect_jobs_list = new ListValue(); 634 ListValue* connect_jobs_list = new ListValue();
612 std::set<ConnectJob*>::const_iterator job = group->jobs().begin(); 635 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
613 for (job = group->jobs().begin(); job != group->jobs().end(); job++) { 636 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
614 int source_id = (*job)->net_log().source().id; 637 int source_id = (*job)->net_log().source().id;
615 connect_jobs_list->Append(Value::CreateIntegerValue(source_id)); 638 connect_jobs_list->Append(Value::CreateIntegerValue(source_id));
616 } 639 }
617 group_dict->Set("connect_jobs", connect_jobs_list); 640 group_dict->Set("connect_jobs", connect_jobs_list);
618 641
619 group_dict->SetBoolean("is_stalled", 642 group_dict->SetBoolean("is_stalled",
620 group->IsStalled(max_sockets_per_group_)); 643 group->IsStalledOnPoolMaxSockets(
644 max_sockets_per_group_));
621 group_dict->SetBoolean("has_backup_job", group->HasBackupJob()); 645 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
622 646
623 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict); 647 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
624 } 648 }
625 dict->Set("groups", all_groups_dict); 649 dict->Set("groups", all_groups_dict);
626 return dict; 650 return dict;
627 } 651 }
628 652
629 bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup( 653 bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
630 base::TimeTicks now, 654 base::TimeTicks now,
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 // its limit, may be left with other stalled groups that could be 809 // its limit, may be left with other stalled groups that could be
786 // woken. This isn't optimal, but there is no starvation, so to avoid 810 // woken. This isn't optimal, but there is no starvation, so to avoid
787 // the looping we leave it at this. 811 // the looping we leave it at this.
788 OnAvailableSocketSlot(top_group_name, top_group); 812 OnAvailableSocketSlot(top_group_name, top_group);
789 } 813 }
790 814
791 // Search for the highest priority pending request, amongst the groups that 815 // Search for the highest priority pending request, amongst the groups that
792 // are not at the |max_sockets_per_group_| limit. Note: for requests with 816 // are not at the |max_sockets_per_group_| limit. Note: for requests with
793 // the same priority, the winner is based on group hash ordering (and not 817 // the same priority, the winner is based on group hash ordering (and not
794 // insertion order). 818 // insertion order).
795 bool ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group, 819 bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
796 std::string* group_name) { 820 Group** group,
821 std::string* group_name) const {
822 CHECK((group && group_name) || (!group && !group_name));
797 Group* top_group = NULL; 823 Group* top_group = NULL;
798 const std::string* top_group_name = NULL; 824 const std::string* top_group_name = NULL;
799 bool has_stalled_group = false; 825 bool has_stalled_group = false;
800 for (GroupMap::iterator i = group_map_.begin(); 826 for (GroupMap::const_iterator i = group_map_.begin();
801 i != group_map_.end(); ++i) { 827 i != group_map_.end(); ++i) {
802 Group* curr_group = i->second; 828 Group* curr_group = i->second;
803 const RequestQueue& queue = curr_group->pending_requests(); 829 const RequestQueue& queue = curr_group->pending_requests();
804 if (queue.empty()) 830 if (queue.empty())
805 continue; 831 continue;
806 if (curr_group->IsStalled(max_sockets_per_group_)) { 832 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
833 if (!group)
834 return true;
807 has_stalled_group = true; 835 has_stalled_group = true;
808 bool has_higher_priority = !top_group || 836 bool has_higher_priority = !top_group ||
809 curr_group->TopPendingPriority() < top_group->TopPendingPriority(); 837 curr_group->TopPendingPriority() < top_group->TopPendingPriority();
810 if (has_higher_priority) { 838 if (has_higher_priority) {
811 top_group = curr_group; 839 top_group = curr_group;
812 top_group_name = &i->first; 840 top_group_name = &i->first;
813 } 841 }
814 } 842 }
815 } 843 }
816 844
817 if (top_group) { 845 if (top_group) {
846 CHECK(group);
818 *group = top_group; 847 *group = top_group;
819 *group_name = *top_group_name; 848 *group_name = *top_group_name;
849 } else {
850 CHECK(!has_stalled_group);
820 } 851 }
821 return has_stalled_group; 852 return has_stalled_group;
822 } 853 }
823 854
824 void ClientSocketPoolBaseHelper::OnConnectJobComplete( 855 void ClientSocketPoolBaseHelper::OnConnectJobComplete(
825 int result, ConnectJob* job) { 856 int result, ConnectJob* job) {
826 DCHECK_NE(ERR_IO_PENDING, result); 857 DCHECK_NE(ERR_IO_PENDING, result);
827 const std::string group_name = job->group_name(); 858 const std::string group_name = job->group_name();
828 GroupMap::iterator group_it = group_map_.find(group_name); 859 GroupMap::iterator group_it = group_map_.find(group_name);
829 CHECK(group_it != group_map_.end()); 860 CHECK(group_it != group_map_.end());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
882 Flush(); 913 Flush();
883 } 914 }
884 915
885 void ClientSocketPoolBaseHelper::Flush() { 916 void ClientSocketPoolBaseHelper::Flush() {
886 pool_generation_number_++; 917 pool_generation_number_++;
887 CancelAllConnectJobs(); 918 CancelAllConnectJobs();
888 CloseIdleSockets(); 919 CloseIdleSockets();
889 AbortAllRequests(); 920 AbortAllRequests();
890 } 921 }
891 922
923 bool ClientSocketPoolBaseHelper::IsStalled() const {
924 // If we are not using |max_sockets_|, then clearly we are not stalled
925 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
926 return false;
927 // So in order to be stalled we need to be using |max_sockets_| AND
928 // we need to have a request that is actually stalled on the global
929 // socket limit. To find such a request, we look for a group that
930 // a has more requests that jobs AND where the number of jobs is less
931 // than |max_sockets_per_group_|. (If the number of jobs is equal to
932 // |max_sockets_per_group_|, then the request is stalled on the group,
933 // which does not count.)
934 for (GroupMap::const_iterator it = group_map_.begin();
935 it != group_map_.end(); ++it) {
936 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_))
937 return true;
938 }
939 return false;
940 }
941
892 void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job, 942 void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
893 Group* group) { 943 Group* group) {
894 CHECK_GT(connecting_socket_count_, 0); 944 CHECK_GT(connecting_socket_count_, 0);
895 connecting_socket_count_--; 945 connecting_socket_count_--;
896 946
897 DCHECK(group); 947 DCHECK(group);
898 DCHECK(ContainsKey(group->jobs(), job)); 948 DCHECK(ContainsKey(group->jobs(), job));
899 group->RemoveJob(job); 949 group->RemoveJob(job);
900 950
901 // If we've got no more jobs for this group, then we no longer need a 951 // If we've got no more jobs for this group, then we no longer need a
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 // Each connecting socket will eventually connect and be handed out. 1068 // Each connecting socket will eventually connect and be handed out.
1019 int total = handed_out_socket_count_ + connecting_socket_count_ + 1069 int total = handed_out_socket_count_ + connecting_socket_count_ +
1020 idle_socket_count(); 1070 idle_socket_count();
1021 // There can be more sockets than the limit since some requests can ignore 1071 // There can be more sockets than the limit since some requests can ignore
1022 // the limit 1072 // the limit
1023 if (total < max_sockets_) 1073 if (total < max_sockets_)
1024 return false; 1074 return false;
1025 return true; 1075 return true;
1026 } 1076 }
1027 1077
1028 void ClientSocketPoolBaseHelper::CloseOneIdleSocket() { 1078 bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1029 CloseOneIdleSocketExceptInGroup(NULL); 1079 if (idle_socket_count() == 0)
1080 return false;
1081 return CloseOneIdleSocketExceptInGroup(NULL);
1030 } 1082 }
1031 1083
1032 bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup( 1084 bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1033 const Group* exception_group) { 1085 const Group* exception_group) {
1034 CHECK_GT(idle_socket_count(), 0); 1086 CHECK_GT(idle_socket_count(), 0);
1035 1087
1036 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) { 1088 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
1037 Group* group = i->second; 1089 Group* group = i->second;
1038 if (exception_group == group) 1090 if (exception_group == group)
1039 continue; 1091 continue;
1040 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); 1092 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
1041 1093
1042 if (!idle_sockets->empty()) { 1094 if (!idle_sockets->empty()) {
1043 delete idle_sockets->front().socket; 1095 delete idle_sockets->front().socket;
1044 idle_sockets->pop_front(); 1096 idle_sockets->pop_front();
1045 DecrementIdleCount(); 1097 DecrementIdleCount();
1046 if (group->IsEmpty()) 1098 if (group->IsEmpty())
1047 RemoveGroup(i); 1099 RemoveGroup(i);
1048 1100
1049 return true; 1101 return true;
1050 } 1102 }
1051 } 1103 }
1052 1104
1053 if (!exception_group)
1054 LOG(DFATAL) << "No idle socket found to close!.";
1055
1056 return false; 1105 return false;
1057 } 1106 }
1058 1107
1108 bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInLayeredPool() {
1109 // This pool doesn't have any idle sockets. It's possible that a pool at a
1110 // higher layer is holding one of this sockets active, but it's actually idle.
1111 // Query the higher layers.
1112 for (std::set<LayeredPool*>::const_iterator it = higher_layer_pools_.begin();
1113 it != higher_layer_pools_.end(); ++it) {
1114 if ((*it)->CloseOneIdleConnection())
1115 return true;
1116 }
1117 return false;
1118 }
1119
1059 void ClientSocketPoolBaseHelper::InvokeUserCallbackLater( 1120 void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
1060 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) { 1121 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
1061 CHECK(!ContainsKey(pending_callback_map_, handle)); 1122 CHECK(!ContainsKey(pending_callback_map_, handle));
1062 pending_callback_map_[handle] = CallbackResultPair(callback, rv); 1123 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
1063 MessageLoop::current()->PostTask( 1124 MessageLoop::current()->PostTask(
1064 FROM_HERE, 1125 FROM_HERE,
1065 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback, 1126 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1066 weak_factory_.GetWeakPtr(), handle)); 1127 weak_factory_.GetWeakPtr(), handle));
1067 } 1128 }
1068 1129
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1152 // Delete active jobs. 1213 // Delete active jobs.
1153 STLDeleteElements(&jobs_); 1214 STLDeleteElements(&jobs_);
1154 1215
1155 // Cancel pending backup job. 1216 // Cancel pending backup job.
1156 weak_factory_.InvalidateWeakPtrs(); 1217 weak_factory_.InvalidateWeakPtrs();
1157 } 1218 }
1158 1219
1159 } // namespace internal 1220 } // namespace internal
1160 1221
1161 } // namespace net 1222 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/client_socket_pool_base.h ('k') | net/socket/client_socket_pool_base_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698