| OLD | NEW |
| 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 // 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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 RequestPriority priority, | 189 RequestPriority priority, |
| 190 ClientSocketPool::RespectLimits respect_limits, | 190 ClientSocketPool::RespectLimits respect_limits, |
| 191 Flags flags, | 191 Flags flags, |
| 192 const NetLogWithSource& net_log); | 192 const NetLogWithSource& net_log); |
| 193 | 193 |
| 194 virtual ~Request(); | 194 virtual ~Request(); |
| 195 | 195 |
| 196 ClientSocketHandle* handle() const { return handle_; } | 196 ClientSocketHandle* handle() const { return handle_; } |
| 197 const CompletionCallback& callback() const { return callback_; } | 197 const CompletionCallback& callback() const { return callback_; } |
| 198 RequestPriority priority() const { return priority_; } | 198 RequestPriority priority() const { return priority_; } |
| 199 void set_priority(RequestPriority priority) { priority_ = priority; } |
| 199 ClientSocketPool::RespectLimits respect_limits() const { | 200 ClientSocketPool::RespectLimits respect_limits() const { |
| 200 return respect_limits_; | 201 return respect_limits_; |
| 201 } | 202 } |
| 202 Flags flags() const { return flags_; } | 203 Flags flags() const { return flags_; } |
| 203 const NetLogWithSource& net_log() const { return net_log_; } | 204 const NetLogWithSource& net_log() const { return net_log_; } |
| 204 | 205 |
| 205 // TODO(eroman): Temporary until crbug.com/467797 is solved. | 206 // TODO(eroman): Temporary until crbug.com/467797 is solved. |
| 206 void CrashIfInvalid() const; | 207 void CrashIfInvalid() const; |
| 207 | 208 |
| 208 private: | 209 private: |
| 209 // TODO(eroman): Temporary until crbug.com/467797 is solved. | 210 // TODO(eroman): Temporary until crbug.com/467797 is solved. |
| 210 enum Liveness { | 211 enum Liveness { |
| 211 ALIVE = 0xCA11AB13, | 212 ALIVE = 0xCA11AB13, |
| 212 DEAD = 0xDEADBEEF, | 213 DEAD = 0xDEADBEEF, |
| 213 }; | 214 }; |
| 214 | 215 |
| 215 ClientSocketHandle* const handle_; | 216 ClientSocketHandle* const handle_; |
| 216 const CompletionCallback callback_; | 217 const CompletionCallback callback_; |
| 217 // TODO(akalin): Support reprioritization. | 218 RequestPriority priority_; |
| 218 const RequestPriority priority_; | |
| 219 const ClientSocketPool::RespectLimits respect_limits_; | 219 const ClientSocketPool::RespectLimits respect_limits_; |
| 220 const Flags flags_; | 220 const Flags flags_; |
| 221 const NetLogWithSource net_log_; | 221 const NetLogWithSource net_log_; |
| 222 | 222 |
| 223 // TODO(eroman): Temporary until crbug.com/467797 is solved. | 223 // TODO(eroman): Temporary until crbug.com/467797 is solved. |
| 224 Liveness liveness_ = ALIVE; | 224 Liveness liveness_ = ALIVE; |
| 225 | 225 |
| 226 DISALLOW_COPY_AND_ASSIGN(Request); | 226 DISALLOW_COPY_AND_ASSIGN(Request); |
| 227 }; | 227 }; |
| 228 | 228 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 259 // See LowerLayeredPool::IsStalled for documentation on this function. | 259 // See LowerLayeredPool::IsStalled for documentation on this function. |
| 260 bool IsStalled() const; | 260 bool IsStalled() const; |
| 261 | 261 |
| 262 // See LowerLayeredPool for documentation on these functions. It is expected | 262 // See LowerLayeredPool for documentation on these functions. It is expected |
| 263 // in the destructor that no higher layer pools remain. | 263 // in the destructor that no higher layer pools remain. |
| 264 void AddHigherLayeredPool(HigherLayeredPool* higher_pool); | 264 void AddHigherLayeredPool(HigherLayeredPool* higher_pool); |
| 265 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool); | 265 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool); |
| 266 | 266 |
| 267 // See ClientSocketPool::RequestSocket for documentation on this function. | 267 // See ClientSocketPool::RequestSocket for documentation on this function. |
| 268 int RequestSocket(const std::string& group_name, | 268 int RequestSocket(const std::string& group_name, |
| 269 std::unique_ptr<const Request> request); | 269 std::unique_ptr<Request> request); |
| 270 | 270 |
| 271 // See ClientSocketPool::RequestSocket for documentation on this function. | 271 // See ClientSocketPool::RequestSockets for documentation on this function. |
| 272 void RequestSockets(const std::string& group_name, | 272 void RequestSockets(const std::string& group_name, |
| 273 const Request& request, | 273 const Request& request, |
| 274 int num_sockets); | 274 int num_sockets); |
| 275 | 275 |
| 276 // See ClientSocketPool::SetPriority for documentation on this function. |
| 277 void SetPriority(const std::string& group_name, |
| 278 ClientSocketHandle* handle, |
| 279 RequestPriority priority); |
| 280 |
| 276 // See ClientSocketPool::CancelRequest for documentation on this function. | 281 // See ClientSocketPool::CancelRequest for documentation on this function. |
| 277 void CancelRequest(const std::string& group_name, | 282 void CancelRequest(const std::string& group_name, |
| 278 ClientSocketHandle* handle); | 283 ClientSocketHandle* handle); |
| 279 | 284 |
| 280 // See ClientSocketPool::ReleaseSocket for documentation on this function. | 285 // See ClientSocketPool::ReleaseSocket for documentation on this function. |
| 281 void ReleaseSocket(const std::string& group_name, | 286 void ReleaseSocket(const std::string& group_name, |
| 282 std::unique_ptr<StreamSocket> socket, | 287 std::unique_ptr<StreamSocket> socket, |
| 283 int id); | 288 int id); |
| 284 | 289 |
| 285 // See ClientSocketPool::FlushWithError for documentation on this function. | 290 // See ClientSocketPool::FlushWithError for documentation on this function. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 // | 378 // |
| 374 // Note that a socket that has never been used before (like a preconnected | 379 // Note that a socket that has never been used before (like a preconnected |
| 375 // socket) may be used even with unread data. This may be, e.g., a SPDY | 380 // socket) may be used even with unread data. This may be, e.g., a SPDY |
| 376 // SETTINGS frame. | 381 // SETTINGS frame. |
| 377 bool IsUsable() const; | 382 bool IsUsable() const; |
| 378 | 383 |
| 379 StreamSocket* socket; | 384 StreamSocket* socket; |
| 380 base::TimeTicks start_time; | 385 base::TimeTicks start_time; |
| 381 }; | 386 }; |
| 382 | 387 |
| 383 typedef PriorityQueue<const Request*> RequestQueue; | 388 typedef PriorityQueue<Request*> RequestQueue; |
| 384 typedef std::map<const ClientSocketHandle*, const Request*> RequestMap; | 389 typedef std::map<const ClientSocketHandle*, Request*> RequestMap; |
| 385 | 390 |
| 386 // A Group is allocated per group_name when there are idle sockets or pending | 391 // A Group is allocated per group_name when there are idle sockets or pending |
| 387 // requests. Otherwise, the Group object is removed from the map. | 392 // requests. Otherwise, the Group object is removed from the map. |
| 388 // |active_socket_count| tracks the number of sockets held by clients. | 393 // |active_socket_count| tracks the number of sockets held by clients. |
| 389 class Group { | 394 class Group { |
| 390 public: | 395 public: |
| 391 using JobList = std::list<std::unique_ptr<ConnectJob>>; | 396 using JobList = std::list<std::unique_ptr<ConnectJob>>; |
| 392 | 397 |
| 393 Group(); | 398 Group(); |
| 394 ~Group(); | 399 ~Group(); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 // Gets (but does not remove) the next pending request. Returns | 457 // Gets (but does not remove) the next pending request. Returns |
| 453 // NULL if there are no pending requests. | 458 // NULL if there are no pending requests. |
| 454 const Request* GetNextPendingRequest() const; | 459 const Request* GetNextPendingRequest() const; |
| 455 | 460 |
| 456 // Returns true if there is a connect job for |handle|. | 461 // Returns true if there is a connect job for |handle|. |
| 457 bool HasConnectJobForHandle(const ClientSocketHandle* handle) const; | 462 bool HasConnectJobForHandle(const ClientSocketHandle* handle) const; |
| 458 | 463 |
| 459 // Inserts the request into the queue based on priority | 464 // Inserts the request into the queue based on priority |
| 460 // order. Older requests are prioritized over requests of equal | 465 // order. Older requests are prioritized over requests of equal |
| 461 // priority. | 466 // priority. |
| 462 void InsertPendingRequest(std::unique_ptr<const Request> request); | 467 void InsertPendingRequest(std::unique_ptr<Request> request); |
| 463 | 468 |
| 464 // Gets and removes the next pending request. Returns NULL if | 469 // Gets and removes the next pending request. Returns NULL if |
| 465 // there are no pending requests. | 470 // there are no pending requests. |
| 466 std::unique_ptr<const Request> PopNextPendingRequest(); | 471 std::unique_ptr<Request> PopNextPendingRequest(); |
| 467 | 472 |
| 468 // Finds the pending request for |handle| and removes it. Returns | 473 // Finds the pending request for |handle| and removes it. Returns |
| 469 // the removed pending request, or NULL if there was none. | 474 // the removed pending request, or NULL if there was none. |
| 470 std::unique_ptr<const Request> FindAndRemovePendingRequest( | 475 std::unique_ptr<Request> FindAndRemovePendingRequest( |
| 471 ClientSocketHandle* handle); | 476 ClientSocketHandle* handle); |
| 472 | 477 |
| 478 // Change the priority of the request named by |*handle|. |*handle| |
| 479 // must refer to a request currently present in the group. If |priority| |
| 480 // is the same as the current priority of the request, this is a no-op. |
| 481 void SetPriority(ClientSocketHandle* handle, RequestPriority priority); |
| 482 |
| 473 void IncrementActiveSocketCount() { active_socket_count_++; } | 483 void IncrementActiveSocketCount() { active_socket_count_++; } |
| 474 void DecrementActiveSocketCount() { active_socket_count_--; } | 484 void DecrementActiveSocketCount() { active_socket_count_--; } |
| 475 | 485 |
| 476 int unassigned_job_count() const { return unassigned_job_count_; } | 486 int unassigned_job_count() const { return unassigned_job_count_; } |
| 477 const JobList& jobs() const { return jobs_; } | 487 const JobList& jobs() const { return jobs_; } |
| 478 const std::list<IdleSocket>& idle_sockets() const { return idle_sockets_; } | 488 const std::list<IdleSocket>& idle_sockets() const { return idle_sockets_; } |
| 479 int active_socket_count() const { return active_socket_count_; } | 489 int active_socket_count() const { return active_socket_count_; } |
| 480 std::list<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; } | 490 std::list<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; } |
| 481 | 491 |
| 482 private: | 492 private: |
| 483 // Returns the iterator's pending request after removing it from | 493 // Returns the iterator's pending request after removing it from |
| 484 // the queue. | 494 // the queue. |
| 485 std::unique_ptr<const Request> RemovePendingRequest( | 495 std::unique_ptr<Request> RemovePendingRequest( |
| 486 const RequestQueue::Pointer& pointer); | 496 const RequestQueue::Pointer& pointer); |
| 487 | 497 |
| 488 // Called when the backup socket timer fires. | 498 // Called when the backup socket timer fires. |
| 489 void OnBackupJobTimerFired( | 499 void OnBackupJobTimerFired( |
| 490 std::string group_name, | 500 std::string group_name, |
| 491 ClientSocketPoolBaseHelper* pool); | 501 ClientSocketPoolBaseHelper* pool); |
| 492 | 502 |
| 493 // Checks that |unassigned_job_count_| does not execeed the number of | 503 // Checks that |unassigned_job_count_| does not execeed the number of |
| 494 // ConnectJobs. | 504 // ConnectJobs. |
| 495 void SanityCheck(); | 505 void SanityCheck(); |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 746 | 756 |
| 747 // RequestSocket bundles up the parameters into a Request and then forwards to | 757 // RequestSocket bundles up the parameters into a Request and then forwards to |
| 748 // ClientSocketPoolBaseHelper::RequestSocket(). | 758 // ClientSocketPoolBaseHelper::RequestSocket(). |
| 749 int RequestSocket(const std::string& group_name, | 759 int RequestSocket(const std::string& group_name, |
| 750 const scoped_refptr<SocketParams>& params, | 760 const scoped_refptr<SocketParams>& params, |
| 751 RequestPriority priority, | 761 RequestPriority priority, |
| 752 ClientSocketPool::RespectLimits respect_limits, | 762 ClientSocketPool::RespectLimits respect_limits, |
| 753 ClientSocketHandle* handle, | 763 ClientSocketHandle* handle, |
| 754 const CompletionCallback& callback, | 764 const CompletionCallback& callback, |
| 755 const NetLogWithSource& net_log) { | 765 const NetLogWithSource& net_log) { |
| 756 std::unique_ptr<const Request> request(new Request( | 766 std::unique_ptr<Request> request(new Request( |
| 757 handle, callback, priority, respect_limits, | 767 handle, callback, priority, respect_limits, |
| 758 internal::ClientSocketPoolBaseHelper::NORMAL, params, net_log)); | 768 internal::ClientSocketPoolBaseHelper::NORMAL, params, net_log)); |
| 759 return helper_.RequestSocket(group_name, std::move(request)); | 769 return helper_.RequestSocket(group_name, std::move(request)); |
| 760 } | 770 } |
| 761 | 771 |
| 762 // RequestSockets bundles up the parameters into a Request and then forwards | 772 // RequestSockets bundles up the parameters into a Request and then forwards |
| 763 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the | 773 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the |
| 764 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag. | 774 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag. |
| 765 void RequestSockets(const std::string& group_name, | 775 void RequestSockets(const std::string& group_name, |
| 766 const scoped_refptr<SocketParams>& params, | 776 const scoped_refptr<SocketParams>& params, |
| 767 int num_sockets, | 777 int num_sockets, |
| 768 const NetLogWithSource& net_log) { | 778 const NetLogWithSource& net_log) { |
| 769 const Request request(nullptr /* no handle */, CompletionCallback(), IDLE, | 779 const Request request(nullptr /* no handle */, CompletionCallback(), IDLE, |
| 770 ClientSocketPool::RespectLimits::ENABLED, | 780 ClientSocketPool::RespectLimits::ENABLED, |
| 771 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, | 781 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, |
| 772 params, net_log); | 782 params, net_log); |
| 773 helper_.RequestSockets(group_name, request, num_sockets); | 783 helper_.RequestSockets(group_name, request, num_sockets); |
| 774 } | 784 } |
| 775 | 785 |
| 786 void SetPriority(const std::string& group_name, |
| 787 ClientSocketHandle* handle, |
| 788 RequestPriority priority) { |
| 789 return helper_.SetPriority(group_name, handle, priority); |
| 790 } |
| 791 |
| 776 void CancelRequest(const std::string& group_name, | 792 void CancelRequest(const std::string& group_name, |
| 777 ClientSocketHandle* handle) { | 793 ClientSocketHandle* handle) { |
| 778 return helper_.CancelRequest(group_name, handle); | 794 return helper_.CancelRequest(group_name, handle); |
| 779 } | 795 } |
| 780 | 796 |
| 781 void ReleaseSocket(const std::string& group_name, | 797 void ReleaseSocket(const std::string& group_name, |
| 782 std::unique_ptr<StreamSocket> socket, | 798 std::unique_ptr<StreamSocket> socket, |
| 783 int id) { | 799 int id) { |
| 784 return helper_.ReleaseSocket(group_name, std::move(socket), id); | 800 return helper_.ReleaseSocket(group_name, std::move(socket), id); |
| 785 } | 801 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 881 }; | 897 }; |
| 882 | 898 |
| 883 internal::ClientSocketPoolBaseHelper helper_; | 899 internal::ClientSocketPoolBaseHelper helper_; |
| 884 | 900 |
| 885 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 901 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 886 }; | 902 }; |
| 887 | 903 |
| 888 } // namespace net | 904 } // namespace net |
| 889 | 905 |
| 890 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 906 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| OLD | NEW |