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

Unified 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, 5 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 side-by-side diff with in-line comments
Download patch
Index: net/request_throttler/request_throttler_manager.cc
===================================================================
--- net/request_throttler/request_throttler_manager.cc (revision 0)
+++ net/request_throttler/request_throttler_manager.cc (revision 0)
@@ -0,0 +1,85 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/request_throttler/request_throttler_manager.h"
+
+#include "base/logging.h"
+#include "base/message_loop.h"
+#include "base/rand_util.h"
+#include "base/string_util.h"
+
+const unsigned int RequestThrottlerManager::kMaximumNumberOfEntries = 1500;
+const unsigned int RequestThrottlerManager::kRequestsBetweenCollecting = 200;
+
+scoped_refptr<RequestThrottlerEntryInterface>
+ RequestThrottlerManager::RegisterRequestUrl(
+ const GURL &url) {
+ DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO);
+
+ // Periodically garbage collect old entries.
+ requests_since_last_gc_++;
+ if (requests_since_last_gc_ >= kRequestsBetweenCollecting) {
+ GarbageCollectEntries();
+ requests_since_last_gc_ = 0;
+ }
+
+ // Normalize the url
+ std::string url_id = GetIdFromUrl(url);
+
+ // Finds the entry in the map or creates it.
+ scoped_refptr<RequestThrottlerEntry>& entry = url_entries_[url_id];
+ if (entry == NULL)
+ entry = new RequestThrottlerEntry();
+
+ return entry;
+}
+
+RequestThrottlerManager::RequestThrottlerManager()
+ : current_loop_(MessageLoop::current()),
+ requests_since_last_gc_(0) {
+}
+
+RequestThrottlerManager::~RequestThrottlerManager() {
+ // Deletes all entries
+ url_entries_.clear();
+}
+
+std::string RequestThrottlerManager::GetIdFromUrl(const GURL& url) {
+ std::string url_id;
+ url_id += url.scheme();
+ url_id += "://";
+ url_id += url.host();
+ url_id += url.path();
+
+ return StringToLowerASCII(url_id);
+}
+
+void RequestThrottlerManager::GarbageCollectEntries() {
+ DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO);
+ UrlEntryMap::iterator i = url_entries_.begin();
+
+ while (i != url_entries_.end()) {
+ if ((i->second)->IsEntryOutdated()) {
+ url_entries_.erase(i++);
+ } else {
+ ++i;
+ }
+ }
+
+ // In case something broke we want to make sure not to grow indefinitely.
+ while (url_entries_.size() > kMaximumNumberOfEntries) {
+ url_entries_.erase(url_entries_.begin());
+ }
+}
+
+void RequestThrottlerManager::NotifyRequestBodyWasMalformed(const GURL& url) {
+ DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO);
+ // Normalize the url
+ std::string url_id = GetIdFromUrl(url);
+ UrlEntryMap::iterator i = url_entries_.find(url_id);
+
+ if (i != url_entries_.end()) {
+ i->second->ReceivedContentWasMalformed();
+ }
+}
« 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