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

Side by Side Diff: net/url_request/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/url_request/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 RequestThrottlerManager* RequestThrottlerManager::GetInstance() {
13 return Singleton<RequestThrottlerManager>::get();
14 }
15
16 scoped_refptr<RequestThrottlerEntryInterface>
17 RequestThrottlerManager::RegisterRequestUrl(const GURL &url) {
18 // Normalize the url.
19 std::string url_id = GetIdFromUrl(url);
20
21 // Periodically garbage collect old entries.
22 GarbageCollectEntriesIfNecessary();
23
24 // Find the entry in the map or create it.
25 scoped_refptr<RequestThrottlerEntry>& entry = url_entries_[url_id];
26 if (entry == NULL)
27 entry = new RequestThrottlerEntry();
28
29 return entry;
30 }
31
32 RequestThrottlerManager::RequestThrottlerManager()
33 : requests_since_last_gc_(0) {
34 }
35
36 RequestThrottlerManager::~RequestThrottlerManager() {
37 // Delete all entries.
38 url_entries_.clear();
39 }
40
41 std::string RequestThrottlerManager::GetIdFromUrl(const GURL& url) {
willchan no longer on Chromium 2010/11/24 00:23:07 Why are you not using the GURL canonicalization ro
yzshen 2010/11/24 09:42:08 I have changed to use Replacements to convert. Tha
42 std::string url_id;
43 url_id += url.scheme();
44 url_id += "://";
45 url_id += url.host();
46 url_id += url.path();
47
48 return StringToLowerASCII(url_id);
49 }
50
51 void RequestThrottlerManager::GarbageCollectEntries() {
52 UrlEntryMap::iterator i = url_entries_.begin();
53
54 while (i != url_entries_.end()) {
55 if ((i->second)->IsEntryOutdated()) {
56 url_entries_.erase(i++);
57 } else {
58 ++i;
59 }
60 }
61
62 // In case something broke we want to make sure not to grow indefinitely.
63 while (url_entries_.size() > kMaximumNumberOfEntries) {
64 url_entries_.erase(url_entries_.begin());
65 }
66 }
67
68 void RequestThrottlerManager::GarbageCollectEntriesIfNecessary() {
69 requests_since_last_gc_++;
70 if (requests_since_last_gc_ < kRequestsBetweenCollecting)
71 return;
72
73 requests_since_last_gc_ = 0;
74 GarbageCollectEntries();
75 }
76
77 void RequestThrottlerManager::OverrideEntryForTests(
78 const GURL& url,
79 RequestThrottlerEntry* entry) {
80 if (entry == NULL)
81 return;
82
83 // Normalize the url.
84 std::string url_id = GetIdFromUrl(url);
85
86 // Periodically garbage collect old entries.
87 GarbageCollectEntriesIfNecessary();
88
89 url_entries_[url_id] = entry;
90 }
91
92 void RequestThrottlerManager::EraseEntryForTests(const GURL& url) {
93 // Normalize the url.
94 std::string url_id = GetIdFromUrl(url);
95 url_entries_.erase(url_id);
96 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698