OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef NET_BASE_CLIENT_SOCKET_POOL_H_ | 5 #ifndef NET_BASE_CLIENT_SOCKET_POOL_H_ |
6 #define NET_BASE_CLIENT_SOCKET_POOL_H_ | 6 #define NET_BASE_CLIENT_SOCKET_POOL_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 70 } |
71 | 71 |
72 private: | 72 private: |
73 friend class base::RefCounted<ClientSocketPool>; | 73 friend class base::RefCounted<ClientSocketPool>; |
74 | 74 |
75 typedef scoped_ptr<ClientSocket> ClientSocketPtr; | 75 typedef scoped_ptr<ClientSocket> ClientSocketPtr; |
76 | 76 |
77 ~ClientSocketPool(); | 77 ~ClientSocketPool(); |
78 | 78 |
79 // Closes all idle sockets if |force| is true. Else, only closes idle | 79 // Closes all idle sockets if |force| is true. Else, only closes idle |
80 // sockets that are disconnected or timed out. | 80 // sockets that timed out or can't be reused. |
81 void CleanupIdleSockets(bool force); | 81 void CleanupIdleSockets(bool force); |
82 | 82 |
83 // Called when the number of idle sockets changes. | 83 // Called when the number of idle sockets changes. |
84 void IncrementIdleCount(); | 84 void IncrementIdleCount(); |
85 void DecrementIdleCount(); | 85 void DecrementIdleCount(); |
86 | 86 |
87 // Called via PostTask by ReleaseSocket. | 87 // Called via PostTask by ReleaseSocket. |
88 void DoReleaseSocket(const std::string& group_name, ClientSocketPtr* ptr); | 88 void DoReleaseSocket(const std::string& group_name, ClientSocketPtr* ptr); |
89 | 89 |
90 // Called when timer_ fires. This method scans the idle sockets removing | 90 // Called when timer_ fires. This method scans the idle sockets removing |
91 // sockets that are disconnected or timed out. | 91 // sockets that timed out or can't be reused. |
92 void OnCleanupTimerFired() { | 92 void OnCleanupTimerFired() { |
93 CleanupIdleSockets(false); | 93 CleanupIdleSockets(false); |
94 } | 94 } |
95 | 95 |
96 // A Request is allocated per call to RequestSocket that results in | 96 // A Request is allocated per call to RequestSocket that results in |
97 // ERR_IO_PENDING. | 97 // ERR_IO_PENDING. |
98 struct Request { | 98 struct Request { |
99 ClientSocketHandle* handle; | 99 ClientSocketHandle* handle; |
100 CompletionCallback* callback; | 100 CompletionCallback* callback; |
101 }; | 101 }; |
102 | 102 |
103 // Entry for a persistent socket which became idle at time |start_time|. | 103 // Entry for a persistent socket which became idle at time |start_time|. |
104 struct IdleSocket { | 104 struct IdleSocket { |
105 ClientSocketPtr* ptr; | 105 ClientSocketPtr* ptr; |
106 base::TimeTicks start_time; | 106 base::TimeTicks start_time; |
107 | 107 |
108 // An idle socket should be removed if it is disconnected, or has been idle | 108 // An idle socket should be removed if it can't be reused, or has been idle |
109 // for too long. |now| is the current time value (TimeTicks::Now()). | 109 // for too long. |now| is the current time value (TimeTicks::Now()). |
| 110 // |
| 111 // An idle socket can't be reused if it is disconnected or has received |
| 112 // data unexpectedly (hence no longer idle). The unread data would be |
| 113 // mistaken for the beginning of the next response if we were to reuse the |
| 114 // socket for a new request. |
110 bool ShouldCleanup(base::TimeTicks now) const; | 115 bool ShouldCleanup(base::TimeTicks now) const; |
111 }; | 116 }; |
112 | 117 |
113 // A Group is allocated per group_name when there are idle sockets or pending | 118 // A Group is allocated per group_name when there are idle sockets or pending |
114 // requests. Otherwise, the Group object is removed from the map. | 119 // requests. Otherwise, the Group object is removed from the map. |
115 struct Group { | 120 struct Group { |
116 Group() : active_socket_count(0) {} | 121 Group() : active_socket_count(0) {} |
117 std::deque<IdleSocket> idle_sockets; | 122 std::deque<IdleSocket> idle_sockets; |
118 std::deque<Request> pending_requests; | 123 std::deque<Request> pending_requests; |
119 int active_socket_count; | 124 int active_socket_count; |
120 }; | 125 }; |
121 | 126 |
122 typedef std::map<std::string, Group> GroupMap; | 127 typedef std::map<std::string, Group> GroupMap; |
123 GroupMap group_map_; | 128 GroupMap group_map_; |
124 | 129 |
125 // Timer used to periodically prune idle sockets that are disconnected or | 130 // Timer used to periodically prune idle sockets that timed out or can't be |
126 // timed out. | 131 // reused. |
127 base::RepeatingTimer<ClientSocketPool> timer_; | 132 base::RepeatingTimer<ClientSocketPool> timer_; |
128 | 133 |
129 // The total number of idle sockets in the system. | 134 // The total number of idle sockets in the system. |
130 int idle_socket_count_; | 135 int idle_socket_count_; |
131 | 136 |
132 // The maximum number of sockets kept per group. | 137 // The maximum number of sockets kept per group. |
133 int max_sockets_per_group_; | 138 int max_sockets_per_group_; |
134 | 139 |
135 DISALLOW_COPY_AND_ASSIGN(ClientSocketPool); | 140 DISALLOW_COPY_AND_ASSIGN(ClientSocketPool); |
136 }; | 141 }; |
137 | 142 |
138 } // namespace net | 143 } // namespace net |
139 | 144 |
140 #endif // NET_BASE_CLIENT_SOCKET_POOL_H_ | 145 #endif // NET_BASE_CLIENT_SOCKET_POOL_H_ |
141 | 146 |
OLD | NEW |