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

Unified Diff: net/socket/client_socket_pool_base.h

Issue 3171017: Only create the backup ConnectJob when it is needed. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Address mbelshe's comments. Created 10 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | net/socket/client_socket_pool_base.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/socket/client_socket_pool_base.h
diff --git a/net/socket/client_socket_pool_base.h b/net/socket/client_socket_pool_base.h
index 96c78d59c987c3d471c713c8673c19b0962c298d..33f8072603dd1727c693ca5c959a1cdbca1593fe 100644
--- a/net/socket/client_socket_pool_base.h
+++ b/net/socket/client_socket_pool_base.h
@@ -228,7 +228,7 @@ class ClientSocketPoolBaseHelper
virtual void OnIPAddressChanged();
int NumConnectJobsInGroup(const std::string& group_name) const {
- return group_map_.find(group_name)->second.jobs.size();
+ return group_map_.find(group_name)->second->jobs().size();
}
// Closes all idle sockets if |force| is true. Else, only closes idle
@@ -268,57 +268,69 @@ class ClientSocketPoolBaseHelper
// A Group is allocated per group_name when there are idle sockets or pending
// requests. Otherwise, the Group object is removed from the map.
// |active_socket_count| tracks the number of sockets held by clients.
- struct Group {
- Group()
- : active_socket_count(0),
- backup_job(NULL),
- backup_task(NULL) {
- }
-
- ~Group() {
- CleanupBackupJob();
- }
+ class Group {
+ public:
+ Group();
+ ~Group();
bool IsEmpty() const {
- return active_socket_count == 0 && idle_sockets.empty() && jobs.empty() &&
- pending_requests.empty();
+ return active_socket_count_ == 0 && idle_sockets_.empty() &&
+ jobs_.empty() && pending_requests_.empty();
}
bool HasAvailableSocketSlot(int max_sockets_per_group) const {
- return active_socket_count + static_cast<int>(jobs.size()) <
+ return active_socket_count_ + static_cast<int>(jobs_.size()) <
max_sockets_per_group;
}
bool IsStalled(int max_sockets_per_group) const {
return HasAvailableSocketSlot(max_sockets_per_group) &&
- pending_requests.size() > jobs.size();
+ pending_requests_.size() > jobs_.size();
}
RequestPriority TopPendingPriority() const {
- return pending_requests.front()->priority();
+ return pending_requests_.front()->priority();
}
+ bool HasBackupJob() const { return !method_factory_.empty(); }
+
void CleanupBackupJob() {
- if (backup_job) {
- delete backup_job;
- backup_job = NULL;
- }
- if (backup_task) {
- backup_task->Cancel();
- backup_task = NULL;
- }
+ method_factory_.RevokeAll();
}
- std::deque<IdleSocket> idle_sockets;
- std::set<const ConnectJob*> jobs;
- RequestQueue pending_requests;
- int active_socket_count; // number of active sockets used by clients
- // A backup job in case the connect for this group takes too long.
- ConnectJob* backup_job;
- CancelableTask* backup_task;
+ // Set a timer to create a backup socket if it takes too long to create one.
+ void StartBackupSocketTimer(const std::string& group_name,
+ ClientSocketPoolBaseHelper* pool);
+
+ // Called when the backup socket timer fires.
+ void OnBackupSocketTimerFired(
+ std::string group_name,
+ ClientSocketPoolBaseHelper* pool);
+
+ void AddJob(const ConnectJob* job) { jobs_.insert(job); }
+ void RemoveJob(const ConnectJob* job) { jobs_.erase(job); }
+ void RemoveAllJobs();
+
+ void IncrementActiveSocketCount() { active_socket_count_++; }
+ void DecrementActiveSocketCount() { active_socket_count_--; }
+
+ const std::set<const ConnectJob*>& jobs() const { return jobs_; }
+ const std::deque<IdleSocket>& idle_sockets() const { return idle_sockets_; }
+ const RequestQueue& pending_requests() const { return pending_requests_; }
+ int active_socket_count() const { return active_socket_count_; }
+ RequestQueue* mutable_pending_requests() { return &pending_requests_; }
+ std::deque<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; }
+
+ private:
+ std::deque<IdleSocket> idle_sockets_;
+ std::set<const ConnectJob*> jobs_;
+ RequestQueue pending_requests_;
+ int active_socket_count_; // number of active sockets used by clients
+ // A factory to pin the backup_job tasks.
+ ScopedRunnableMethodFactory<Group> method_factory_;
};
- typedef std::map<std::string, Group> GroupMap;
+ typedef std::map<std::string, Group*> GroupMap;
typedef std::set<const ConnectJob*> ConnectJobSet;
@@ -341,6 +353,10 @@ class ClientSocketPoolBaseHelper
static const Request* RemoveRequestFromQueue(RequestQueue::iterator it,
RequestQueue* pending_requests);
+ Group* GetOrCreateGroup(const std::string& group_name);
+ void RemoveGroup(const std::string& group_name);
+ void RemoveGroup(GroupMap::iterator it);
+
// Called when the number of idle sockets changes.
void IncrementIdleCount();
void DecrementIdleCount();
@@ -394,17 +410,11 @@ class ClientSocketPoolBaseHelper
// Assigns an idle socket for the group to the request.
// Returns |true| if an idle socket is available, false otherwise.
- bool AssignIdleSocketToGroup(Group* group, const Request* request);
+ bool AssignIdleSocketToGroup(const Request* request, Group* group);
static void LogBoundConnectJobToRequest(
const NetLog::Source& connect_job_source, const Request* request);
- // Set a timer to create a backup socket if it takes too long to create one.
- void StartBackupSocketTimer(const std::string& group_name);
-
- // Called when the backup socket timer fires.
- void OnBackupSocketTimerFired(const std::string& group_name);
-
// Closes one idle socket. Picks the first one encountered.
// TODO(willchan): Consider a better algorithm for doing this. Perhaps we
// should keep an ordered list of idle sockets, and close them in order.
@@ -463,9 +473,6 @@ class ClientSocketPoolBaseHelper
// TODO(vandebo) Remove when backup jobs move to TCPClientSocketPool
bool backup_jobs_enabled_;
- // A factory to pin the backup_job tasks.
- ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_;
-
// A unique id for the pool. It gets incremented every time we Flush() the
// pool. This is so that when sockets get released back to the pool, we can
// make sure that they are discarded rather than reused.
« no previous file with comments | « no previous file | net/socket/client_socket_pool_base.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698