OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // A ClientSocketPoolBase is used to restrict the number of sockets open at | 5 // A ClientSocketPoolBase is used to restrict the number of sockets open at |
6 // a time. It also maintains a list of idle persistent sockets for reuse. | 6 // a time. It also maintains a list of idle persistent sockets for reuse. |
7 // Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle | 7 // Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle |
8 // the core logic of (1) restricting the number of active (connected or | 8 // the core logic of (1) restricting the number of active (connected or |
9 // connecting) sockets per "group" (generally speaking, the hostname), (2) | 9 // connecting) sockets per "group" (generally speaking, the hostname), (2) |
10 // maintaining a per-group list of idle, persistent sockets for reuse, and (3) | 10 // maintaining a per-group list of idle, persistent sockets for reuse, and (3) |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 // ClientSocketPoolBaseHelper takes ownership of |request|, which must be | 181 // ClientSocketPoolBaseHelper takes ownership of |request|, which must be |
182 // heap allocated. | 182 // heap allocated. |
183 int RequestSocket(const std::string& group_name, const Request* request); | 183 int RequestSocket(const std::string& group_name, const Request* request); |
184 | 184 |
185 // See ClientSocketPool::CancelRequest for documentation on this function. | 185 // See ClientSocketPool::CancelRequest for documentation on this function. |
186 void CancelRequest(const std::string& group_name, | 186 void CancelRequest(const std::string& group_name, |
187 const ClientSocketHandle* handle); | 187 const ClientSocketHandle* handle); |
188 | 188 |
189 // See ClientSocketPool::ReleaseSocket for documentation on this function. | 189 // See ClientSocketPool::ReleaseSocket for documentation on this function. |
190 void ReleaseSocket(const std::string& group_name, | 190 void ReleaseSocket(const std::string& group_name, |
191 ClientSocket* socket); | 191 ClientSocket* socket, |
| 192 int id); |
192 | 193 |
| 194 // See ClientSocketPool::Flush for documentation on this function. |
| 195 void Flush(); |
| 196 |
193 // See ClientSocketPool::CloseIdleSockets for documentation on this function. | 197 // See ClientSocketPool::CloseIdleSockets for documentation on this function. |
194 void CloseIdleSockets(); | 198 void CloseIdleSockets(); |
195 | 199 |
196 // See ClientSocketPool::IdleSocketCount() for documentation on this function. | 200 // See ClientSocketPool::IdleSocketCount() for documentation on this function. |
197 int idle_socket_count() const { | 201 int idle_socket_count() const { |
198 return idle_socket_count_; | 202 return idle_socket_count_; |
199 } | 203 } |
200 | 204 |
201 // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this | 205 // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this |
202 // function. | 206 // function. |
203 int IdleSocketCountInGroup(const std::string& group_name) const; | 207 int IdleSocketCountInGroup(const std::string& group_name) const; |
204 | 208 |
205 // See ClientSocketPool::GetLoadState() for documentation on this function. | 209 // See ClientSocketPool::GetLoadState() for documentation on this function. |
206 LoadState GetLoadState(const std::string& group_name, | 210 LoadState GetLoadState(const std::string& group_name, |
207 const ClientSocketHandle* handle) const; | 211 const ClientSocketHandle* handle) const; |
208 | 212 |
209 int ConnectRetryIntervalMs() const { | 213 int ConnectRetryIntervalMs() const { |
210 // TODO(mbelshe): Make this tuned dynamically based on measured RTT. | 214 // TODO(mbelshe): Make this tuned dynamically based on measured RTT. |
211 // For now, just use the max retry interval. | 215 // For now, just use the max retry interval. |
212 return ClientSocketPool::kMaxConnectRetryIntervalMs; | 216 return ClientSocketPool::kMaxConnectRetryIntervalMs; |
213 } | 217 } |
214 | 218 |
215 // ConnectJob::Delegate methods: | 219 // ConnectJob::Delegate methods: |
216 virtual void OnConnectJobComplete(int result, ConnectJob* job); | 220 virtual void OnConnectJobComplete(int result, ConnectJob* job); |
217 | 221 |
218 // NetworkChangeNotifier::Observer methods: | 222 // NetworkChangeNotifier::Observer methods: |
219 virtual void OnIPAddressChanged(); | 223 virtual void OnIPAddressChanged() { Flush(); } |
220 | 224 |
221 // For testing. | 225 // For testing. |
222 bool may_have_stalled_group() const { return may_have_stalled_group_; } | 226 bool may_have_stalled_group() const { return may_have_stalled_group_; } |
223 | 227 |
224 int NumConnectJobsInGroup(const std::string& group_name) const { | 228 int NumConnectJobsInGroup(const std::string& group_name) const { |
225 return group_map_.find(group_name)->second.jobs.size(); | 229 return group_map_.find(group_name)->second.jobs.size(); |
226 } | 230 } |
227 | 231 |
228 // Closes all idle sockets if |force| is true. Else, only closes idle | 232 // Closes all idle sockets if |force| is true. Else, only closes idle |
229 // sockets that timed out or can't be reused. Made public for testing. | 233 // sockets that timed out or can't be reused. Made public for testing. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 static void InsertRequestIntoQueue(const Request* r, | 332 static void InsertRequestIntoQueue(const Request* r, |
329 RequestQueue* pending_requests); | 333 RequestQueue* pending_requests); |
330 static const Request* RemoveRequestFromQueue(RequestQueue::iterator it, | 334 static const Request* RemoveRequestFromQueue(RequestQueue::iterator it, |
331 RequestQueue* pending_requests); | 335 RequestQueue* pending_requests); |
332 | 336 |
333 // Called when the number of idle sockets changes. | 337 // Called when the number of idle sockets changes. |
334 void IncrementIdleCount(); | 338 void IncrementIdleCount(); |
335 void DecrementIdleCount(); | 339 void DecrementIdleCount(); |
336 | 340 |
337 // Called via PostTask by ReleaseSocket. | 341 // Called via PostTask by ReleaseSocket. |
338 void DoReleaseSocket(const std::string& group_name, ClientSocket* socket); | 342 void DoReleaseSocket( |
| 343 const std::string& group_name, ClientSocket* socket, int id); |
339 | 344 |
340 // Scans the group map for groups which have an available socket slot and | 345 // Scans the group map for groups which have an available socket slot and |
341 // at least one pending request. Returns number of groups found, and if found | 346 // at least one pending request. Returns number of groups found, and if found |
342 // at least one, fills |group| and |group_name| with data of the stalled group | 347 // at least one, fills |group| and |group_name| with data of the stalled group |
343 // having highest priority. | 348 // having highest priority. |
344 int FindTopStalledGroup(Group** group, std::string* group_name); | 349 int FindTopStalledGroup(Group** group, std::string* group_name); |
345 | 350 |
346 // Called when timer_ fires. This method scans the idle sockets removing | 351 // Called when timer_ fires. This method scans the idle sockets removing |
347 // sockets that timed out or can't be reused. | 352 // sockets that timed out or can't be reused. |
348 void OnCleanupTimerFired() { | 353 void OnCleanupTimerFired() { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 | 449 |
445 const scoped_ptr<ConnectJobFactory> connect_job_factory_; | 450 const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
446 | 451 |
447 NetworkChangeNotifier* const network_change_notifier_; | 452 NetworkChangeNotifier* const network_change_notifier_; |
448 | 453 |
449 // TODO(vandebo) Remove when backup jobs move to TCPClientSocketPool | 454 // TODO(vandebo) Remove when backup jobs move to TCPClientSocketPool |
450 bool backup_jobs_enabled_; | 455 bool backup_jobs_enabled_; |
451 | 456 |
452 // A factory to pin the backup_job tasks. | 457 // A factory to pin the backup_job tasks. |
453 ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_; | 458 ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_; |
| 459 |
| 460 // A unique id for the pool. It gets incremented every time we Flush() the |
| 461 // pool. This is so that when sockets get released back to the pool, we can |
| 462 // make sure that they are discarded rather than reused. |
| 463 int pool_generation_number_; |
454 }; | 464 }; |
455 | 465 |
456 } // namespace internal | 466 } // namespace internal |
457 | 467 |
458 // The maximum duration, in seconds, to keep unused idle persistent sockets | 468 // The maximum duration, in seconds, to keep unused idle persistent sockets |
459 // alive. | 469 // alive. |
460 // TODO(willchan): Change this timeout after getting histogram data on how | 470 // TODO(willchan): Change this timeout after getting histogram data on how |
461 // long it should be. | 471 // long it should be. |
462 static const int kUnusedIdleSocketTimeout = 10; | 472 static const int kUnusedIdleSocketTimeout = 10; |
463 // The maximum duration, in seconds, to keep used idle persistent sockets alive. | 473 // The maximum duration, in seconds, to keep used idle persistent sockets alive. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 const BoundNetLog& net_log) { | 545 const BoundNetLog& net_log) { |
536 Request* request = new Request(handle, callback, priority, params, net_log); | 546 Request* request = new Request(handle, callback, priority, params, net_log); |
537 return helper_->RequestSocket(group_name, request); | 547 return helper_->RequestSocket(group_name, request); |
538 } | 548 } |
539 | 549 |
540 void CancelRequest(const std::string& group_name, | 550 void CancelRequest(const std::string& group_name, |
541 const ClientSocketHandle* handle) { | 551 const ClientSocketHandle* handle) { |
542 return helper_->CancelRequest(group_name, handle); | 552 return helper_->CancelRequest(group_name, handle); |
543 } | 553 } |
544 | 554 |
545 void ReleaseSocket(const std::string& group_name, ClientSocket* socket) { | 555 void ReleaseSocket(const std::string& group_name, ClientSocket* socket, int id
) { |
546 return helper_->ReleaseSocket(group_name, socket); | 556 return helper_->ReleaseSocket(group_name, socket, id); |
547 } | 557 } |
548 | 558 |
549 void CloseIdleSockets() { return helper_->CloseIdleSockets(); } | 559 void CloseIdleSockets() { return helper_->CloseIdleSockets(); } |
550 | 560 |
551 int idle_socket_count() const { return helper_->idle_socket_count(); } | 561 int idle_socket_count() const { return helper_->idle_socket_count(); } |
552 | 562 |
553 int IdleSocketCountInGroup(const std::string& group_name) const { | 563 int IdleSocketCountInGroup(const std::string& group_name) const { |
554 return helper_->IdleSocketCountInGroup(group_name); | 564 return helper_->IdleSocketCountInGroup(group_name); |
555 } | 565 } |
556 | 566 |
(...skipping 22 matching lines...) Expand all Loading... |
579 base::TimeDelta ConnectionTimeout() const { | 589 base::TimeDelta ConnectionTimeout() const { |
580 return helper_->ConnectionTimeout(); | 590 return helper_->ConnectionTimeout(); |
581 } | 591 } |
582 | 592 |
583 scoped_refptr<ClientSocketPoolHistograms> histograms() const { | 593 scoped_refptr<ClientSocketPoolHistograms> histograms() const { |
584 return histograms_; | 594 return histograms_; |
585 } | 595 } |
586 | 596 |
587 void enable_backup_jobs() { helper_->enable_backup_jobs(); } | 597 void enable_backup_jobs() { helper_->enable_backup_jobs(); } |
588 | 598 |
| 599 void Flush() { helper_->Flush(); } |
| 600 |
589 private: | 601 private: |
590 // This adaptor class exists to bridge the | 602 // This adaptor class exists to bridge the |
591 // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and | 603 // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and |
592 // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the | 604 // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the |
593 // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to | 605 // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to |
594 // static_cast themselves. | 606 // static_cast themselves. |
595 class ConnectJobFactoryAdaptor | 607 class ConnectJobFactoryAdaptor |
596 : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { | 608 : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { |
597 public: | 609 public: |
598 typedef typename ClientSocketPoolBase<SocketParams>::ConnectJobFactory | 610 typedef typename ClientSocketPoolBase<SocketParams>::ConnectJobFactory |
(...skipping 29 matching lines...) Expand all Loading... |
628 // the posting of the task and the execution, then we'll hit the DCHECK that | 640 // the posting of the task and the execution, then we'll hit the DCHECK that |
629 // |ClientSocketPoolBaseHelper::group_map_| is empty. | 641 // |ClientSocketPoolBaseHelper::group_map_| is empty. |
630 scoped_refptr<internal::ClientSocketPoolBaseHelper> helper_; | 642 scoped_refptr<internal::ClientSocketPoolBaseHelper> helper_; |
631 | 643 |
632 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 644 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
633 }; | 645 }; |
634 | 646 |
635 } // namespace net | 647 } // namespace net |
636 | 648 |
637 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 649 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
OLD | NEW |