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

Side by Side Diff: net/request_throttler/request_throttler_manager.cc

Issue 4194001: Implement exponential back-off mechanism and enforce it at the URLRequestHttpJob level. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 1 month 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 #include "net/request_throttler/request_throttler_manager.h"
6
7 #include "base/string_util.h"
8
9 const unsigned int RequestThrottlerManager::kMaximumNumberOfEntries = 1500;
10 const unsigned int RequestThrottlerManager::kRequestsBetweenCollecting = 200;
11
12 scoped_refptr<RequestThrottlerEntryInterface>
13 RequestThrottlerManager::RegisterRequestUrl(const GURL &url) {
14 // Normalize the url.
15 std::string url_id = GetIdFromUrl(url);
16
17 AutoLock auto_lock(lock_);
18
19 // Periodically garbage collect old entries.
20 GarbageCollectEntriesIfNecessary();
21
22 // Find the entry in the map or create it.
23 scoped_refptr<RequestThrottlerEntry>& entry = url_entries_[url_id];
24 if (entry == NULL) {
25 if (!no_throttling_by_default_) {
26 entry = new RequestThrottlerEntry();
27 } else {
28 // In order to disable back-off, create a very permissive instance.
29 // This is used by unit tests since we would like to avoid indeterministic
30 // behavior in unit tests.
31 entry = new RequestThrottlerEntry(1, 10000, 0, 0, 1.0, 0.0, 0, 120000);
32 }
33 }
34
35 return entry;
36 }
37
38 RequestThrottlerManager::RequestThrottlerManager()
39 : requests_since_last_gc_(0),
40 no_throttling_by_default_(false) {
41 }
42
43 RequestThrottlerManager::~RequestThrottlerManager() {
44 // Delete all entries.
45 url_entries_.clear();
46 }
47
48 std::string RequestThrottlerManager::GetIdFromUrl(const GURL& url) {
49 std::string url_id;
50 url_id += url.scheme();
eroman 2010/11/11 17:54:21 Is this going to be reached for data: URLs? We wou
yzshen 2010/11/11 22:17:05 At URLRequest(Job) level, only URLRequestHttpJob e
51 url_id += "://";
52 url_id += url.host();
53 url_id += url.path();
54
55 return StringToLowerASCII(url_id);
56 }
57
58 void RequestThrottlerManager::GarbageCollectEntries() {
59 lock_.AssertAcquired();
60
61 UrlEntryMap::iterator i = url_entries_.begin();
62
63 while (i != url_entries_.end()) {
64 if ((i->second)->IsEntryOutdated()) {
65 url_entries_.erase(i++);
66 } else {
67 ++i;
68 }
69 }
70
71 // In case something broke we want to make sure not to grow indefinitely.
72 while (url_entries_.size() > kMaximumNumberOfEntries) {
73 url_entries_.erase(url_entries_.begin());
74 }
75 }
76
77 void RequestThrottlerManager::GarbageCollectEntriesIfNecessary() {
78 lock_.AssertAcquired();
79
80 requests_since_last_gc_++;
81 if (requests_since_last_gc_ < kRequestsBetweenCollecting)
82 return;
83
84 requests_since_last_gc_ = 0;
85 GarbageCollectEntries();
86 }
87
88 void RequestThrottlerManager::NotifyRequestBodyWasMalformed(const GURL& url) {
89 // Normalize the url.
90 std::string url_id = GetIdFromUrl(url);
91
92 AutoLock auto_lock(lock_);
93
94 UrlEntryMap::iterator i = url_entries_.find(url_id);
95 if (i != url_entries_.end()) {
96 i->second->ReceivedContentWasMalformed();
97 }
98 }
99
100 int64 RequestThrottlerManager::GetBackoffDelayInMilliseconds(const GURL& url) {
101 std::string url_id = GetIdFromUrl(url);
102 base::TimeTicks release_time;
103 base::TimeTicks now = base::TimeTicks::Now();
104
105 {
106 AutoLock auto_lock(lock_);
107
108 UrlEntryMap::iterator i = url_entries_.find(url_id);
109 release_time = i != url_entries_.end() ? i->second->release_time() : now;
110 }
111
112 base::TimeDelta delay = std::max(release_time - now, base::TimeDelta());
113 return delay.InMillisecondsRoundedUp();
114 }
115
116 void RequestThrottlerManager::OverrideEntry(
117 const GURL& url,
118 const scoped_refptr<RequestThrottlerEntry>& entry) {
119 if (entry == NULL)
120 return;
121
122 // Normalize the url.
123 std::string url_id = GetIdFromUrl(url);
124
125 AutoLock auto_lock(lock_);
126
127 // Periodically garbage collect old entries.
128 GarbageCollectEntriesIfNecessary();
129
130 url_entries_[url_id] = entry;
131 }
132
133 void RequestThrottlerManager::ClearAllEntries() {
134 AutoLock auto_lock(lock_);
135 url_entries_.clear();
136 }
137
138 void RequestThrottlerManager::DisableThrottlingByDefault() {
139 AutoLock auto_lock(lock_);
140 no_throttling_by_default_ = true;
141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698