OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_HTTP_WINHTTP_REQUEST_THROTTLE_H_ | |
6 #define NET_HTTP_WINHTTP_REQUEST_THROTTLE_H_ | |
7 | |
8 #include <windows.h> | |
9 #include <winhttp.h> | |
10 | |
11 #include <map> | |
12 #include <string> | |
13 | |
14 #include "base/basictypes.h" | |
15 #include "base/linked_ptr.h" | |
16 #include "testing/gtest/include/gtest/gtest_prod.h" | |
17 | |
18 namespace net { | |
19 | |
20 // The WinHttpRequestThrottle class regulates the rate at which we call | |
21 // WinHttpSendRequest, ensuring that at any time there are at most 6 WinHTTP | |
22 // requests in progress for each server or proxy. | |
23 // | |
24 // The throttling is intended to cause WinHTTP to maintain at most 6 | |
25 // persistent HTTP connections with each server or proxy. This works well in | |
26 // most cases, except when making HTTPS requests via a proxy, in which case | |
27 // WinHTTP may open much more than 6 connections to the proxy in spite of our | |
28 // rate limiting. | |
29 // | |
30 // Because we identify a server by its hostname rather than its IP address, | |
31 // we also can't distinguish between two different hostnames that resolve to | |
32 // the same IP address. | |
33 // | |
34 // Although WinHTTP has the WINHTTP_OPTION_MAX_CONNS_PER_SERVER option to | |
35 // limit the number of connections allowed per server, we can't use it | |
36 // because it has two serious bugs: | |
37 // 1. It causes WinHTTP to not close idle persistent connections, leaving | |
38 // many connections in the CLOSE_WAIT state. This may cause system | |
39 // crashes (Blue Screen of Death) when VPN is used. | |
40 // 2. It causes WinHTTP to crash intermittently in | |
41 // HTTP_REQUEST_HANDLE_OBJECT::OpenProxyTunnel_Fsm() if a proxy is used. | |
42 // Therefore, we have to resort to throttling our WinHTTP requests to achieve | |
43 // the same effect. | |
44 // | |
45 // Note on thread safety: The WinHttpRequestThrottle class is only used by | |
46 // the IO thread, so it doesn't need to be protected with a lock. The | |
47 // drawback is that the time we mark a request done is only approximate. | |
48 // We do that in the HttpTransactionWinHttp destructor, rather than in the | |
49 // WinHTTP status callback upon receiving HANDLE_CLOSING. | |
50 class WinHttpRequestThrottle { | |
51 public: | |
52 WinHttpRequestThrottle() {} | |
53 | |
54 virtual ~WinHttpRequestThrottle(); | |
55 | |
56 // Intended to be a near drop-in replacement of WinHttpSendRequest. | |
57 BOOL SubmitRequest(const std::string& server, | |
58 HINTERNET request_handle, | |
59 DWORD total_size, | |
60 DWORD_PTR context); | |
61 | |
62 // Called when a request failed or completed successfully. | |
63 void NotifyRequestDone(const std::string& server); | |
64 | |
65 // Called from the HttpTransactionWinHttp destructor. | |
66 void RemoveRequest(const std::string& server, | |
67 HINTERNET request_handle); | |
68 | |
69 protected: | |
70 // Unit tests can stub out this method in a derived class. | |
71 virtual BOOL SendRequest(HINTERNET request_handle, | |
72 DWORD total_size, | |
73 DWORD_PTR context, | |
74 bool report_async_error); | |
75 | |
76 private: | |
77 FRIEND_TEST(WinHttpRequestThrottleTest, GarbageCollect); | |
78 | |
79 class RequestQueue; | |
80 | |
81 struct PerServerThrottle { | |
82 PerServerThrottle(); | |
83 ~PerServerThrottle(); | |
84 | |
85 int num_requests; // Number of requests in progress | |
86 linked_ptr<RequestQueue> request_queue; // Requests waiting to be sent | |
87 }; | |
88 | |
89 typedef std::map<std::string, PerServerThrottle> ThrottleMap; | |
90 | |
91 static const int kMaxConnectionsPerServer; | |
92 static const int kGarbageCollectionThreshold; | |
93 | |
94 void GarbageCollect(); | |
95 | |
96 ThrottleMap throttles_; | |
97 | |
98 DISALLOW_EVIL_CONSTRUCTORS(WinHttpRequestThrottle); | |
99 }; | |
100 | |
101 } // namespace net | |
102 | |
103 #endif // NET_HTTP_WINHTTP_REQUEST_THROTTLE_H_ | |
104 | |
OLD | NEW |