websocket_endpoint_lock_manager.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_ENDPOINT_LOCK_MANAGER_H_
  5. #define NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include "base/containers/linked_list.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/time/time.h"
  12. #include "net/base/ip_endpoint.h"
  13. #include "net/base/net_export.h"
  14. #include "net/socket/websocket_transport_client_socket_pool.h"
  15. namespace net {
  16. // Keep track of ongoing WebSocket connections in order to satisfy the WebSocket
  17. // connection throttling requirements described in RFC6455 4.1.2:
  18. //
  19. // 2. If the client already has a WebSocket connection to the remote
  20. // host (IP address) identified by /host/ and port /port/ pair, even
  21. // if the remote host is known by another name, the client MUST wait
  22. // until that connection has been established or for that connection
  23. // to have failed. There MUST be no more than one connection in a
  24. // CONNECTING state. If multiple connections to the same IP address
  25. // are attempted simultaneously, the client MUST serialize them so
  26. // that there is no more than one connection at a time running
  27. // through the following steps.
  28. //
  29. class NET_EXPORT_PRIVATE WebSocketEndpointLockManager {
  30. public:
  31. // Implement this interface to wait for an endpoint to be available.
  32. class NET_EXPORT_PRIVATE Waiter : public base::LinkNode<Waiter> {
  33. public:
  34. // If the node is in a list, removes it.
  35. virtual ~Waiter();
  36. virtual void GotEndpointLock() = 0;
  37. };
  38. // LockReleaser calls UnlockEndpoint() when it is destroyed, but only if it
  39. // has not already been called. Only one LockReleaser object may exist for
  40. // each endpoint at a time.
  41. class NET_EXPORT_PRIVATE LockReleaser final {
  42. public:
  43. LockReleaser(WebSocketEndpointLockManager* websocket_endpoint_lock_manager,
  44. IPEndPoint endpoint);
  45. LockReleaser(const LockReleaser&) = delete;
  46. LockReleaser& operator=(const LockReleaser&) = delete;
  47. ~LockReleaser();
  48. private:
  49. friend class WebSocketEndpointLockManager;
  50. // This is null if UnlockEndpoint() has been called before this object was
  51. // destroyed.
  52. raw_ptr<WebSocketEndpointLockManager> websocket_endpoint_lock_manager_;
  53. const IPEndPoint endpoint_;
  54. };
  55. WebSocketEndpointLockManager();
  56. WebSocketEndpointLockManager(const WebSocketEndpointLockManager&) = delete;
  57. WebSocketEndpointLockManager& operator=(const WebSocketEndpointLockManager&) =
  58. delete;
  59. ~WebSocketEndpointLockManager();
  60. // Returns OK if lock was acquired immediately, ERR_IO_PENDING if not. If the
  61. // lock was not acquired, then |waiter->GotEndpointLock()| will be called when
  62. // it is. A Waiter automatically removes itself from the list of waiters when
  63. // its destructor is called.
  64. int LockEndpoint(const IPEndPoint& endpoint, Waiter* waiter);
  65. // Asynchronously releases the lock on |endpoint| after a delay. Does nothing
  66. // if |endpoint| is not locked. If a LockReleaser object has been created for
  67. // this endpoint, it will be unregistered.
  68. void UnlockEndpoint(const IPEndPoint& endpoint);
  69. // Checks that |lock_info_map_| is empty. For tests.
  70. bool IsEmpty() const;
  71. // Changes the value of the unlock delay. Returns the previous value of the
  72. // delay.
  73. base::TimeDelta SetUnlockDelayForTesting(base::TimeDelta new_delay);
  74. private:
  75. struct LockInfo {
  76. typedef base::LinkedList<Waiter> WaiterQueue;
  77. LockInfo();
  78. ~LockInfo();
  79. // This object must be copyable to be placed in the map, but it cannot be
  80. // copied after |queue| has been assigned to.
  81. LockInfo(const LockInfo& rhs);
  82. // Not used.
  83. LockInfo& operator=(const LockInfo& rhs);
  84. // Must be NULL to copy this object into the map. Must be set to non-NULL
  85. // after the object is inserted into the map then point to the same list
  86. // until this object is deleted.
  87. std::unique_ptr<WaiterQueue> queue;
  88. // This pointer is non-NULL if a LockReleaser object has been constructed
  89. // since the last call to UnlockEndpoint().
  90. raw_ptr<LockReleaser> lock_releaser;
  91. };
  92. // SocketLockInfoMap requires std::map iterator semantics for LockInfoMap
  93. // (ie. that the iterator will remain valid as long as the entry is not
  94. // deleted).
  95. typedef std::map<IPEndPoint, LockInfo> LockInfoMap;
  96. // Records the association of a LockReleaser with a particular endpoint.
  97. void RegisterLockReleaser(LockReleaser* lock_releaser, IPEndPoint endpoint);
  98. void UnlockEndpointAfterDelay(const IPEndPoint& endpoint);
  99. void DelayedUnlockEndpoint(const IPEndPoint& endpoint);
  100. // If an entry is present in the map for a particular endpoint, then that
  101. // endpoint is locked. If LockInfo.queue is non-empty, then one or more
  102. // Waiters are waiting for the lock.
  103. LockInfoMap lock_info_map_;
  104. // Time to wait between a call to Unlock* and actually unlocking the socket.
  105. base::TimeDelta unlock_delay_;
  106. // Number of sockets currently pending unlock.
  107. size_t pending_unlock_count_ = 0;
  108. base::WeakPtrFactory<WebSocketEndpointLockManager> weak_factory_{this};
  109. };
  110. } // namespace net
  111. #endif // NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_