Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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_URL_REQUEST_URL_REQUEST_BACKOFF_MANAGER_H_ | |
| 6 #define NET_URL_REQUEST_URL_REQUEST_BACKOFF_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/threading/non_thread_safe.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "net/base/net_export.h" | |
| 15 #include "net/base/network_change_notifier.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 class HttpResponseHeaders; | |
| 21 | |
| 22 // Class that manages information on Backoff headers. URL requests for HTTP | |
|
Deprecated (see juliatuttle)
2015/07/13 20:30:41
A link to the spec that you're implementing here (
xunjieli
2015/07/13 20:53:37
Done.
| |
| 23 // contents should register their URLs in this manager on each request. | |
| 24 // | |
| 25 // URLRequestBackoffManager maintains a map of URL IDs to | |
| 26 // URLRequestBackoffManager::Entry. It creates an entry when a request receives | |
| 27 // a Backoff header, and does garbage collection from time to time in order to | |
| 28 // clean out outdated entries. URL ID consists of lowercased scheme, host, port | |
| 29 // and path. A newer request with the same ID will override the old entry. | |
| 30 class NET_EXPORT URLRequestBackoffManager | |
| 31 : NON_EXPORTED_BASE(public base::NonThreadSafe), | |
| 32 public NetworkChangeNotifier::IPAddressObserver, | |
| 33 public NetworkChangeNotifier::ConnectionTypeObserver { | |
| 34 public: | |
| 35 // Minimum number of seconds that a Backoff header can specify. | |
| 36 static const uint16 kMinimumBackoffInSeconds; | |
| 37 // Maximum number of seconds that a Backoff header can specify. | |
| 38 static const uint16 kMaximumBackoffInSeconds; | |
| 39 // Number of throttled requests that will be made between garbage collection. | |
| 40 static const uint16 kNewEntriesBetweenCollecting; | |
| 41 | |
| 42 URLRequestBackoffManager(); | |
| 43 ~URLRequestBackoffManager() override; | |
| 44 | |
| 45 // Updates internal states with a response. | |
| 46 void UpdateWithResponse(const GURL& url, | |
| 47 HttpResponseHeaders* headers, | |
| 48 const base::Time& response_time); | |
| 49 | |
| 50 // Returns whether the request should be rejected because of a Backoff header. | |
| 51 bool ShouldRejectRequest(const GURL& url, const base::Time& request_time); | |
| 52 | |
| 53 // IPAddressObserver implementation. | |
| 54 void OnIPAddressChanged() override; | |
| 55 | |
| 56 // ConnectionTypeObserver implementation. | |
| 57 void OnConnectionTypeChanged( | |
| 58 NetworkChangeNotifier::ConnectionType type) override; | |
| 59 | |
| 60 // Used by tests. | |
| 61 int GetNumberOfEntriesForTests() const; | |
| 62 | |
| 63 private: | |
| 64 // An struct that holds relevant information obtained from a Backoff header. | |
| 65 struct Entry { | |
| 66 Entry(const base::Time& time1, const base::Time& time2) | |
| 67 : throttled_time(time1), release_time(time2), used(false) {} | |
| 68 ~Entry() {} | |
| 69 | |
| 70 // Returns whether this entry is outdated. | |
| 71 bool IsOutDated() { return base::Time::Now() >= release_time; } | |
| 72 | |
| 73 // Before this time, requests with the same URL ID should be throttled. | |
| 74 const base::Time throttled_time; | |
| 75 | |
| 76 // Only one request with the same URL ID should be allowed in | |
| 77 // [|throttled_time|, |release_time|). | |
| 78 // After this time, all requests with the same URL ID are allowed. | |
| 79 const base::Time release_time; | |
| 80 | |
| 81 // Indicates whether a request has been made in | |
| 82 // [|throttled_time|, |release_time|). | |
| 83 bool used; | |
| 84 }; | |
| 85 | |
| 86 // From each URL, generate an ID composed of the scheme, host, port and path | |
| 87 // that allows unique mapping an entry to it. | |
| 88 typedef std::map<std::string, Entry*> UrlEntryMap; | |
| 89 | |
| 90 // Method that ensures the map gets cleaned from time to time. The period at | |
| 91 // which garbage collecting happens is adjustable with the | |
| 92 // kNewEntriesBetweenCollecting constant. | |
| 93 void GarbageCollectEntriesIfNecessary(); | |
| 94 | |
| 95 // Return true if there is a well-formed Backoff header key-value pair, | |
| 96 // and write the Backoff header value in |result|. Return false if no header | |
| 97 // is found or the value is invalid (i.e. less than kMinimumBackoffInSeconds | |
| 98 // or greater than kMaximumBackoffInSeconds). | |
| 99 bool GetBackoffTime(HttpResponseHeaders* headers, | |
| 100 base::TimeDelta* result) const; | |
| 101 | |
| 102 // Method that transforms a URL into an ID that can be used in the map. | |
| 103 // Resulting IDs will be lowercase and consist of the scheme, host, port | |
| 104 // and path (without query string, fragment, etc.). | |
| 105 // If the URL is invalid, the invalid spec will be returned, without any | |
| 106 // transformation. | |
| 107 std::string GetIdFromUrl(const GURL& url) const; | |
| 108 | |
| 109 // When switching from online to offline or change IP addresses, | |
| 110 // clear all back-off history. This is a precaution in case the change in | |
| 111 // online state now allows communicating without errors with servers that | |
| 112 // were previously returning Backoff headers. | |
| 113 void OnNetworkChange(); | |
| 114 | |
| 115 UrlEntryMap url_entries_; | |
| 116 | |
| 117 // Keeps track of how many new entries are created since last garbage | |
| 118 // collection. | |
| 119 unsigned int new_entries_since_last_gc_; | |
| 120 | |
| 121 // Valid after construction. | |
| 122 GURL::Replacements url_id_replacements_; | |
| 123 | |
| 124 DISALLOW_COPY_AND_ASSIGN(URLRequestBackoffManager); | |
| 125 }; | |
| 126 | |
| 127 } // namespace net | |
| 128 | |
| 129 #endif // NET_URL_REQUEST_URL_REQUEST_BACKOFF_MANAGER_H_ | |
| OLD | NEW |