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

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

Issue 835623003: Add a delay when unlocking WebSocket endpoints. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a "Scoped" prefix to WebSocketEndpointZeroUnlockDela Created 5 years, 11 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 2014 The Chromium Authors. All rights reserved. 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 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_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_ 5 #ifndef NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_
6 #define NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_ 6 #define NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_
7 7
8 #include <map> 8 #include <map>
9 9
10 #include "base/containers/linked_list.h" 10 #include "base/containers/linked_list.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/singleton.h" 13 #include "base/memory/singleton.h"
14 #include "base/time/time.h"
14 #include "net/base/ip_endpoint.h" 15 #include "net/base/ip_endpoint.h"
15 #include "net/base/net_export.h" 16 #include "net/base/net_export.h"
16 #include "net/socket/websocket_transport_client_socket_pool.h" 17 #include "net/socket/websocket_transport_client_socket_pool.h"
17 18
18 namespace net { 19 namespace net {
19 20
20 class StreamSocket; 21 class StreamSocket;
21 22
22 class NET_EXPORT_PRIVATE WebSocketEndpointLockManager { 23 class NET_EXPORT_PRIVATE WebSocketEndpointLockManager {
mmenke 2015/01/20 15:50:53 Mind adding some class level documentation? I've
Adam Rice 2015/01/21 08:54:02 Wow. Not only did I fail to document it here, I on
23 public: 24 public:
24 class NET_EXPORT_PRIVATE Waiter : public base::LinkNode<Waiter> { 25 class NET_EXPORT_PRIVATE Waiter : public base::LinkNode<Waiter> {
25 public: 26 public:
26 // If the node is in a list, removes it. 27 // If the node is in a list, removes it.
27 virtual ~Waiter(); 28 virtual ~Waiter();
28 29
29 virtual void GotEndpointLock() = 0; 30 virtual void GotEndpointLock() = 0;
30 }; 31 };
31 32
32 static WebSocketEndpointLockManager* GetInstance(); 33 static WebSocketEndpointLockManager* GetInstance();
mmenke 2015/01/20 15:50:53 Unrelated to this CL, but should this really be a
mmenke 2015/01/20 17:46:21 And to elaborte: This is the case with Chrome, it
Adam Rice 2015/01/21 08:54:02 Originally I didn't know what its lifetime should
33 34
34 // Returns OK if lock was acquired immediately, ERR_IO_PENDING if not. If the 35 // Returns OK if lock was acquired immediately, ERR_IO_PENDING if not. If the
35 // lock was not acquired, then |waiter->GotEndpointLock()| will be called when 36 // lock was not acquired, then |waiter->GotEndpointLock()| will be called when
36 // it is. A Waiter automatically removes itself from the list of waiters when 37 // it is. A Waiter automatically removes itself from the list of waiters when
37 // its destructor is called. 38 // its destructor is called.
38 int LockEndpoint(const IPEndPoint& endpoint, Waiter* waiter); 39 int LockEndpoint(const IPEndPoint& endpoint, Waiter* waiter);
39 40
40 // Records the IPEndPoint associated with a particular socket. This is 41 // Records the IPEndPoint associated with a particular socket. This is
41 // necessary because TCPClientSocket refuses to return the PeerAddress after 42 // necessary because TCPClientSocket refuses to return the PeerAddress after
42 // the connection is disconnected. The association will be forgotten when 43 // the connection is disconnected. The association will be forgotten when
43 // UnlockSocket() or UnlockEndpoint() is called. The |socket| pointer must not 44 // UnlockSocket() or UnlockEndpoint() is called. The |socket| pointer must not
44 // be deleted between the call to RememberSocket() and the call to 45 // be deleted between the call to RememberSocket() and the call to
45 // UnlockSocket(). 46 // UnlockSocket().
46 void RememberSocket(StreamSocket* socket, const IPEndPoint& endpoint); 47 void RememberSocket(StreamSocket* socket, const IPEndPoint& endpoint);
47 48
48 // Releases the lock on the endpoint that was associated with |socket| by 49 // Removes the socket association that was recorded by RememberSocket(), then
49 // RememberSocket(). If appropriate, triggers the next socket connection. 50 // asynchronously releases the lock on the endpoint after a delay. If
50 // Should be called exactly once for each |socket| that was passed to 51 // appropriate, calls |waiter->GetEndpointLock()| when the lock is
51 // RememberSocket(). Does nothing if UnlockEndpoint() has been called since 52 // released. Should be called exactly once for each |socket| that was passed
53 // to RememberSocket(). Does nothing if UnlockEndpoint() has been called since
52 // the call to RememberSocket(). 54 // the call to RememberSocket().
53 void UnlockSocket(StreamSocket* socket); 55 void UnlockSocket(StreamSocket* socket);
54 56
55 // Releases the lock on |endpoint|. Does nothing if |endpoint| is not locked. 57 // Asynchronously releases the lock on |endpoint| after a delay. Does nothing
56 // Removes any socket association that was recorded with RememberSocket(). If 58 // if |endpoint| is not locked. Removes any socket association that was
57 // appropriate, calls |waiter->GotEndpointLock()|. 59 // recorded with RememberSocket(). If appropriate, calls
60 // |waiter->GotEndpointLock()| when the lock is released.
58 void UnlockEndpoint(const IPEndPoint& endpoint); 61 void UnlockEndpoint(const IPEndPoint& endpoint);
59 62
60 // Checks that |lock_info_map_| and |socket_lock_info_map_| are empty. For 63 // Checks that |lock_info_map_| and |socket_lock_info_map_| are empty. For
61 // tests. 64 // tests.
62 bool IsEmpty() const; 65 bool IsEmpty() const;
63 66
67 // Changes the value of the unlock delay. Returns the previous value of the
68 // delay.
69 base::TimeDelta SetUnlockDelayForTesting(base::TimeDelta new_delay);
70
64 private: 71 private:
65 struct LockInfo { 72 struct LockInfo {
66 typedef base::LinkedList<Waiter> WaiterQueue; 73 typedef base::LinkedList<Waiter> WaiterQueue;
67 74
68 LockInfo(); 75 LockInfo();
69 ~LockInfo(); 76 ~LockInfo();
70 77
71 // This object must be copyable to be placed in the map, but it cannot be 78 // This object must be copyable to be placed in the map, but it cannot be
72 // copied after |queue| has been assigned to. 79 // copied after |queue| has been assigned to.
73 LockInfo(const LockInfo& rhs); 80 LockInfo(const LockInfo& rhs);
(...skipping 16 matching lines...) Expand all
90 97
91 // SocketLockInfoMap requires std::map iterator semantics for LockInfoMap 98 // SocketLockInfoMap requires std::map iterator semantics for LockInfoMap
92 // (ie. that the iterator will remain valid as long as the entry is not 99 // (ie. that the iterator will remain valid as long as the entry is not
93 // deleted). 100 // deleted).
94 typedef std::map<IPEndPoint, LockInfo> LockInfoMap; 101 typedef std::map<IPEndPoint, LockInfo> LockInfoMap;
95 typedef std::map<StreamSocket*, LockInfoMap::iterator> SocketLockInfoMap; 102 typedef std::map<StreamSocket*, LockInfoMap::iterator> SocketLockInfoMap;
96 103
97 WebSocketEndpointLockManager(); 104 WebSocketEndpointLockManager();
98 ~WebSocketEndpointLockManager(); 105 ~WebSocketEndpointLockManager();
99 106
100 void UnlockEndpointByIterator(LockInfoMap::iterator lock_info_it); 107 void UnlockEndpointAfterDelay(const IPEndPoint& endpoint);
108 void DelayedUnlockEndpoint(const IPEndPoint& endpoint);
101 void EraseSocket(LockInfoMap::iterator lock_info_it); 109 void EraseSocket(LockInfoMap::iterator lock_info_it);
102 110
103 // If an entry is present in the map for a particular endpoint, then that 111 // If an entry is present in the map for a particular endpoint, then that
104 // endpoint is locked. If LockInfo.queue is non-empty, then one or more 112 // endpoint is locked. If LockInfo.queue is non-empty, then one or more
105 // Waiters are waiting for the lock. 113 // Waiters are waiting for the lock.
106 LockInfoMap lock_info_map_; 114 LockInfoMap lock_info_map_;
107 115
108 // Store sockets remembered by RememberSocket() and not yet unlocked by 116 // Store sockets remembered by RememberSocket() and not yet unlocked by
109 // UnlockSocket() or UnlockEndpoint(). Every entry in this map always 117 // UnlockSocket() or UnlockEndpoint(). Every entry in this map always
110 // references a live entry in lock_info_map_, and the LockInfo::socket member 118 // references a live entry in lock_info_map_, and the LockInfo::socket member
111 // is non-NULL if and only if there is an entry in this map for the socket. 119 // is non-NULL if and only if there is an entry in this map for the socket.
112 SocketLockInfoMap socket_lock_info_map_; 120 SocketLockInfoMap socket_lock_info_map_;
113 121
122 // Time to wait between a call to Unlock* and actually unlocking the socket.
123 base::TimeDelta unlock_delay_;
124
125 // Number of sockets currently pending unlock.
126 size_t pending_unlock_count_;
127
128 // The messsage loop holding the unlock delay callback may outlive this
129 // object.
130 base::WeakPtrFactory<WebSocketEndpointLockManager> weak_factory_;
131
114 friend struct DefaultSingletonTraits<WebSocketEndpointLockManager>; 132 friend struct DefaultSingletonTraits<WebSocketEndpointLockManager>;
115 133
116 DISALLOW_COPY_AND_ASSIGN(WebSocketEndpointLockManager); 134 DISALLOW_COPY_AND_ASSIGN(WebSocketEndpointLockManager);
117 }; 135 };
118 136
119 } // namespace net 137 } // namespace net
120 138
121 #endif // NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_ 139 #endif // NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698