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

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

Issue 2487001: Request Throttler : Exponential back-off (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Corrected a flaky test due to having 2 implementation of request throttling Created 10 years, 4 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 | Annotate | Revision Log
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/logging.h"
8 #include "base/message_loop.h"
9 #include "base/rand_util.h"
10 #include "base/string_util.h"
11
12 const unsigned int RequestThrottlerManager::kMaximumNumberOfEntries = 1500;
13 const unsigned int RequestThrottlerManager::kRequestsBetweenCollecting = 200;
14
15 scoped_refptr<RequestThrottlerEntryInterface>
16 RequestThrottlerManager::RegisterRequestUrl(
17 const GURL &url) {
18 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO);
19
20 // Periodically garbage collect old entries.
21 requests_since_last_gc_++;
22 if (requests_since_last_gc_ >= kRequestsBetweenCollecting) {
23 GarbageCollectEntries();
24 requests_since_last_gc_ = 0;
25 }
26
27 // Normalize the url
28 std::string url_id = GetIdFromUrl(url);
29
30 // Finds the entry in the map or creates it.
31 scoped_refptr<RequestThrottlerEntry>& entry = url_entries_[url_id];
32 if (entry == NULL)
33 entry = new RequestThrottlerEntry();
34
35 return entry;
36 }
37
38 RequestThrottlerManager::RequestThrottlerManager()
39 : current_loop_(MessageLoop::current()),
40 requests_since_last_gc_(0) {
41 }
42
43 RequestThrottlerManager::~RequestThrottlerManager() {
44 // Deletes 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();
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 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO);
60 UrlEntryMap::iterator i = url_entries_.begin();
61
62 while (i != url_entries_.end()) {
63 if ((i->second)->IsEntryOutdated()) {
64 url_entries_.erase(i++);
65 } else {
66 ++i;
67 }
68 }
69
70 // In case something broke we want to make sure not to grow indefinitely.
71 while (url_entries_.size() > kMaximumNumberOfEntries) {
72 url_entries_.erase(url_entries_.begin());
73 }
74 }
75
76 void RequestThrottlerManager::NotifyRequestBodyWasMalformed(const GURL& url) {
77 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO);
78 // Normalize the url
79 std::string url_id = GetIdFromUrl(url);
80 UrlEntryMap::iterator i = url_entries_.find(url_id);
81
82 if (i != url_entries_.end()) {
83 i->second->ReceivedContentWasMalformed();
84 }
85 }
OLDNEW
« no previous file with comments | « net/request_throttler/request_throttler_manager.h ('k') | net/request_throttler/request_throttler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698