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

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

Issue 9226039: Revert r113405, since it appears to be causing a crash and a hang. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update more liscence dates Created 8 years, 11 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) 2011 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"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
(...skipping 188 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 DCHECK(higher_layer_pools_.empty());
211 210
212 NetworkChangeNotifier::RemoveIPAddressObserver(this); 211 NetworkChangeNotifier::RemoveIPAddressObserver(this);
213 } 212 }
214 213
215 ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {} 214 ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {}
216 215
217 // InsertRequestIntoQueue inserts the request into the queue based on 216 // InsertRequestIntoQueue inserts the request into the queue based on
218 // priority. Highest priorities are closest to the front. Older requests are 217 // priority. Highest priorities are closest to the front. Older requests are
219 // prioritized over requests of equal priority. 218 // prioritized over requests of equal priority.
220 // 219 //
(...skipping 11 matching lines...) Expand all
232 ClientSocketPoolBaseHelper::RemoveRequestFromQueue( 231 ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
233 const RequestQueue::iterator& it, Group* group) { 232 const RequestQueue::iterator& it, Group* group) {
234 const Request* req = *it; 233 const Request* req = *it;
235 group->mutable_pending_requests()->erase(it); 234 group->mutable_pending_requests()->erase(it);
236 // If there are no more requests, we kill the backup timer. 235 // If there are no more requests, we kill the backup timer.
237 if (group->pending_requests().empty()) 236 if (group->pending_requests().empty())
238 group->CleanupBackupJob(); 237 group->CleanupBackupJob();
239 return req; 238 return req;
240 } 239 }
241 240
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
254 int ClientSocketPoolBaseHelper::RequestSocket( 241 int ClientSocketPoolBaseHelper::RequestSocket(
255 const std::string& group_name, 242 const std::string& group_name,
256 const Request* request) { 243 const Request* request) {
257 CHECK(!request->callback().is_null()); 244 CHECK(!request->callback().is_null());
258 CHECK(request->handle()); 245 CHECK(request->handle());
259 246
260 // Cleanup any timed-out idle sockets if no timer is used. 247 // Cleanup any timed-out idle sockets if no timer is used.
261 if (!use_cleanup_timer_) 248 if (!use_cleanup_timer_)
262 CleanupIdleSockets(false); 249 CleanupIdleSockets(false);
263 250
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 if (AssignIdleSocketToGroup(request, group)) 329 if (AssignIdleSocketToGroup(request, group))
343 return OK; 330 return OK;
344 } 331 }
345 332
346 if (!preconnecting && group->TryToUsePreconnectConnectJob()) 333 if (!preconnecting && group->TryToUsePreconnectConnectJob())
347 return ERR_IO_PENDING; 334 return ERR_IO_PENDING;
348 335
349 // Can we make another active socket now? 336 // Can we make another active socket now?
350 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) && 337 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
351 !request->ignore_limits()) { 338 !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.
356 request->net_log().AddEvent( 339 request->net_log().AddEvent(
357 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL); 340 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL);
358 return ERR_IO_PENDING; 341 return ERR_IO_PENDING;
359 } 342 }
360 343
361 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) { 344 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
362 if (idle_socket_count() > 0) { 345 if (idle_socket_count() > 0) {
363 // There's an idle socket in this pool. Either that's because there's
364 // still one in this group, but we got here due to preconnecting bypassing
365 // idle sockets, or because there's an idle socket in another group.
366 bool closed = CloseOneIdleSocketExceptInGroup(group); 346 bool closed = CloseOneIdleSocketExceptInGroup(group);
367 if (preconnecting && !closed) 347 if (preconnecting && !closed)
368 return ERR_PRECONNECT_MAX_SOCKET_LIMIT; 348 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
369 } else { 349 } else {
370 do { 350 // We could check if we really have a stalled group here, but it requires
371 if (!CloseOneIdleConnectionInLayeredPool()) { 351 // a scan of all groups, so just flip a flag here, and do the check later.
372 // We could check if we really have a stalled group here, but it 352 request->net_log().AddEvent(
373 // requires a scan of all groups, so just flip a flag here, and do 353 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS, NULL);
374 // the check later. 354 return ERR_IO_PENDING;
375 request->net_log().AddEvent(
376 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS, NULL);
377 return ERR_IO_PENDING;
378 }
379 } while (ReachedMaxSocketsLimit());
380
381 // It is possible that CloseOneIdleConnectionInLayeredPool() has deleted
382 // our Group (see http://crbug.com/109876), so look it up again
383 // to be safe.
384 group = GetOrCreateGroup(group_name);
385 } 355 }
386 } 356 }
387 357
388 // We couldn't find a socket to reuse, and there's space to allocate one, 358 // We couldn't find a socket to reuse, so allocate and connect a new one.
389 // so allocate and connect a new one.
390 scoped_ptr<ConnectJob> connect_job( 359 scoped_ptr<ConnectJob> connect_job(
391 connect_job_factory_->NewConnectJob(group_name, *request, this)); 360 connect_job_factory_->NewConnectJob(group_name, *request, this));
392 361
393 connect_job->Initialize(preconnecting); 362 connect_job->Initialize(preconnecting);
394 int rv = connect_job->Connect(); 363 int rv = connect_job->Connect();
395 if (rv == OK) { 364 if (rv == OK) {
396 LogBoundConnectJobToRequest(connect_job->net_log().source(), request); 365 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
397 if (!preconnecting) { 366 if (!preconnecting) {
398 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */, 367 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
399 handle, base::TimeDelta(), group, request->net_log()); 368 handle, base::TimeDelta(), group, request->net_log());
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 // its limit, may be left with other stalled groups that could be 785 // its limit, may be left with other stalled groups that could be
817 // woken. This isn't optimal, but there is no starvation, so to avoid 786 // woken. This isn't optimal, but there is no starvation, so to avoid
818 // the looping we leave it at this. 787 // the looping we leave it at this.
819 OnAvailableSocketSlot(top_group_name, top_group); 788 OnAvailableSocketSlot(top_group_name, top_group);
820 } 789 }
821 790
822 // Search for the highest priority pending request, amongst the groups that 791 // Search for the highest priority pending request, amongst the groups that
823 // are not at the |max_sockets_per_group_| limit. Note: for requests with 792 // are not at the |max_sockets_per_group_| limit. Note: for requests with
824 // the same priority, the winner is based on group hash ordering (and not 793 // the same priority, the winner is based on group hash ordering (and not
825 // insertion order). 794 // insertion order).
826 bool ClientSocketPoolBaseHelper::FindTopStalledGroup( 795 bool ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group,
827 Group** group, 796 std::string* group_name) {
828 std::string* group_name) const {
829 CHECK((group && group_name) || (!group && !group_name));
830 Group* top_group = NULL; 797 Group* top_group = NULL;
831 const std::string* top_group_name = NULL; 798 const std::string* top_group_name = NULL;
832 bool has_stalled_group = false; 799 bool has_stalled_group = false;
833 for (GroupMap::const_iterator i = group_map_.begin(); 800 for (GroupMap::iterator i = group_map_.begin();
834 i != group_map_.end(); ++i) { 801 i != group_map_.end(); ++i) {
835 Group* curr_group = i->second; 802 Group* curr_group = i->second;
836 const RequestQueue& queue = curr_group->pending_requests(); 803 const RequestQueue& queue = curr_group->pending_requests();
837 if (queue.empty()) 804 if (queue.empty())
838 continue; 805 continue;
839 if (curr_group->IsStalled(max_sockets_per_group_)) { 806 if (curr_group->IsStalled(max_sockets_per_group_)) {
840 if (!group)
841 return true;
842 has_stalled_group = true; 807 has_stalled_group = true;
843 bool has_higher_priority = !top_group || 808 bool has_higher_priority = !top_group ||
844 curr_group->TopPendingPriority() < top_group->TopPendingPriority(); 809 curr_group->TopPendingPriority() < top_group->TopPendingPriority();
845 if (has_higher_priority) { 810 if (has_higher_priority) {
846 top_group = curr_group; 811 top_group = curr_group;
847 top_group_name = &i->first; 812 top_group_name = &i->first;
848 } 813 }
849 } 814 }
850 } 815 }
851 816
852 if (top_group) { 817 if (top_group) {
853 CHECK(group);
854 *group = top_group; 818 *group = top_group;
855 *group_name = *top_group_name; 819 *group_name = *top_group_name;
856 } else {
857 CHECK(!has_stalled_group);
858 } 820 }
859 return has_stalled_group; 821 return has_stalled_group;
860 } 822 }
861 823
862 void ClientSocketPoolBaseHelper::OnConnectJobComplete( 824 void ClientSocketPoolBaseHelper::OnConnectJobComplete(
863 int result, ConnectJob* job) { 825 int result, ConnectJob* job) {
864 DCHECK_NE(ERR_IO_PENDING, result); 826 DCHECK_NE(ERR_IO_PENDING, result);
865 const std::string group_name = job->group_name(); 827 const std::string group_name = job->group_name();
866 GroupMap::iterator group_it = group_map_.find(group_name); 828 GroupMap::iterator group_it = group_map_.find(group_name);
867 CHECK(group_it != group_map_.end()); 829 CHECK(group_it != group_map_.end());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
920 Flush(); 882 Flush();
921 } 883 }
922 884
923 void ClientSocketPoolBaseHelper::Flush() { 885 void ClientSocketPoolBaseHelper::Flush() {
924 pool_generation_number_++; 886 pool_generation_number_++;
925 CancelAllConnectJobs(); 887 CancelAllConnectJobs();
926 CloseIdleSockets(); 888 CloseIdleSockets();
927 AbortAllRequests(); 889 AbortAllRequests();
928 } 890 }
929 891
930 bool ClientSocketPoolBaseHelper::IsStalled() const {
931 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
932 return false;
933 for (GroupMap::const_iterator it = group_map_.begin();
934 it != group_map_.end(); it++) {
935 if (it->second->IsStalled(max_sockets_per_group_))
936 return true;
937 }
938 return false;
939 }
940
941 void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job, 892 void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
942 Group* group) { 893 Group* group) {
943 CHECK_GT(connecting_socket_count_, 0); 894 CHECK_GT(connecting_socket_count_, 0);
944 connecting_socket_count_--; 895 connecting_socket_count_--;
945 896
946 DCHECK(group); 897 DCHECK(group);
947 DCHECK(ContainsKey(group->jobs(), job)); 898 DCHECK(ContainsKey(group->jobs(), job));
948 group->RemoveJob(job); 899 group->RemoveJob(job);
949 900
950 // If we've got no more jobs for this group, then we no longer need a 901 // 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
1067 // Each connecting socket will eventually connect and be handed out. 1018 // Each connecting socket will eventually connect and be handed out.
1068 int total = handed_out_socket_count_ + connecting_socket_count_ + 1019 int total = handed_out_socket_count_ + connecting_socket_count_ +
1069 idle_socket_count(); 1020 idle_socket_count();
1070 // There can be more sockets than the limit since some requests can ignore 1021 // There can be more sockets than the limit since some requests can ignore
1071 // the limit 1022 // the limit
1072 if (total < max_sockets_) 1023 if (total < max_sockets_)
1073 return false; 1024 return false;
1074 return true; 1025 return true;
1075 } 1026 }
1076 1027
1077 bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() { 1028 void ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1078 if (idle_socket_count() == 0) 1029 CloseOneIdleSocketExceptInGroup(NULL);
1079 return false;
1080 return CloseOneIdleSocketExceptInGroup(NULL);
1081 } 1030 }
1082 1031
1083 bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup( 1032 bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1084 const Group* exception_group) { 1033 const Group* exception_group) {
1085 CHECK_GT(idle_socket_count(), 0); 1034 CHECK_GT(idle_socket_count(), 0);
1086 1035
1087 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) { 1036 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
1088 Group* group = i->second; 1037 Group* group = i->second;
1089 if (exception_group == group) 1038 if (exception_group == group)
1090 continue; 1039 continue;
1091 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); 1040 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
1092 1041
1093 if (!idle_sockets->empty()) { 1042 if (!idle_sockets->empty()) {
1094 delete idle_sockets->front().socket; 1043 delete idle_sockets->front().socket;
1095 idle_sockets->pop_front(); 1044 idle_sockets->pop_front();
1096 DecrementIdleCount(); 1045 DecrementIdleCount();
1097 if (group->IsEmpty()) 1046 if (group->IsEmpty())
1098 RemoveGroup(i); 1047 RemoveGroup(i);
1099 1048
1100 return true; 1049 return true;
1101 } 1050 }
1102 } 1051 }
1103 1052
1053 if (!exception_group)
1054 LOG(DFATAL) << "No idle socket found to close!.";
1055
1104 return false; 1056 return false;
1105 } 1057 }
1106 1058
1107 bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInLayeredPool() {
1108 // This pool doesn't have any idle sockets. It's possible that a pool at a
1109 // higher layer is holding one of this sockets active, but it's actually idle.
1110 // Query the higher layers.
1111 for (std::set<LayeredPool*>::const_iterator it = higher_layer_pools_.begin();
1112 it != higher_layer_pools_.end(); ++it) {
1113 if ((*it)->CloseOneIdleConnection())
1114 return true;
1115 }
1116 return false;
1117 }
1118
1119 void ClientSocketPoolBaseHelper::InvokeUserCallbackLater( 1059 void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
1120 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) { 1060 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
1121 CHECK(!ContainsKey(pending_callback_map_, handle)); 1061 CHECK(!ContainsKey(pending_callback_map_, handle));
1122 pending_callback_map_[handle] = CallbackResultPair(callback, rv); 1062 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
1123 MessageLoop::current()->PostTask( 1063 MessageLoop::current()->PostTask(
1124 FROM_HERE, 1064 FROM_HERE,
1125 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback, 1065 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1126 weak_factory_.GetWeakPtr(), handle)); 1066 weak_factory_.GetWeakPtr(), handle));
1127 } 1067 }
1128 1068
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1212 // Delete active jobs. 1152 // Delete active jobs.
1213 STLDeleteElements(&jobs_); 1153 STLDeleteElements(&jobs_);
1214 1154
1215 // Cancel pending backup job. 1155 // Cancel pending backup job.
1216 weak_factory_.InvalidateWeakPtrs(); 1156 weak_factory_.InvalidateWeakPtrs();
1217 } 1157 }
1218 1158
1219 } // namespace internal 1159 } // namespace internal
1220 1160
1221 } // namespace net 1161 } // 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