| OLD | NEW |
| (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 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 entry = new RequestThrottlerEntry(); |
| 26 |
| 27 return entry; |
| 28 } |
| 29 |
| 30 RequestThrottlerManager::RequestThrottlerManager() |
| 31 : requests_since_last_gc_(0) { |
| 32 } |
| 33 |
| 34 RequestThrottlerManager::~RequestThrottlerManager() { |
| 35 // Delete all entries. |
| 36 url_entries_.clear(); |
| 37 } |
| 38 |
| 39 std::string RequestThrottlerManager::GetIdFromUrl(const GURL& url) { |
| 40 std::string url_id; |
| 41 url_id += url.scheme(); |
| 42 url_id += "://"; |
| 43 url_id += url.host(); |
| 44 url_id += url.path(); |
| 45 |
| 46 return StringToLowerASCII(url_id); |
| 47 } |
| 48 |
| 49 void RequestThrottlerManager::GarbageCollectEntries() { |
| 50 lock_.AssertAcquired(); |
| 51 |
| 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 lock_.AssertAcquired(); |
| 70 |
| 71 requests_since_last_gc_++; |
| 72 if (requests_since_last_gc_ < kRequestsBetweenCollecting) |
| 73 return; |
| 74 |
| 75 requests_since_last_gc_ = 0; |
| 76 GarbageCollectEntries(); |
| 77 } |
| 78 |
| 79 void RequestThrottlerManager::OverrideEntry( |
| 80 const GURL& url, |
| 81 const scoped_refptr<RequestThrottlerEntry>& entry) { |
| 82 if (entry == NULL) |
| 83 return; |
| 84 |
| 85 // Normalize the url. |
| 86 std::string url_id = GetIdFromUrl(url); |
| 87 |
| 88 AutoLock auto_lock(lock_); |
| 89 |
| 90 // Periodically garbage collect old entries. |
| 91 GarbageCollectEntriesIfNecessary(); |
| 92 |
| 93 url_entries_[url_id] = entry; |
| 94 } |
| 95 |
| 96 void RequestThrottlerManager::EraseEntry(const GURL& url) { |
| 97 // Normalize the url. |
| 98 std::string url_id = GetIdFromUrl(url); |
| 99 |
| 100 AutoLock auto_lock(lock_); |
| 101 |
| 102 url_entries_.erase(url_id); |
| 103 } |
| OLD | NEW |