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

Side by Side Diff: net/base/mock_host_resolver.cc

Issue 9197009: Adds custom ttl argument to HostCache::Set. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 8 years, 11 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/mock_host_resolver.h" 5 #include "net/base/mock_host_resolver.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/string_split.h" 12 #include "base/string_split.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
15 #include "net/base/host_cache.h" 15 #include "net/base/host_cache.h"
16 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
17 #include "net/base/net_util.h" 17 #include "net/base/net_util.h"
18 #include "net/base/sys_addrinfo.h" 18 #include "net/base/sys_addrinfo.h"
19 #include "net/base/test_completion_callback.h" 19 #include "net/base/test_completion_callback.h"
20 20
21 namespace net { 21 namespace net {
22 22
23 namespace { 23 namespace {
24 24
25 // Cache size for the MockCachingHostResolver.
26 const unsigned kMaxCacheEntries = 100;
27 // TTL for the successful resolutions. Failures are not cached.
28 const base::TimeDelta kCacheEntryTTL = base::TimeDelta::FromMinutes(1);
29
25 char* do_strdup(const char* src) { 30 char* do_strdup(const char* src) {
26 #if defined(OS_WIN) 31 #if defined(OS_WIN)
27 return _strdup(src); 32 return _strdup(src);
28 #else 33 #else
29 return strdup(src); 34 return strdup(src);
30 #endif 35 #endif
31 } 36 }
32 37
33 } // namespace 38 } // namespace
34 39
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 return cache_.get(); 128 return cache_.get();
124 } 129 }
125 130
126 // start id from 1 to distinguish from NULL RequestHandle 131 // start id from 1 to distinguish from NULL RequestHandle
127 MockHostResolverBase::MockHostResolverBase(bool use_caching) 132 MockHostResolverBase::MockHostResolverBase(bool use_caching)
128 : synchronous_mode_(false), next_request_id_(1) { 133 : synchronous_mode_(false), next_request_id_(1) {
129 rules_ = CreateCatchAllHostResolverProc(); 134 rules_ = CreateCatchAllHostResolverProc();
130 proc_ = rules_; 135 proc_ = rules_;
131 136
132 if (use_caching) { 137 if (use_caching) {
133 cache_.reset(new HostCache( 138 cache_.reset(new HostCache(kMaxCacheEntries));
134 100, // max entries.
135 base::TimeDelta::FromMinutes(1),
136 base::TimeDelta::FromSeconds(0)));
137 } 139 }
138 } 140 }
139 141
140 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, 142 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info,
141 AddressList* addresses) { 143 AddressList* addresses) {
142 IPAddressNumber ip; 144 IPAddressNumber ip;
143 if (ParseIPLiteralToNumber(info.hostname(), &ip)) { 145 if (ParseIPLiteralToNumber(info.hostname(), &ip)) {
144 *addresses = AddressList::CreateFromIPAddressWithCname( 146 *addresses = AddressList::CreateFromIPAddressWithCname(
145 ip, info.port(), info.host_resolver_flags() & HOST_RESOLVER_CANONNAME); 147 ip, info.port(), info.host_resolver_flags() & HOST_RESOLVER_CANONNAME);
146 return OK; 148 return OK;
(...skipping 19 matching lines...) Expand all
166 AddressList addr; 168 AddressList addr;
167 int rv = proc_->Resolve(info.hostname(), 169 int rv = proc_->Resolve(info.hostname(),
168 info.address_family(), 170 info.address_family(),
169 info.host_resolver_flags(), 171 info.host_resolver_flags(),
170 &addr, 172 &addr,
171 NULL); 173 NULL);
172 if (cache_.get()) { 174 if (cache_.get()) {
173 HostCache::Key key(info.hostname(), 175 HostCache::Key key(info.hostname(),
174 info.address_family(), 176 info.address_family(),
175 info.host_resolver_flags()); 177 info.host_resolver_flags());
176 cache_->Set(key, rv, addr, base::TimeTicks::Now()); 178 // Storing a failure with TTL 0 so that it overwrites previous value.
179 cache_->Set(key, rv, addr,
180 base::TimeTicks::Now(),
181 (rv == OK) ? kCacheEntryTTL : base::TimeDelta());
177 } 182 }
178 if (rv == OK) 183 if (rv == OK)
179 *addresses = CreateAddressListUsingPort(addr, info.port()); 184 *addresses = CreateAddressListUsingPort(addr, info.port());
180 return rv; 185 return rv;
181 } 186 }
182 187
183 void MockHostResolverBase::ResolveNow(size_t id) { 188 void MockHostResolverBase::ResolveNow(size_t id) {
184 RequestMap::iterator it = requests_.find(id); 189 RequestMap::iterator it = requests_.find(id);
185 if (it == requests_.end()) 190 if (it == requests_.end())
186 return; // was canceled 191 return; // was canceled
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 CHECK_EQ(old_proc, current_proc_); 396 CHECK_EQ(old_proc, current_proc_);
392 } 397 }
393 398
394 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { 399 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) {
395 current_proc_ = proc; 400 current_proc_ = proc;
396 previous_proc_ = HostResolverProc::SetDefault(current_proc_); 401 previous_proc_ = HostResolverProc::SetDefault(current_proc_);
397 current_proc_->SetLastProc(previous_proc_); 402 current_proc_->SetLastProc(previous_proc_);
398 } 403 }
399 404
400 } // namespace net 405 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698