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

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

Issue 1898133002: Add reprioritization to socket pools. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Plumbed SetPriority down from HttpStreamFactoryImpl::Job. Created 4 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
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 // 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
181 RequestPriority priority, 181 RequestPriority priority,
182 ClientSocketPool::RespectLimits respect_limits, 182 ClientSocketPool::RespectLimits respect_limits,
183 Flags flags, 183 Flags flags,
184 const BoundNetLog& net_log); 184 const BoundNetLog& net_log);
185 185
186 virtual ~Request(); 186 virtual ~Request();
187 187
188 ClientSocketHandle* handle() const { return handle_; } 188 ClientSocketHandle* handle() const { return handle_; }
189 const CompletionCallback& callback() const { return callback_; } 189 const CompletionCallback& callback() const { return callback_; }
190 RequestPriority priority() const { return priority_; } 190 RequestPriority priority() const { return priority_; }
191 void set_priority(RequestPriority priority) { priority_ = priority; }
191 ClientSocketPool::RespectLimits respect_limits() const { 192 ClientSocketPool::RespectLimits respect_limits() const {
192 return respect_limits_; 193 return respect_limits_;
193 } 194 }
194 Flags flags() const { return flags_; } 195 Flags flags() const { return flags_; }
195 const BoundNetLog& net_log() const { return net_log_; } 196 const BoundNetLog& net_log() const { return net_log_; }
196 197
197 // TODO(eroman): Temporary until crbug.com/467797 is solved. 198 // TODO(eroman): Temporary until crbug.com/467797 is solved.
198 void CrashIfInvalid() const; 199 void CrashIfInvalid() const;
199 200
200 private: 201 private:
201 // TODO(eroman): Temporary until crbug.com/467797 is solved. 202 // TODO(eroman): Temporary until crbug.com/467797 is solved.
202 enum Liveness { 203 enum Liveness {
203 ALIVE = 0xCA11AB13, 204 ALIVE = 0xCA11AB13,
204 DEAD = 0xDEADBEEF, 205 DEAD = 0xDEADBEEF,
205 }; 206 };
206 207
207 ClientSocketHandle* const handle_; 208 ClientSocketHandle* const handle_;
208 const CompletionCallback callback_; 209 const CompletionCallback callback_;
209 // TODO(akalin): Support reprioritization. 210 RequestPriority priority_;
210 const RequestPriority priority_;
211 const ClientSocketPool::RespectLimits respect_limits_; 211 const ClientSocketPool::RespectLimits respect_limits_;
212 const Flags flags_; 212 const Flags flags_;
213 const BoundNetLog net_log_; 213 const BoundNetLog net_log_;
214 214
215 // TODO(eroman): Temporary until crbug.com/467797 is solved. 215 // TODO(eroman): Temporary until crbug.com/467797 is solved.
216 Liveness liveness_ = ALIVE; 216 Liveness liveness_ = ALIVE;
217 217
218 DISALLOW_COPY_AND_ASSIGN(Request); 218 DISALLOW_COPY_AND_ASSIGN(Request);
219 }; 219 };
220 220
(...skipping 30 matching lines...) Expand all
251 // See LowerLayeredPool::IsStalled for documentation on this function. 251 // See LowerLayeredPool::IsStalled for documentation on this function.
252 bool IsStalled() const; 252 bool IsStalled() const;
253 253
254 // See LowerLayeredPool for documentation on these functions. It is expected 254 // See LowerLayeredPool for documentation on these functions. It is expected
255 // in the destructor that no higher layer pools remain. 255 // in the destructor that no higher layer pools remain.
256 void AddHigherLayeredPool(HigherLayeredPool* higher_pool); 256 void AddHigherLayeredPool(HigherLayeredPool* higher_pool);
257 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool); 257 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool);
258 258
259 // See ClientSocketPool::RequestSocket for documentation on this function. 259 // See ClientSocketPool::RequestSocket for documentation on this function.
260 int RequestSocket(const std::string& group_name, 260 int RequestSocket(const std::string& group_name,
261 std::unique_ptr<const Request> request); 261 std::unique_ptr<Request> request);
262 262
263 // See ClientSocketPool::RequestSocket for documentation on this function. 263 // See ClientSocketPool::RequestSockets for documentation on this function.
264 void RequestSockets(const std::string& group_name, 264 void RequestSockets(const std::string& group_name,
265 const Request& request, 265 const Request& request,
266 int num_sockets); 266 int num_sockets);
267 267
268 // See ClientSocketPool::SetPriority for documentation on this function.
269 void SetPriority(const std::string& group_name,
270 ClientSocketHandle* handle,
271 RequestPriority priority);
272
268 // See ClientSocketPool::CancelRequest for documentation on this function. 273 // See ClientSocketPool::CancelRequest for documentation on this function.
269 void CancelRequest(const std::string& group_name, 274 void CancelRequest(const std::string& group_name,
270 ClientSocketHandle* handle); 275 ClientSocketHandle* handle);
271 276
272 // See ClientSocketPool::ReleaseSocket for documentation on this function. 277 // See ClientSocketPool::ReleaseSocket for documentation on this function.
273 void ReleaseSocket(const std::string& group_name, 278 void ReleaseSocket(const std::string& group_name,
274 std::unique_ptr<StreamSocket> socket, 279 std::unique_ptr<StreamSocket> socket,
275 int id); 280 int id);
276 281
277 // See ClientSocketPool::FlushWithError for documentation on this function. 282 // See ClientSocketPool::FlushWithError for documentation on this function.
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 378
374 // An idle socket should be removed if it can't be reused, or has been idle 379 // An idle socket should be removed if it can't be reused, or has been idle
375 // for too long. |now| is the current time value (TimeTicks::Now()). 380 // for too long. |now| is the current time value (TimeTicks::Now()).
376 // |timeout| is the length of time to wait before timing out an idle socket. 381 // |timeout| is the length of time to wait before timing out an idle socket.
377 bool ShouldCleanup(base::TimeTicks now, base::TimeDelta timeout) const; 382 bool ShouldCleanup(base::TimeTicks now, base::TimeDelta timeout) 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 Group(); 396 Group();
392 ~Group(); 397 ~Group();
393 398
394 bool IsEmpty() const { 399 bool IsEmpty() const {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 // Gets (but does not remove) the next pending request. Returns 455 // Gets (but does not remove) the next pending request. Returns
451 // NULL if there are no pending requests. 456 // NULL if there are no pending requests.
452 const Request* GetNextPendingRequest() const; 457 const Request* GetNextPendingRequest() const;
453 458
454 // Returns true if there is a connect job for |handle|. 459 // Returns true if there is a connect job for |handle|.
455 bool HasConnectJobForHandle(const ClientSocketHandle* handle) const; 460 bool HasConnectJobForHandle(const ClientSocketHandle* handle) const;
456 461
457 // Inserts the request into the queue based on priority 462 // Inserts the request into the queue based on priority
458 // order. Older requests are prioritized over requests of equal 463 // order. Older requests are prioritized over requests of equal
459 // priority. 464 // priority.
460 void InsertPendingRequest(std::unique_ptr<const Request> request); 465 void InsertPendingRequest(std::unique_ptr<Request> request);
461 466
462 // Gets and removes the next pending request. Returns NULL if 467 // Gets and removes the next pending request. Returns NULL if
463 // there are no pending requests. 468 // there are no pending requests.
464 std::unique_ptr<const Request> PopNextPendingRequest(); 469 std::unique_ptr<Request> PopNextPendingRequest();
465 470
466 // Finds the pending request for |handle| and removes it. Returns 471 // Finds the pending request for |handle| and removes it. Returns
467 // the removed pending request, or NULL if there was none. 472 // the removed pending request, or NULL if there was none.
468 std::unique_ptr<const Request> FindAndRemovePendingRequest( 473 std::unique_ptr<Request> FindAndRemovePendingRequest(
469 ClientSocketHandle* handle); 474 ClientSocketHandle* handle);
470 475
471 void IncrementActiveSocketCount() { active_socket_count_++; } 476 void IncrementActiveSocketCount() { active_socket_count_++; }
472 void DecrementActiveSocketCount() { active_socket_count_--; } 477 void DecrementActiveSocketCount() { active_socket_count_--; }
473 478
474 int unassigned_job_count() const { return unassigned_job_count_; } 479 int unassigned_job_count() const { return unassigned_job_count_; }
475 const std::list<ConnectJob*>& jobs() const { return jobs_; } 480 const std::list<ConnectJob*>& jobs() const { return jobs_; }
476 const std::list<IdleSocket>& idle_sockets() const { return idle_sockets_; } 481 const std::list<IdleSocket>& idle_sockets() const { return idle_sockets_; }
477 int active_socket_count() const { return active_socket_count_; } 482 int active_socket_count() const { return active_socket_count_; }
478 std::list<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; } 483 std::list<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; }
479 484
480 private: 485 private:
481 // Returns the iterator's pending request after removing it from 486 // Returns the iterator's pending request after removing it from
482 // the queue. 487 // the queue.
483 std::unique_ptr<const Request> RemovePendingRequest( 488 std::unique_ptr<Request> RemovePendingRequest(
484 const RequestQueue::Pointer& pointer); 489 const RequestQueue::Pointer& pointer);
485 490
486 // Called when the backup socket timer fires. 491 // Called when the backup socket timer fires.
487 void OnBackupJobTimerFired( 492 void OnBackupJobTimerFired(
488 std::string group_name, 493 std::string group_name,
489 ClientSocketPoolBaseHelper* pool); 494 ClientSocketPoolBaseHelper* pool);
490 495
491 // Checks that |unassigned_job_count_| does not execeed the number of 496 // Checks that |unassigned_job_count_| does not execeed the number of
492 // ConnectJobs. 497 // ConnectJobs.
493 void SanityCheck(); 498 void SanityCheck();
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 764
760 // RequestSocket bundles up the parameters into a Request and then forwards to 765 // RequestSocket bundles up the parameters into a Request and then forwards to
761 // ClientSocketPoolBaseHelper::RequestSocket(). 766 // ClientSocketPoolBaseHelper::RequestSocket().
762 int RequestSocket(const std::string& group_name, 767 int RequestSocket(const std::string& group_name,
763 const scoped_refptr<SocketParams>& params, 768 const scoped_refptr<SocketParams>& params,
764 RequestPriority priority, 769 RequestPriority priority,
765 ClientSocketPool::RespectLimits respect_limits, 770 ClientSocketPool::RespectLimits respect_limits,
766 ClientSocketHandle* handle, 771 ClientSocketHandle* handle,
767 const CompletionCallback& callback, 772 const CompletionCallback& callback,
768 const BoundNetLog& net_log) { 773 const BoundNetLog& net_log) {
769 std::unique_ptr<const Request> request(new Request( 774 std::unique_ptr<Request> request(new Request(
770 handle, callback, priority, respect_limits, 775 handle, callback, priority, respect_limits,
771 internal::ClientSocketPoolBaseHelper::NORMAL, params, net_log)); 776 internal::ClientSocketPoolBaseHelper::NORMAL, params, net_log));
772 return helper_.RequestSocket(group_name, std::move(request)); 777 return helper_.RequestSocket(group_name, std::move(request));
773 } 778 }
774 779
775 // RequestSockets bundles up the parameters into a Request and then forwards 780 // RequestSockets bundles up the parameters into a Request and then forwards
776 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the 781 // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the
777 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag. 782 // priority to IDLE and specifies the NO_IDLE_SOCKETS flag.
778 void RequestSockets(const std::string& group_name, 783 void RequestSockets(const std::string& group_name,
779 const scoped_refptr<SocketParams>& params, 784 const scoped_refptr<SocketParams>& params,
780 int num_sockets, 785 int num_sockets,
781 const BoundNetLog& net_log) { 786 const BoundNetLog& net_log) {
782 const Request request(nullptr /* no handle */, CompletionCallback(), IDLE, 787 const Request request(nullptr /* no handle */, CompletionCallback(), IDLE,
783 ClientSocketPool::RespectLimits::ENABLED, 788 ClientSocketPool::RespectLimits::ENABLED,
784 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, 789 internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS,
785 params, net_log); 790 params, net_log);
786 helper_.RequestSockets(group_name, request, num_sockets); 791 helper_.RequestSockets(group_name, request, num_sockets);
787 } 792 }
788 793
794 void SetPriority(const std::string& group_name,
795 ClientSocketHandle* handle,
796 RequestPriority priority) {
797 return helper_.SetPriority(group_name, handle, priority);
798 }
799
789 void CancelRequest(const std::string& group_name, 800 void CancelRequest(const std::string& group_name,
790 ClientSocketHandle* handle) { 801 ClientSocketHandle* handle) {
791 return helper_.CancelRequest(group_name, handle); 802 return helper_.CancelRequest(group_name, handle);
792 } 803 }
793 804
794 void ReleaseSocket(const std::string& group_name, 805 void ReleaseSocket(const std::string& group_name,
795 std::unique_ptr<StreamSocket> socket, 806 std::unique_ptr<StreamSocket> socket,
796 int id) { 807 int id) {
797 return helper_.ReleaseSocket(group_name, std::move(socket), id); 808 return helper_.ReleaseSocket(group_name, std::move(socket), id);
798 } 809 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 }; 900 };
890 901
891 internal::ClientSocketPoolBaseHelper helper_; 902 internal::ClientSocketPoolBaseHelper helper_;
892 903
893 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); 904 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase);
894 }; 905 };
895 906
896 } // namespace net 907 } // namespace net
897 908
898 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ 909 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698