websocket_transport_client_socket_pool.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_SOCKET_WEBSOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_
  5. #define NET_SOCKET_WEBSOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_
  6. #include <list>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include <utility>
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/timer/timer.h"
  16. #include "net/base/net_export.h"
  17. #include "net/base/proxy_server.h"
  18. #include "net/log/net_log_with_source.h"
  19. #include "net/socket/client_socket_pool.h"
  20. #include "net/socket/connect_job.h"
  21. #include "net/socket/ssl_client_socket.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. namespace net {
  24. struct CommonConnectJobParams;
  25. struct NetworkTrafficAnnotationTag;
  26. class NET_EXPORT_PRIVATE WebSocketTransportClientSocketPool
  27. : public ClientSocketPool {
  28. public:
  29. WebSocketTransportClientSocketPool(
  30. int max_sockets,
  31. int max_sockets_per_group,
  32. const ProxyServer& proxy_server,
  33. const CommonConnectJobParams* common_connect_job_params);
  34. WebSocketTransportClientSocketPool(
  35. const WebSocketTransportClientSocketPool&) = delete;
  36. WebSocketTransportClientSocketPool& operator=(
  37. const WebSocketTransportClientSocketPool&) = delete;
  38. ~WebSocketTransportClientSocketPool() override;
  39. // Allow another connection to be started to the IPEndPoint that this |handle|
  40. // is connected to. Used when the WebSocket handshake completes successfully.
  41. // This only works if the socket is connected, however the caller does not
  42. // need to explicitly check for this. Instead, ensure that dead sockets are
  43. // returned to ReleaseSocket() in a timely fashion.
  44. static void UnlockEndpoint(
  45. ClientSocketHandle* handle,
  46. WebSocketEndpointLockManager* websocket_endpoint_lock_manager);
  47. // ClientSocketPool implementation.
  48. int RequestSocket(
  49. const GroupId& group_id,
  50. scoped_refptr<SocketParams> params,
  51. const absl::optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag,
  52. RequestPriority priority,
  53. const SocketTag& socket_tag,
  54. RespectLimits respect_limits,
  55. ClientSocketHandle* handle,
  56. CompletionOnceCallback callback,
  57. const ProxyAuthCallback& proxy_auth_callback,
  58. const NetLogWithSource& net_log) override;
  59. int RequestSockets(
  60. const GroupId& group_id,
  61. scoped_refptr<SocketParams> params,
  62. const absl::optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag,
  63. int num_sockets,
  64. CompletionOnceCallback callback,
  65. const NetLogWithSource& net_log) override;
  66. void SetPriority(const GroupId& group_id,
  67. ClientSocketHandle* handle,
  68. RequestPriority priority) override;
  69. void CancelRequest(const GroupId& group_id,
  70. ClientSocketHandle* handle,
  71. bool cancel_connect_job) override;
  72. void ReleaseSocket(const GroupId& group_id,
  73. std::unique_ptr<StreamSocket> socket,
  74. int64_t generation) override;
  75. void FlushWithError(int error, const char* net_log_reason_utf8) override;
  76. void CloseIdleSockets(const char* net_log_reason_utf8) override;
  77. void CloseIdleSocketsInGroup(const GroupId& group_id,
  78. const char* net_log_reason_utf8) override;
  79. int IdleSocketCount() const override;
  80. size_t IdleSocketCountInGroup(const GroupId& group_id) const override;
  81. LoadState GetLoadState(const GroupId& group_id,
  82. const ClientSocketHandle* handle) const override;
  83. base::Value GetInfoAsValue(const std::string& name,
  84. const std::string& type) const override;
  85. bool HasActiveSocket(const GroupId& group_id) const override;
  86. // HigherLayeredPool implementation.
  87. bool IsStalled() const override;
  88. void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override;
  89. void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override;
  90. private:
  91. class ConnectJobDelegate : public ConnectJob::Delegate {
  92. public:
  93. ConnectJobDelegate(WebSocketTransportClientSocketPool* owner,
  94. CompletionOnceCallback callback,
  95. ClientSocketHandle* socket_handle,
  96. const NetLogWithSource& request_net_log);
  97. ConnectJobDelegate(const ConnectJobDelegate&) = delete;
  98. ConnectJobDelegate& operator=(const ConnectJobDelegate&) = delete;
  99. ~ConnectJobDelegate() override;
  100. // ConnectJob::Delegate implementation
  101. void OnConnectJobComplete(int result, ConnectJob* job) override;
  102. void OnNeedsProxyAuth(const HttpResponseInfo& response,
  103. HttpAuthController* auth_controller,
  104. base::OnceClosure restart_with_auth_callback,
  105. ConnectJob* job) override;
  106. // Calls Connect() on |connect_job|, and takes ownership. Returns Connect's
  107. // return value.
  108. int Connect(std::unique_ptr<ConnectJob> connect_job);
  109. CompletionOnceCallback release_callback() { return std::move(callback_); }
  110. ConnectJob* connect_job() { return connect_job_.get(); }
  111. ClientSocketHandle* socket_handle() { return socket_handle_; }
  112. const NetLogWithSource& request_net_log() { return request_net_log_; }
  113. const NetLogWithSource& connect_job_net_log();
  114. private:
  115. raw_ptr<WebSocketTransportClientSocketPool> owner_;
  116. CompletionOnceCallback callback_;
  117. std::unique_ptr<ConnectJob> connect_job_;
  118. const raw_ptr<ClientSocketHandle> socket_handle_;
  119. const NetLogWithSource request_net_log_;
  120. };
  121. // Store the arguments from a call to RequestSocket() that has stalled so we
  122. // can replay it when there are available socket slots.
  123. struct StalledRequest {
  124. StalledRequest(
  125. const GroupId& group_id,
  126. const scoped_refptr<SocketParams>& params,
  127. const absl::optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag,
  128. RequestPriority priority,
  129. ClientSocketHandle* handle,
  130. CompletionOnceCallback callback,
  131. const ProxyAuthCallback& proxy_auth_callback,
  132. const NetLogWithSource& net_log);
  133. StalledRequest(StalledRequest&& other);
  134. ~StalledRequest();
  135. const GroupId group_id;
  136. const scoped_refptr<SocketParams> params;
  137. const absl::optional<NetworkTrafficAnnotationTag> proxy_annotation_tag;
  138. const RequestPriority priority;
  139. const raw_ptr<ClientSocketHandle> handle;
  140. CompletionOnceCallback callback;
  141. ProxyAuthCallback proxy_auth_callback;
  142. const NetLogWithSource net_log;
  143. };
  144. typedef std::map<const ClientSocketHandle*,
  145. std::unique_ptr<ConnectJobDelegate>>
  146. PendingConnectsMap;
  147. // This is a list so that we can remove requests from the middle, and also
  148. // so that iterators are not invalidated unless the corresponding request is
  149. // removed.
  150. typedef std::list<StalledRequest> StalledRequestQueue;
  151. typedef std::map<const ClientSocketHandle*, StalledRequestQueue::iterator>
  152. StalledRequestMap;
  153. // Tries to hand out the socket connected by |job|. |result| must be (async)
  154. // result of TransportConnectJob::Connect(). Returns true iff it has handed
  155. // out a socket.
  156. bool TryHandOutSocket(int result, ConnectJobDelegate* connect_job_delegate);
  157. void OnConnectJobComplete(int result,
  158. ConnectJobDelegate* connect_job_delegate);
  159. void InvokeUserCallbackLater(ClientSocketHandle* handle,
  160. CompletionOnceCallback callback,
  161. int rv);
  162. void InvokeUserCallback(ClientSocketHandle* handle,
  163. CompletionOnceCallback callback,
  164. int rv);
  165. bool ReachedMaxSocketsLimit() const;
  166. void HandOutSocket(std::unique_ptr<StreamSocket> socket,
  167. const LoadTimingInfo::ConnectTiming& connect_timing,
  168. ClientSocketHandle* handle,
  169. const NetLogWithSource& net_log);
  170. void AddJob(ClientSocketHandle* handle,
  171. std::unique_ptr<ConnectJobDelegate> delegate);
  172. bool DeleteJob(ClientSocketHandle* handle);
  173. const ConnectJob* LookupConnectJob(const ClientSocketHandle* handle) const;
  174. void ActivateStalledRequest();
  175. bool DeleteStalledRequest(ClientSocketHandle* handle);
  176. const ProxyServer proxy_server_;
  177. std::set<const ClientSocketHandle*> pending_callbacks_;
  178. PendingConnectsMap pending_connects_;
  179. StalledRequestQueue stalled_request_queue_;
  180. StalledRequestMap stalled_request_map_;
  181. const int max_sockets_;
  182. int handed_out_socket_count_ = 0;
  183. bool flushing_ = false;
  184. base::WeakPtrFactory<WebSocketTransportClientSocketPool> weak_factory_{this};
  185. };
  186. } // namespace net
  187. #endif // NET_SOCKET_WEBSOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_