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

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: Fixed incomplete refactor. 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 15 matching lines...) Expand all
162 164
163 int MockHostResolverBase::ResolveProc(size_t id, 165 int MockHostResolverBase::ResolveProc(size_t id,
164 const RequestInfo& info, 166 const RequestInfo& info,
165 AddressList* addresses) { 167 AddressList* addresses) {
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);
174 if (rv != OK)
175 return rv;
cbentzel 2012/01/20 11:04:53 Not sure you want to early exit here - doesn't mat
szym 2012/01/20 16:31:46 Right, I didn't catch that Set(..., TTL = 0) is no
176
172 if (cache_.get()) { 177 if (cache_.get()) {
173 HostCache::Key key(info.hostname(), 178 HostCache::Key key(info.hostname(),
174 info.address_family(), 179 info.address_family(),
175 info.host_resolver_flags()); 180 info.host_resolver_flags());
176 cache_->Set(key, rv, addr, base::TimeTicks::Now()); 181 cache_->Set(key, rv, addr,
182 base::TimeTicks::Now(),
183 kCacheEntryTTL);
177 } 184 }
178 if (rv == OK) 185 *addresses = CreateAddressListUsingPort(addr, info.port());
179 *addresses = CreateAddressListUsingPort(addr, info.port());
180 return rv; 186 return rv;
181 } 187 }
182 188
183 void MockHostResolverBase::ResolveNow(size_t id) { 189 void MockHostResolverBase::ResolveNow(size_t id) {
184 RequestMap::iterator it = requests_.find(id); 190 RequestMap::iterator it = requests_.find(id);
185 if (it == requests_.end()) 191 if (it == requests_.end())
186 return; // was canceled 192 return; // was canceled
187 193
188 scoped_ptr<Request> req(it->second); 194 scoped_ptr<Request> req(it->second);
189 requests_.erase(it); 195 requests_.erase(it);
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 CHECK_EQ(old_proc, current_proc_); 397 CHECK_EQ(old_proc, current_proc_);
392 } 398 }
393 399
394 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { 400 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) {
395 current_proc_ = proc; 401 current_proc_ = proc;
396 previous_proc_ = HostResolverProc::SetDefault(current_proc_); 402 previous_proc_ = HostResolverProc::SetDefault(current_proc_);
397 current_proc_->SetLastProc(previous_proc_); 403 current_proc_->SetLastProc(previous_proc_);
398 } 404 }
399 405
400 } // namespace net 406 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698