OLD | NEW |
---|---|
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/host_resolver_impl.h" | 5 #include "net/base/host_resolver_impl.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <Winsock2.h> | 8 #include <Winsock2.h> |
9 #elif defined(OS_POSIX) | 9 #elif defined(OS_POSIX) |
10 #include <netdb.h> | 10 #include <netdb.h> |
11 #endif | 11 #endif |
12 | 12 |
13 #include <algorithm> | |
13 #include <cmath> | 14 #include <cmath> |
14 #include <deque> | |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
18 #include "base/bind.h" | 18 #include "base/bind.h" |
19 #include "base/bind_helpers.h" | 19 #include "base/bind_helpers.h" |
20 #include "base/callback.h" | |
20 #include "base/compiler_specific.h" | 21 #include "base/compiler_specific.h" |
21 #include "base/debug/debugger.h" | 22 #include "base/debug/debugger.h" |
22 #include "base/debug/stack_trace.h" | 23 #include "base/debug/stack_trace.h" |
24 #include "base/memory/weak_ptr.h" | |
mmenke
2011/12/21 16:22:58
Since SupportsWeakPtr is defined in this file, thi
szym
2011/12/28 01:24:10
Done.
| |
23 #include "base/message_loop_proxy.h" | 25 #include "base/message_loop_proxy.h" |
24 #include "base/metrics/field_trial.h" | 26 #include "base/metrics/field_trial.h" |
25 #include "base/metrics/histogram.h" | 27 #include "base/metrics/histogram.h" |
26 #include "base/stl_util.h" | 28 #include "base/stl_util.h" |
27 #include "base/string_util.h" | 29 #include "base/string_util.h" |
28 #include "base/threading/worker_pool.h" | 30 #include "base/threading/worker_pool.h" |
29 #include "base/time.h" | 31 #include "base/time.h" |
30 #include "base/utf_string_conversions.h" | 32 #include "base/utf_string_conversions.h" |
31 #include "base/values.h" | 33 #include "base/values.h" |
32 #include "net/base/address_list.h" | 34 #include "net/base/address_list.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 NetLog* net_log) { | 136 NetLog* net_log) { |
135 // Maximum of 8 concurrent resolver threads. | 137 // Maximum of 8 concurrent resolver threads. |
136 // Some routers (or resolvers) appear to start to provide host-not-found if | 138 // Some routers (or resolvers) appear to start to provide host-not-found if |
137 // too many simultaneous resolutions are pending. This number needs to be | 139 // too many simultaneous resolutions are pending. This number needs to be |
138 // further optimized, but 8 is what FF currently does. | 140 // further optimized, but 8 is what FF currently does. |
139 static const size_t kDefaultMaxJobs = 8u; | 141 static const size_t kDefaultMaxJobs = 8u; |
140 | 142 |
141 if (max_concurrent_resolves == HostResolver::kDefaultParallelism) | 143 if (max_concurrent_resolves == HostResolver::kDefaultParallelism) |
142 max_concurrent_resolves = kDefaultMaxJobs; | 144 max_concurrent_resolves = kDefaultMaxJobs; |
143 | 145 |
144 HostResolverImpl* resolver = | 146 // TODO(szym): Add experiments with reserved slots for higher priority |
145 new HostResolverImpl(NULL, HostCache::CreateDefaultCache(), | 147 // requests. |
146 max_concurrent_resolves, max_retry_attempts, net_log); | 148 |
149 HostResolverImpl* resolver = new HostResolverImpl( | |
150 HostCache::CreateDefaultCache(), | |
151 PriorityDispatch::Limits::MakeAny(max_concurrent_resolves), | |
152 HostResolverImpl::ProcJobParams(NULL, max_retry_attempts), | |
153 net_log); | |
147 | 154 |
148 return resolver; | 155 return resolver; |
149 } | 156 } |
150 | 157 |
151 static int ResolveAddrInfo(HostResolverProc* resolver_proc, | 158 static int ResolveAddrInfo(HostResolverProc* resolver_proc, |
152 const std::string& host, | 159 const std::string& host, |
153 AddressFamily address_family, | 160 AddressFamily address_family, |
154 HostResolverFlags host_resolver_flags, | 161 HostResolverFlags host_resolver_flags, |
155 AddressList* out, | 162 AddressList* out, |
156 int* os_error) { | 163 int* os_error) { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 return dict; | 260 return dict; |
254 } | 261 } |
255 | 262 |
256 private: | 263 private: |
257 const std::string host_; | 264 const std::string host_; |
258 const NetLog::Source source_; | 265 const NetLog::Source source_; |
259 }; | 266 }; |
260 | 267 |
261 //----------------------------------------------------------------------------- | 268 //----------------------------------------------------------------------------- |
262 | 269 |
263 class HostResolverImpl::Request { | 270 class HostResolverImpl::Request { |
mmenke
2011/12/22 16:18:49
Think we should add a comment here that these are
szym
2011/12/28 01:24:10
Done.
| |
264 public: | 271 public: |
265 Request(const BoundNetLog& source_net_log, | 272 Request(const BoundNetLog& source_net_log, |
266 const BoundNetLog& request_net_log, | 273 const BoundNetLog& request_net_log, |
267 const RequestInfo& info, | 274 const RequestInfo& info, |
268 const CompletionCallback& callback, | 275 const CompletionCallback& callback, |
269 AddressList* addresses) | 276 AddressList* addresses) |
270 : source_net_log_(source_net_log), | 277 : source_net_log_(source_net_log), |
271 request_net_log_(request_net_log), | 278 request_net_log_(request_net_log), |
272 info_(info), | 279 info_(info), |
273 job_(NULL), | 280 job_(NULL), |
274 callback_(callback), | 281 callback_(callback), |
275 addresses_(addresses) { | 282 addresses_(addresses) { |
276 } | 283 } |
277 | 284 |
278 // Mark the request as cancelled. | 285 // Mark the request as canceled (so that we don't have to remove it from |
279 void MarkAsCancelled() { | 286 // a RequestsList). |
287 void MarkAsCanceled() { | |
280 job_ = NULL; | 288 job_ = NULL; |
281 addresses_ = NULL; | 289 addresses_ = NULL; |
282 callback_.Reset(); | 290 callback_.Reset(); |
283 } | 291 } |
284 | 292 |
285 bool was_cancelled() const { | 293 bool was_canceled() const { |
286 return callback_.is_null(); | 294 return callback_.is_null(); |
287 } | 295 } |
288 | 296 |
289 void set_job(Job* job) { | 297 void set_job(Job* job) { |
290 DCHECK(job != NULL); | 298 DCHECK(job); |
291 // Identify which job the request is waiting on. | 299 // Identify which job the request is waiting on. |
292 job_ = job; | 300 job_ = job; |
293 } | 301 } |
294 | 302 |
303 // Prepare final AddressList and call completion callback. | |
295 void OnComplete(int error, const AddressList& addrlist) { | 304 void OnComplete(int error, const AddressList& addrlist) { |
296 if (error == OK) | 305 if (error == OK) |
297 *addresses_ = CreateAddressListUsingPort(addrlist, port()); | 306 *addresses_ = CreateAddressListUsingPort(addrlist, port()); |
298 CompletionCallback callback = callback_; | 307 CompletionCallback callback = callback_; |
299 MarkAsCancelled(); | 308 MarkAsCanceled(); |
300 callback.Run(error); | 309 callback.Run(error); |
301 } | 310 } |
302 | 311 |
303 int port() const { | 312 int port() const { |
304 return info_.port(); | 313 return info_.port(); |
305 } | 314 } |
306 | 315 |
307 Job* job() const { | 316 Job* job() const { |
308 return job_; | 317 return job_; |
309 } | 318 } |
310 | 319 |
320 // NetLog for the source, passed in HostResolver::Resolve. | |
311 const BoundNetLog& source_net_log() { | 321 const BoundNetLog& source_net_log() { |
312 return source_net_log_; | 322 return source_net_log_; |
313 } | 323 } |
314 | 324 |
325 // NetLog for this request. | |
315 const BoundNetLog& request_net_log() { | 326 const BoundNetLog& request_net_log() { |
316 return request_net_log_; | 327 return request_net_log_; |
317 } | 328 } |
318 | 329 |
319 const RequestInfo& info() const { | 330 const RequestInfo& info() const { |
320 return info_; | 331 return info_; |
321 } | 332 } |
322 | 333 |
323 private: | 334 private: |
324 BoundNetLog source_net_log_; | 335 BoundNetLog source_net_log_; |
325 BoundNetLog request_net_log_; | 336 BoundNetLog request_net_log_; |
326 | 337 |
327 // The request info that started the request. | 338 // The request info that started the request. |
328 RequestInfo info_; | 339 RequestInfo info_; |
329 | 340 |
330 // The resolve job (running in worker pool) that this request is dependent on. | 341 // The resolve job that this request is dependent on. |
331 Job* job_; | 342 Job* job_; |
332 | 343 |
333 // The user's callback to invoke when the request completes. | 344 // The user's callback to invoke when the request completes. |
334 CompletionCallback callback_; | 345 CompletionCallback callback_; |
335 | 346 |
336 // The address list to save result into. | 347 // The address list to save result into. |
337 AddressList* addresses_; | 348 AddressList* addresses_; |
338 | 349 |
339 DISALLOW_COPY_AND_ASSIGN(Request); | 350 DISALLOW_COPY_AND_ASSIGN(Request); |
340 }; | 351 }; |
341 | 352 |
342 //------------------------------------------------------------------------------ | 353 //------------------------------------------------------------------------------ |
343 | 354 |
344 // Provide a common macro to simplify code and readability. We must use a | 355 // Provide a common macro to simplify code and readability. We must use a |
345 // macros as the underlying HISTOGRAM macro creates static varibles. | 356 // macros as the underlying HISTOGRAM macro creates static varibles. |
346 #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \ | 357 #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \ |
347 base::TimeDelta::FromMicroseconds(1), base::TimeDelta::FromHours(1), 100) | 358 base::TimeDelta::FromMicroseconds(1), base::TimeDelta::FromHours(1), 100) |
348 | 359 |
349 // This class represents a request to the worker pool for a "getaddrinfo()" | 360 // Calls HostResolverProc on the WorkerPool. Performs retries if necessary. |
350 // call. | 361 // TODO(szym): Move to separate source file for testing and mocking. |
351 class HostResolverImpl::Job | 362 class HostResolverImpl::ProcJob |
352 : public base::RefCountedThreadSafe<HostResolverImpl::Job> { | 363 : public base::RefCountedThreadSafe<HostResolverImpl::ProcJob> { |
mmenke
2011/12/22 16:18:49
I think using the names "Job" and "ProcJob" are a
mmenke
2011/12/22 16:19:54
Alternatively, could add a prefix to Job.
szym
2011/12/28 01:24:10
ProcTask it is.
| |
353 public: | 364 public: |
354 Job(int id, | 365 typedef base::Callback<void(int, int, const AddressList&)> Callback; |
mmenke
2011/12/21 16:22:58
nit: Mind adding a linebreak here?
szym
2011/12/28 01:24:10
Done.
| |
355 HostResolverImpl* resolver, | 366 ProcJob(const Key& key, |
356 const Key& key, | 367 const ProcJobParams& params, |
357 const BoundNetLog& source_net_log, | 368 const Callback& callback, |
358 NetLog* net_log) | 369 const BoundNetLog& job_net_log) |
359 : id_(id), | 370 : key_(key), |
mmenke
2011/12/21 16:22:58
nit: Know this was like this before, but this sho
szym
2011/12/28 01:24:10
Done.
| |
360 key_(key), | 371 params_(params), |
361 resolver_(resolver), | 372 callback_(callback), |
362 origin_loop_(base::MessageLoopProxy::current()), | 373 origin_loop_(base::MessageLoopProxy::current()), |
363 resolver_proc_(resolver->effective_resolver_proc()), | |
364 unresponsive_delay_(resolver->unresponsive_delay()), | |
365 attempt_number_(0), | 374 attempt_number_(0), |
366 completed_attempt_number_(0), | 375 completed_attempt_number_(0), |
367 completed_attempt_error_(ERR_UNEXPECTED), | 376 completed_attempt_error_(ERR_UNEXPECTED), |
368 had_non_speculative_request_(false), | 377 had_non_speculative_request_(false), |
369 net_log_(BoundNetLog::Make(net_log, | 378 net_log_(BoundNetLog::Make(job_net_log.net_log(), |
370 NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB)) { | 379 NetLog::SOURCE_HOST_RESOLVER_IMPL_PROC_JOB)) { |
371 DCHECK(resolver); | 380 if (!params_.resolver_proc_) |
372 net_log_.BeginEvent( | 381 params_.resolver_proc_ = HostResolverProc::GetDefault(); |
373 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, | |
374 make_scoped_refptr( | |
375 new JobCreationParameters(key.hostname, source_net_log.source()))); | |
376 } | |
377 | 382 |
378 // Attaches a request to this job. The job takes ownership of |req| and will | 383 job_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_JOB, |
379 // take care to delete it. | 384 new NetLogSourceParameter("source_dependency", |
380 void AddRequest(Request* req) { | 385 net_log_.source())); |
381 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
382 req->request_net_log().BeginEvent( | |
383 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, | |
384 make_scoped_refptr(new NetLogSourceParameter( | |
385 "source_dependency", net_log_.source()))); | |
386 | 386 |
387 req->set_job(this); | 387 net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_JOB, |
388 requests_.push_back(req); | 388 new JobCreationParameters(key_.hostname, |
389 | 389 job_net_log.source())); |
390 if (!req->info().is_speculative()) | |
391 had_non_speculative_request_ = true; | |
392 } | 390 } |
393 | 391 |
394 void Start() { | 392 void Start() { |
395 DCHECK(origin_loop_->BelongsToCurrentThread()); | 393 DCHECK(origin_loop_->BelongsToCurrentThread()); |
396 StartLookupAttempt(); | 394 StartLookupAttempt(); |
397 } | 395 } |
398 | 396 |
397 // Cancels this ProcJob. It will be orphaned. Any outstanding resolve | |
398 // attempts running on worker threads will continue running. Only once all the | |
399 // attempts complete will the final reference to this ProcJob be released. | |
400 void Cancel() { | |
401 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
402 | |
403 if (was_canceled()) | |
404 return; | |
405 | |
406 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); | |
407 | |
408 callback_.Reset(); | |
409 | |
410 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_JOB, NULL); | |
411 } | |
412 | |
413 void set_had_non_speculative_request() { | |
414 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
415 had_non_speculative_request_ = true; | |
416 } | |
417 | |
418 bool was_canceled() const { | |
419 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
420 return callback_.is_null(); | |
421 } | |
422 | |
423 bool was_completed() const { | |
424 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
425 return completed_attempt_number_ > 0; | |
426 } | |
427 | |
428 private: | |
399 void StartLookupAttempt() { | 429 void StartLookupAttempt() { |
400 DCHECK(origin_loop_->BelongsToCurrentThread()); | 430 DCHECK(origin_loop_->BelongsToCurrentThread()); |
401 base::TimeTicks start_time = base::TimeTicks::Now(); | 431 base::TimeTicks start_time = base::TimeTicks::Now(); |
402 ++attempt_number_; | 432 ++attempt_number_; |
403 // Dispatch the lookup attempt to a worker thread. | 433 // Dispatch the lookup attempt to a worker thread. |
404 if (!base::WorkerPool::PostTask( | 434 if (!base::WorkerPool::PostTask( |
405 FROM_HERE, | 435 FROM_HERE, |
406 base::Bind(&Job::DoLookup, this, start_time, attempt_number_), | 436 base::Bind(&ProcJob::DoLookup, this, start_time, attempt_number_), |
407 true)) { | 437 true)) { |
408 NOTREACHED(); | 438 NOTREACHED(); |
409 | 439 |
410 // Since we could be running within Resolve() right now, we can't just | 440 // Since we could be running within Resolve() right now, we can't just |
411 // call OnLookupComplete(). Instead we must wait until Resolve() has | 441 // call OnLookupComplete(). Instead we must wait until Resolve() has |
412 // returned (IO_PENDING). | 442 // returned (IO_PENDING). |
413 origin_loop_->PostTask( | 443 origin_loop_->PostTask( |
414 FROM_HERE, | 444 FROM_HERE, |
415 base::Bind(&Job::OnLookupComplete, this, AddressList(), | 445 base::Bind(&ProcJob::OnLookupComplete, this, AddressList(), |
416 start_time, attempt_number_, ERR_UNEXPECTED, 0)); | 446 start_time, attempt_number_, ERR_UNEXPECTED, 0)); |
417 return; | 447 return; |
418 } | 448 } |
419 | 449 |
420 net_log_.AddEvent( | 450 net_log_.AddEvent( |
421 NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED, | 451 NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED, |
422 make_scoped_refptr(new NetLogIntegerParameter( | 452 make_scoped_refptr(new NetLogIntegerParameter( |
423 "attempt_number", attempt_number_))); | 453 "attempt_number", attempt_number_))); |
424 | 454 |
425 // Post a task to check if we get the results within a given time. | 455 // Post a task to check if we get the results within a given time. |
426 // OnCheckForComplete has the potential for starting a new attempt on a | 456 // OnCheckForComplete has the potential for starting a new attempt on a |
427 // different worker thread if none of our outstanding attempts have | 457 // different worker thread if none of our outstanding attempts have |
428 // completed yet. | 458 // completed yet. |
429 if (attempt_number_ <= resolver_->max_retry_attempts()) { | 459 if (attempt_number_ <= params_.max_retry_attempts_) { |
430 origin_loop_->PostDelayedTask( | 460 origin_loop_->PostDelayedTask( |
431 FROM_HERE, | 461 FROM_HERE, |
432 base::Bind(&Job::OnCheckForComplete, this), | 462 base::Bind(&ProcJob::OnCheckForComplete, this), |
433 unresponsive_delay_.InMilliseconds()); | 463 params_.unresponsive_delay_.InMilliseconds()); |
434 } | 464 } |
435 } | 465 } |
436 | 466 |
437 // Cancels the current job. The Job will be orphaned. Any outstanding resolve | |
438 // attempts running on worker threads will continue running. Only once all the | |
439 // attempts complete will the final reference to this Job be released. | |
440 void Cancel() { | |
441 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
442 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); | |
443 | |
444 HostResolver* resolver = resolver_; | |
445 resolver_ = NULL; | |
446 | |
447 // End here to prevent issues when a Job outlives the HostResolver that | |
448 // spawned it. | |
449 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, NULL); | |
450 | |
451 // We will call HostResolverImpl::CancelRequest(Request*) on each one | |
452 // in order to notify any observers. | |
453 for (RequestsList::const_iterator it = requests_.begin(); | |
454 it != requests_.end(); ++it) { | |
455 HostResolverImpl::Request* req = *it; | |
456 if (!req->was_cancelled()) | |
457 resolver->CancelRequest(req); | |
458 } | |
459 } | |
460 | |
461 bool was_cancelled() const { | |
462 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
463 return resolver_ == NULL; | |
464 } | |
465 | |
466 bool was_completed() const { | |
467 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
468 return completed_attempt_number_ > 0; | |
469 } | |
470 | |
471 const Key& key() const { | |
472 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
473 return key_; | |
474 } | |
475 | |
476 int id() const { | |
477 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
478 return id_; | |
479 } | |
480 | |
481 const RequestsList& requests() const { | |
482 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
483 return requests_; | |
484 } | |
485 | |
486 // Returns the first request attached to the job. | |
487 const Request* initial_request() const { | |
488 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
489 DCHECK(!requests_.empty()); | |
490 return requests_[0]; | |
491 } | |
492 | |
493 // Returns true if |req_info| can be fulfilled by this job. | |
494 bool CanServiceRequest(const RequestInfo& req_info) const { | |
495 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
496 return key_ == resolver_->GetEffectiveKeyForRequest(req_info); | |
497 } | |
498 | |
499 private: | |
500 friend class base::RefCountedThreadSafe<HostResolverImpl::Job>; | |
501 | |
502 ~Job() { | |
503 // Free the requests attached to this job. | |
504 STLDeleteElements(&requests_); | |
505 } | |
506 | |
507 // WARNING: This code runs inside a worker pool. The shutdown code cannot | 467 // WARNING: This code runs inside a worker pool. The shutdown code cannot |
508 // wait for it to finish, so we must be very careful here about using other | 468 // wait for it to finish, so we must be very careful here about using other |
509 // objects (like MessageLoops, Singletons, etc). During shutdown these objects | 469 // objects (like MessageLoops, Singletons, etc). During shutdown these objects |
510 // may no longer exist. Multiple DoLookups() could be running in parallel, so | 470 // may no longer exist. Multiple DoLookups() could be running in parallel, so |
511 // any state inside of |this| must not mutate . | 471 // any state inside of |this| must not mutate . |
512 void DoLookup(const base::TimeTicks& start_time, | 472 void DoLookup(const base::TimeTicks& start_time, |
513 const uint32 attempt_number) { | 473 const uint32 attempt_number) { |
514 AddressList results; | 474 AddressList results; |
515 int os_error = 0; | 475 int os_error = 0; |
516 // Running on the worker thread | 476 // Running on the worker thread |
517 int error = ResolveAddrInfo(resolver_proc_, | 477 int error = ResolveAddrInfo(params_.resolver_proc_, |
518 key_.hostname, | 478 key_.hostname, |
519 key_.address_family, | 479 key_.address_family, |
520 key_.host_resolver_flags, | 480 key_.host_resolver_flags, |
521 &results, | 481 &results, |
522 &os_error); | 482 &os_error); |
523 | 483 |
524 origin_loop_->PostTask( | 484 origin_loop_->PostTask( |
525 FROM_HERE, | 485 FROM_HERE, |
526 base::Bind(&Job::OnLookupComplete, this, results, start_time, | 486 base::Bind(&ProcJob::OnLookupComplete, this, results, start_time, |
527 attempt_number, error, os_error)); | 487 attempt_number, error, os_error)); |
528 } | 488 } |
529 | 489 |
530 // Callback to see if DoLookup() has finished or not (runs on origin thread). | 490 // Callback to see if DoLookup() has finished or not (runs on origin thread). |
531 void OnCheckForComplete() { | 491 void OnCheckForComplete() { |
532 DCHECK(origin_loop_->BelongsToCurrentThread()); | 492 DCHECK(origin_loop_->BelongsToCurrentThread()); |
533 | 493 |
534 if (was_completed() || was_cancelled()) | 494 if (was_completed() || was_canceled()) |
535 return; | 495 return; |
536 | 496 |
537 DCHECK(resolver_); | 497 params_.unresponsive_delay_ *= params_.retry_factor_; |
538 unresponsive_delay_ *= resolver_->retry_factor(); | |
539 StartLookupAttempt(); | 498 StartLookupAttempt(); |
540 } | 499 } |
541 | 500 |
542 // Callback for when DoLookup() completes (runs on origin thread). | 501 // Callback for when DoLookup() completes (runs on origin thread). |
543 void OnLookupComplete(const AddressList& results, | 502 void OnLookupComplete(const AddressList& results, |
544 const base::TimeTicks& start_time, | 503 const base::TimeTicks& start_time, |
545 const uint32 attempt_number, | 504 const uint32 attempt_number, |
546 int error, | 505 int error, |
547 const int os_error) { | 506 const int os_error) { |
548 DCHECK(origin_loop_->BelongsToCurrentThread()); | 507 DCHECK(origin_loop_->BelongsToCurrentThread()); |
549 DCHECK(error || results.head()); | 508 DCHECK(error || results.head()); |
550 | 509 |
551 bool was_retry_attempt = attempt_number > 1; | 510 bool was_retry_attempt = attempt_number > 1; |
552 | 511 |
553 if (!was_cancelled()) { | 512 if (!was_canceled()) { |
554 scoped_refptr<NetLog::EventParameters> params; | 513 scoped_refptr<NetLog::EventParameters> params; |
555 if (error != OK) { | 514 if (error != OK) { |
556 params = new HostResolveFailedParams(attempt_number, error, os_error); | 515 params = new HostResolveFailedParams(attempt_number, error, os_error); |
557 } else { | 516 } else { |
558 params = new NetLogIntegerParameter("attempt_number", attempt_number_); | 517 params = new NetLogIntegerParameter("attempt_number", attempt_number_); |
559 } | 518 } |
560 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED, | 519 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED, |
561 params); | 520 params); |
562 | 521 |
563 // If host is already resolved, then record data and return. | 522 // If host is already resolved, then record data and return. |
(...skipping 20 matching lines...) Expand all Loading... | |
584 if (error != OK && NetworkChangeNotifier::IsOffline()) | 543 if (error != OK && NetworkChangeNotifier::IsOffline()) |
585 error = ERR_INTERNET_DISCONNECTED; | 544 error = ERR_INTERNET_DISCONNECTED; |
586 | 545 |
587 // We will record data for the first attempt. Don't contaminate with retry | 546 // We will record data for the first attempt. Don't contaminate with retry |
588 // attempt's data. | 547 // attempt's data. |
589 if (!was_retry_attempt) | 548 if (!was_retry_attempt) |
590 RecordPerformanceHistograms(start_time, error, os_error); | 549 RecordPerformanceHistograms(start_time, error, os_error); |
591 | 550 |
592 RecordAttemptHistograms(start_time, attempt_number, error, os_error); | 551 RecordAttemptHistograms(start_time, attempt_number, error, os_error); |
593 | 552 |
594 if (was_cancelled()) | 553 if (was_canceled()) |
595 return; | 554 return; |
596 | 555 |
597 if (was_retry_attempt) { | 556 if (was_retry_attempt) { |
598 // If retry attempt finishes before 1st attempt, then get stats on how | 557 // If retry attempt finishes before 1st attempt, then get stats on how |
599 // much time is saved by having spawned an extra attempt. | 558 // much time is saved by having spawned an extra attempt. |
600 retry_attempt_finished_time_ = base::TimeTicks::Now(); | 559 retry_attempt_finished_time_ = base::TimeTicks::Now(); |
601 } | 560 } |
602 | 561 |
603 scoped_refptr<NetLog::EventParameters> params; | 562 scoped_refptr<NetLog::EventParameters> params; |
604 if (error != OK) { | 563 if (error != OK) { |
605 params = new HostResolveFailedParams(0, error, os_error); | 564 params = new HostResolveFailedParams(0, error, os_error); |
606 } else { | 565 } else { |
607 params = new AddressListNetLogParam(results_); | 566 params = new AddressListNetLogParam(results_); |
608 } | 567 } |
609 | 568 |
610 // End here to prevent issues when a Job outlives the HostResolver that | 569 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_JOB, params); |
611 // spawned it. | |
612 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, params); | |
613 | 570 |
614 DCHECK(!requests_.empty()); | 571 Callback callback = callback_; |
615 | 572 callback_.Reset(); |
616 // Use the port number of the first request. | 573 callback.Run(error, os_error, results_); |
617 if (error == OK) | |
618 MutableSetPort(requests_[0]->port(), &results_); | |
619 | |
620 resolver_->OnJobComplete(this, error, os_error, results_); | |
621 } | 574 } |
622 | 575 |
623 void RecordPerformanceHistograms(const base::TimeTicks& start_time, | 576 void RecordPerformanceHistograms(const base::TimeTicks& start_time, |
624 const int error, | 577 const int error, |
625 const int os_error) const { | 578 const int os_error) const { |
626 DCHECK(origin_loop_->BelongsToCurrentThread()); | 579 DCHECK(origin_loop_->BelongsToCurrentThread()); |
627 enum Category { // Used in HISTOGRAM_ENUMERATION. | 580 enum Category { // Used in HISTOGRAM_ENUMERATION. |
628 RESOLVE_SUCCESS, | 581 RESOLVE_SUCCESS, |
629 RESOLVE_FAIL, | 582 RESOLVE_FAIL, |
630 RESOLVE_SPECULATIVE_SUCCESS, | 583 RESOLVE_SPECULATIVE_SUCCESS, |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
706 DNS_HISTOGRAM(base::FieldTrial::MakeName("DNS.ResolveSuccess", | 659 DNS_HISTOGRAM(base::FieldTrial::MakeName("DNS.ResolveSuccess", |
707 "DnsParallelism"), duration); | 660 "DnsParallelism"), duration); |
708 } | 661 } |
709 } | 662 } |
710 } | 663 } |
711 | 664 |
712 void RecordAttemptHistograms(const base::TimeTicks& start_time, | 665 void RecordAttemptHistograms(const base::TimeTicks& start_time, |
713 const uint32 attempt_number, | 666 const uint32 attempt_number, |
714 const int error, | 667 const int error, |
715 const int os_error) const { | 668 const int os_error) const { |
669 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
716 bool first_attempt_to_complete = | 670 bool first_attempt_to_complete = |
717 completed_attempt_number_ == attempt_number; | 671 completed_attempt_number_ == attempt_number; |
718 bool is_first_attempt = (attempt_number == 1); | 672 bool is_first_attempt = (attempt_number == 1); |
719 | 673 |
720 if (first_attempt_to_complete) { | 674 if (first_attempt_to_complete) { |
721 // If this was first attempt to complete, then record the resolution | 675 // If this was first attempt to complete, then record the resolution |
722 // status of the attempt. | 676 // status of the attempt. |
723 if (completed_attempt_error_ == OK) { | 677 if (completed_attempt_error_ == OK) { |
724 UMA_HISTOGRAM_ENUMERATION( | 678 UMA_HISTOGRAM_ENUMERATION( |
725 "DNS.AttemptFirstSuccess", attempt_number, 100); | 679 "DNS.AttemptFirstSuccess", attempt_number, 100); |
726 } else { | 680 } else { |
727 UMA_HISTOGRAM_ENUMERATION( | 681 UMA_HISTOGRAM_ENUMERATION( |
728 "DNS.AttemptFirstFailure", attempt_number, 100); | 682 "DNS.AttemptFirstFailure", attempt_number, 100); |
729 } | 683 } |
730 } | 684 } |
731 | 685 |
732 if (error == OK) | 686 if (error == OK) |
733 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100); | 687 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100); |
734 else | 688 else |
735 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100); | 689 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100); |
736 | 690 |
737 // If first attempt didn't finish before retry attempt, then calculate stats | 691 // If first attempt didn't finish before retry attempt, then calculate stats |
738 // on how much time is saved by having spawned an extra attempt. | 692 // on how much time is saved by having spawned an extra attempt. |
739 if (!first_attempt_to_complete && is_first_attempt && !was_cancelled()) { | 693 if (!first_attempt_to_complete && is_first_attempt && !was_canceled()) { |
740 DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry", | 694 DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry", |
741 base::TimeTicks::Now() - retry_attempt_finished_time_); | 695 base::TimeTicks::Now() - retry_attempt_finished_time_); |
742 } | 696 } |
743 | 697 |
744 if (was_cancelled() || !first_attempt_to_complete) { | 698 if (was_canceled() || !first_attempt_to_complete) { |
745 // Count those attempts which completed after the job was already canceled | 699 // Count those attempts which completed after the job was already canceled |
746 // OR after the job was already completed by an earlier attempt (so in | 700 // OR after the job was already completed by an earlier attempt (so in |
747 // effect). | 701 // effect). |
748 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100); | 702 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100); |
749 | 703 |
750 // Record if job is cancelled. | 704 // Record if job is canceled. |
751 if (was_cancelled()) | 705 if (was_canceled()) |
752 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100); | 706 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100); |
753 } | 707 } |
754 | 708 |
755 base::TimeDelta duration = base::TimeTicks::Now() - start_time; | 709 base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
756 if (error == OK) | 710 if (error == OK) |
757 DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration); | 711 DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration); |
758 else | 712 else |
759 DNS_HISTOGRAM("DNS.AttemptFailDuration", duration); | 713 DNS_HISTOGRAM("DNS.AttemptFailDuration", duration); |
760 } | 714 } |
761 | 715 |
762 // Immutable. Can be read from either thread, | |
763 const int id_; | |
764 | |
765 // Set on the origin thread, read on the worker thread. | 716 // Set on the origin thread, read on the worker thread. |
766 Key key_; | 717 Key key_; |
767 | 718 |
768 // Only used on the origin thread (where Resolve was called). | 719 // Holds an owning reference to the HostResolverProc that we are going to use. |
769 HostResolverImpl* resolver_; | 720 // This may not be the current resolver procedure by the time we call |
770 RequestsList requests_; // The requests waiting on this job. | 721 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning |
722 // reference ensures that it remains valid until we are done. | |
723 ProcJobParams params_; | |
724 | |
725 // The listener to the results of this ProcJob. | |
726 Callback callback_; | |
771 | 727 |
772 // Used to post ourselves onto the origin thread. | 728 // Used to post ourselves onto the origin thread. |
773 scoped_refptr<base::MessageLoopProxy> origin_loop_; | 729 scoped_refptr<base::MessageLoopProxy> origin_loop_; |
774 | 730 |
775 // Hold an owning reference to the HostResolverProc that we are going to use. | |
776 // This may not be the current resolver procedure by the time we call | |
777 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning | |
778 // reference ensures that it remains valid until we are done. | |
779 scoped_refptr<HostResolverProc> resolver_proc_; | |
780 | |
781 // The amount of time after starting a resolution attempt until deciding to | |
782 // retry. | |
783 base::TimeDelta unresponsive_delay_; | |
784 | |
785 // Keeps track of the number of attempts we have made so far to resolve the | 731 // Keeps track of the number of attempts we have made so far to resolve the |
786 // host. Whenever we start an attempt to resolve the host, we increase this | 732 // host. Whenever we start an attempt to resolve the host, we increase this |
787 // number. | 733 // number. |
788 uint32 attempt_number_; | 734 uint32 attempt_number_; |
789 | 735 |
790 // The index of the attempt which finished first (or 0 if the job is still in | 736 // The index of the attempt which finished first (or 0 if the job is still in |
791 // progress). | 737 // progress). |
792 uint32 completed_attempt_number_; | 738 uint32 completed_attempt_number_; |
793 | 739 |
794 // The result (a net error code) from the first attempt to complete. | 740 // The result (a net error code) from the first attempt to complete. |
795 int completed_attempt_error_; | 741 int completed_attempt_error_; |
796 | 742 |
797 // The time when retry attempt was finished. | 743 // The time when retry attempt was finished. |
798 base::TimeTicks retry_attempt_finished_time_; | 744 base::TimeTicks retry_attempt_finished_time_; |
799 | 745 |
800 // True if a non-speculative request was ever attached to this job | 746 // True if a non-speculative request was ever attached to this job |
801 // (regardless of whether or not it was later cancelled. | 747 // (regardless of whether or not it was later canceled. |
802 // This boolean is used for histogramming the duration of jobs used to | 748 // This boolean is used for histogramming the duration of jobs used to |
803 // service non-speculative requests. | 749 // service non-speculative requests. |
804 bool had_non_speculative_request_; | 750 bool had_non_speculative_request_; |
805 | 751 |
806 AddressList results_; | 752 AddressList results_; |
807 | 753 |
808 BoundNetLog net_log_; | 754 BoundNetLog net_log_; |
809 | 755 |
810 DISALLOW_COPY_AND_ASSIGN(Job); | 756 DISALLOW_COPY_AND_ASSIGN(ProcJob); |
811 }; | 757 }; |
812 | 758 |
813 //----------------------------------------------------------------------------- | 759 //----------------------------------------------------------------------------- |
814 | 760 |
815 // This class represents a request to the worker pool for a "probe for IPv6 | 761 // Represents a request to the worker pool for a "probe for IPv6 support" call. |
816 // support" call. | |
817 class HostResolverImpl::IPv6ProbeJob | 762 class HostResolverImpl::IPv6ProbeJob |
818 : public base::RefCountedThreadSafe<HostResolverImpl::IPv6ProbeJob> { | 763 : public base::RefCountedThreadSafe<HostResolverImpl::IPv6ProbeJob> { |
819 public: | 764 public: |
820 explicit IPv6ProbeJob(HostResolverImpl* resolver) | 765 explicit IPv6ProbeJob(HostResolverImpl* resolver) |
821 : resolver_(resolver), | 766 : resolver_(resolver), |
822 origin_loop_(base::MessageLoopProxy::current()) { | 767 origin_loop_(base::MessageLoopProxy::current()) { |
823 DCHECK(resolver); | 768 DCHECK(resolver); |
824 } | 769 } |
825 | 770 |
826 void Start() { | 771 void Start() { |
827 DCHECK(origin_loop_->BelongsToCurrentThread()); | 772 DCHECK(origin_loop_->BelongsToCurrentThread()); |
828 if (was_cancelled()) | 773 if (was_canceled()) |
829 return; | 774 return; |
830 const bool kIsSlow = true; | 775 const bool kIsSlow = true; |
831 base::WorkerPool::PostTask( | 776 base::WorkerPool::PostTask( |
832 FROM_HERE, base::Bind(&IPv6ProbeJob::DoProbe, this), kIsSlow); | 777 FROM_HERE, base::Bind(&IPv6ProbeJob::DoProbe, this), kIsSlow); |
833 } | 778 } |
834 | 779 |
835 // Cancels the current job. | 780 // Cancels the current job. |
836 void Cancel() { | 781 void Cancel() { |
837 DCHECK(origin_loop_->BelongsToCurrentThread()); | 782 DCHECK(origin_loop_->BelongsToCurrentThread()); |
838 if (was_cancelled()) | 783 if (was_canceled()) |
839 return; | 784 return; |
840 resolver_ = NULL; // Read/write ONLY on origin thread. | 785 resolver_ = NULL; // Read/write ONLY on origin thread. |
841 } | 786 } |
842 | 787 |
843 private: | 788 private: |
844 friend class base::RefCountedThreadSafe<HostResolverImpl::IPv6ProbeJob>; | 789 friend class base::RefCountedThreadSafe<HostResolverImpl::IPv6ProbeJob>; |
845 | 790 |
846 ~IPv6ProbeJob() { | 791 ~IPv6ProbeJob() { |
847 } | 792 } |
848 | 793 |
849 bool was_cancelled() const { | 794 bool was_canceled() const { |
850 DCHECK(origin_loop_->BelongsToCurrentThread()); | 795 DCHECK(origin_loop_->BelongsToCurrentThread()); |
851 return !resolver_; | 796 return !resolver_; |
852 } | 797 } |
853 | 798 |
854 // Run on worker thread. | 799 // Run on worker thread. |
855 void DoProbe() { | 800 void DoProbe() { |
856 // Do actual testing on this thread, as it takes 40-100ms. | 801 // Do actual testing on this thread, as it takes 40-100ms. |
857 AddressFamily family = IPv6Supported() ? ADDRESS_FAMILY_UNSPECIFIED | 802 AddressFamily family = IPv6Supported() ? ADDRESS_FAMILY_UNSPECIFIED |
858 : ADDRESS_FAMILY_IPV4; | 803 : ADDRESS_FAMILY_IPV4; |
859 | 804 |
860 origin_loop_->PostTask( | 805 origin_loop_->PostTask( |
861 FROM_HERE, | 806 FROM_HERE, |
862 base::Bind(&IPv6ProbeJob::OnProbeComplete, this, family)); | 807 base::Bind(&IPv6ProbeJob::OnProbeComplete, this, family)); |
863 } | 808 } |
864 | 809 |
865 // Callback for when DoProbe() completes. | 810 // Callback for when DoProbe() completes. |
866 void OnProbeComplete(AddressFamily address_family) { | 811 void OnProbeComplete(AddressFamily address_family) { |
867 DCHECK(origin_loop_->BelongsToCurrentThread()); | 812 DCHECK(origin_loop_->BelongsToCurrentThread()); |
868 if (was_cancelled()) | 813 if (was_canceled()) |
869 return; | 814 return; |
870 resolver_->IPv6ProbeSetDefaultAddressFamily(address_family); | 815 resolver_->IPv6ProbeSetDefaultAddressFamily(address_family); |
871 } | 816 } |
872 | 817 |
873 // Used/set only on origin thread. | 818 // Used/set only on origin thread. |
874 HostResolverImpl* resolver_; | 819 HostResolverImpl* resolver_; |
875 | 820 |
876 // Used to post ourselves onto the origin thread. | 821 // Used to post ourselves onto the origin thread. |
877 scoped_refptr<base::MessageLoopProxy> origin_loop_; | 822 scoped_refptr<base::MessageLoopProxy> origin_loop_; |
878 | 823 |
879 DISALLOW_COPY_AND_ASSIGN(IPv6ProbeJob); | 824 DISALLOW_COPY_AND_ASSIGN(IPv6ProbeJob); |
880 }; | 825 }; |
881 | 826 |
882 //----------------------------------------------------------------------------- | 827 //----------------------------------------------------------------------------- |
883 | 828 |
884 // We rely on the priority enum values being sequential having starting at 0, | 829 // Keeps track of the highest priority. |
885 // and increasing for lower priorities. | 830 class PriorityTracker { |
886 COMPILE_ASSERT(HIGHEST == 0u && | |
887 LOWEST > HIGHEST && | |
888 IDLE > LOWEST && | |
889 NUM_PRIORITIES > IDLE, | |
890 priority_indexes_incompatible); | |
891 | |
892 // JobPool contains all the information relating to queued requests, including | |
893 // the limits on how many jobs are allowed to be used for this category of | |
894 // requests. | |
895 class HostResolverImpl::JobPool { | |
896 public: | 831 public: |
897 JobPool(size_t max_outstanding_jobs, size_t max_pending_requests) | 832 explicit PriorityTracker(RequestPriority initial) |
mmenke
2011/12/22 16:18:49
Don't think there's any reason to take a RequestPr
szym
2011/12/22 16:56:57
Not quite immediately.
The Job is added to the di
mmenke
2011/12/22 17:34:30
Ah, good point. Think it's fine as-is, then.
szym
2011/12/28 01:24:10
A new work-around in place using PrioritizedDispat
| |
898 : num_outstanding_jobs_(0u) { | 833 : highest_priority_(initial), total_count_(0) { |
899 SetConstraints(max_outstanding_jobs, max_pending_requests); | 834 memset(counts_, 0, sizeof(counts_)); |
900 } | 835 } |
901 | 836 |
902 ~JobPool() { | 837 RequestPriority highest_priority() const { |
903 // Free the pending requests. | 838 return highest_priority_; |
904 for (size_t i = 0; i < arraysize(pending_requests_); ++i) | 839 } |
905 STLDeleteElements(&pending_requests_[i]); | 840 |
906 } | 841 size_t total_count() const { |
907 | 842 return total_count_; |
908 // Sets the constraints for this pool. See SetPoolConstraints() for the | 843 } |
909 // specific meaning of these parameters. | 844 |
910 void SetConstraints(size_t max_outstanding_jobs, | 845 // Returns true if priority changed. |
911 size_t max_pending_requests) { | 846 bool Add(RequestPriority req_priority) { |
912 CHECK_NE(max_outstanding_jobs, 0u); | 847 ++total_count_; |
913 max_outstanding_jobs_ = max_outstanding_jobs; | 848 ++counts_[req_priority]; |
914 max_pending_requests_ = max_pending_requests; | 849 if (highest_priority_ < req_priority) |
915 } | 850 return false; |
916 | 851 |
917 // Returns the number of pending requests enqueued to this pool. | 852 highest_priority_ = req_priority; |
918 // A pending request is one waiting to be attached to a job. | 853 return true; |
919 size_t GetNumPendingRequests() const { | 854 } |
920 size_t total = 0u; | 855 |
921 for (size_t i = 0u; i < arraysize(pending_requests_); ++i) | 856 // Returns true if priority changed. |
922 total += pending_requests_[i].size(); | 857 bool Remove(RequestPriority req_priority) { |
923 return total; | 858 DCHECK_GT(total_count_, 0u); |
924 } | 859 DCHECK_GT(counts_[req_priority], 0u); |
925 | 860 --total_count_; |
926 bool HasPendingRequests() const { | 861 --counts_[req_priority]; |
927 return GetNumPendingRequests() > 0u; | 862 if ((counts_[req_priority] == 0u) && (highest_priority_ == req_priority)) { |
928 } | 863 for (size_t i = highest_priority_; i < NUM_PRIORITIES; ++i) { |
929 | 864 if (counts_[i] > 0) { |
930 // Enqueues a request to this pool. As a result of enqueing this request, | 865 highest_priority_ = static_cast<RequestPriority>(i); |
931 // the queue may have reached its maximum size. In this case, a request is | 866 return true; |
932 // evicted from the queue, and returned. Otherwise returns NULL. The caller | |
933 // is responsible for freeing the evicted request. | |
934 Request* InsertPendingRequest(Request* req) { | |
935 req->request_net_log().BeginEvent( | |
936 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE, | |
937 NULL); | |
938 | |
939 PendingRequestsQueue& q = pending_requests_[req->info().priority()]; | |
940 q.push_back(req); | |
941 | |
942 // If the queue is too big, kick out the lowest priority oldest request. | |
943 if (GetNumPendingRequests() > max_pending_requests_) { | |
944 // Iterate over the queues from lowest priority to highest priority. | |
945 for (int i = static_cast<int>(arraysize(pending_requests_)) - 1; | |
946 i >= 0; --i) { | |
947 PendingRequestsQueue& q = pending_requests_[i]; | |
948 if (!q.empty()) { | |
949 Request* req = q.front(); | |
950 q.pop_front(); | |
951 req->request_net_log().AddEvent( | |
952 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE_EVICTED, NULL); | |
953 req->request_net_log().EndEvent( | |
954 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE, NULL); | |
955 return req; | |
956 } | 867 } |
957 } | 868 } |
958 } | 869 DCHECK_EQ(0u, total_count_); |
959 | 870 // In absence of requests set default. |
960 return NULL; | 871 if (highest_priority_ != IDLE) { |
961 } | 872 highest_priority_ = IDLE; |
962 | 873 return true; |
963 // Erases |req| from this container. Caller is responsible for freeing | |
964 // |req| afterwards. | |
965 void RemovePendingRequest(Request* req) { | |
966 PendingRequestsQueue& q = pending_requests_[req->info().priority()]; | |
967 PendingRequestsQueue::iterator it = std::find(q.begin(), q.end(), req); | |
968 DCHECK(it != q.end()); | |
969 q.erase(it); | |
970 req->request_net_log().EndEvent( | |
971 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE, NULL); | |
972 } | |
973 | |
974 // Removes and returns the highest priority pending request. | |
975 Request* RemoveTopPendingRequest() { | |
976 DCHECK(HasPendingRequests()); | |
977 | |
978 for (size_t i = 0u; i < arraysize(pending_requests_); ++i) { | |
979 PendingRequestsQueue& q = pending_requests_[i]; | |
980 if (!q.empty()) { | |
981 Request* req = q.front(); | |
982 q.pop_front(); | |
983 req->request_net_log().EndEvent( | |
984 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE, NULL); | |
985 return req; | |
986 } | 874 } |
987 } | 875 } |
988 | 876 return false; |
989 NOTREACHED(); | 877 } |
990 return NULL; | 878 |
991 } | 879 private: |
992 | 880 RequestPriority highest_priority_; |
993 // Keeps track of a job that was just added/removed, and belongs to this pool. | 881 size_t total_count_; |
994 void AdjustNumOutstandingJobs(int offset) { | 882 size_t counts_[NUM_PRIORITIES]; |
995 DCHECK(offset == 1 || (offset == -1 && num_outstanding_jobs_ > 0u)); | 883 }; |
996 num_outstanding_jobs_ += offset; | 884 |
997 } | 885 //----------------------------------------------------------------------------- |
998 | 886 |
999 void ResetNumOutstandingJobs() { | 887 // Aggregates all Requests for the same Key. Dispatched via PriorityDispatch. |
1000 num_outstanding_jobs_ = 0; | 888 // Spawns ProcJob when started. |
1001 } | 889 class HostResolverImpl::Job : public PriorityDispatch::Job { |
1002 | 890 public: |
1003 // Returns true if a new job can be created for this pool. | 891 // Creates new job for |key| where |request_net_log| is bound to the |
1004 bool CanCreateJob() const { | 892 // request that spawned it. |
1005 return num_outstanding_jobs_ + 1u <= max_outstanding_jobs_; | 893 Job(HostResolverImpl* resolver, |
1006 } | 894 const Key& key, |
1007 | 895 RequestPriority priority, |
1008 // Removes any pending requests from the queue which are for the | 896 const BoundNetLog& request_net_log) |
1009 // same (hostname / effective address-family) as |job|, and attaches them to | 897 : resolver_(resolver->AsWeakPtr()), |
1010 // |job|. | 898 key_(key), |
1011 void MoveRequestsToJob(Job* job) { | 899 priority_tracker_(priority), |
1012 for (size_t i = 0u; i < arraysize(pending_requests_); ++i) { | 900 had_non_speculative_request_(false), |
1013 PendingRequestsQueue& q = pending_requests_[i]; | 901 net_log_(BoundNetLog::Make(request_net_log.net_log(), |
1014 PendingRequestsQueue::iterator req_it = q.begin(); | 902 NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB)), |
1015 while (req_it != q.end()) { | 903 net_error_(ERR_IO_PENDING), |
1016 Request* req = *req_it; | 904 os_error_(0) { |
1017 if (job->CanServiceRequest(req->info())) { | 905 request_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB, NULL); |
1018 // Job takes ownership of |req|. | 906 |
1019 job->AddRequest(req); | 907 net_log_.BeginEvent( |
1020 req_it = q.erase(req_it); | 908 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
1021 } else { | 909 make_scoped_refptr(new JobCreationParameters( |
1022 ++req_it; | 910 key_.hostname, request_net_log.source()))); |
1023 } | 911 } |
912 | |
913 virtual ~Job() { | |
914 if (net_error_ == ERR_IO_PENDING) | |
915 Cancel(); | |
916 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, NULL); | |
mmenke
2011/12/22 16:18:49
Think we should log the error here, if any. Code
szym
2011/12/28 01:24:10
Done.
| |
917 STLDeleteElements(&requests_); | |
918 } | |
919 | |
920 HostResolverImpl* resolver() const { | |
921 return resolver_; | |
922 } | |
923 | |
924 RequestPriority priority() const { | |
925 return priority_tracker_.highest_priority(); | |
926 } | |
927 | |
928 // Number of non-canceled requests in |requests_|. | |
929 size_t num_active_requests() const { | |
930 return priority_tracker_.total_count(); | |
931 } | |
932 | |
933 const Key& key() const { | |
934 return key_; | |
935 } | |
936 | |
937 int net_error() const { | |
938 return net_error_; | |
939 } | |
940 | |
941 // Used by HostResolverImpl in |dispatch_|. | |
942 PriorityDispatch::Handle& handle() { | |
943 return handle_; | |
944 } | |
945 | |
946 // The Job will own |req| and destroy it in ~Job. | |
947 void AddRequest(Request* req) { | |
948 DCHECK_EQ(key_.hostname, req->info().hostname()); | |
949 | |
950 req->request_net_log().AddEvent( | |
951 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, | |
952 make_scoped_refptr(new NetLogSourceParameter( | |
953 "source_dependency", net_log_.source()))); | |
954 | |
955 net_log_.AddEvent( | |
956 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, | |
957 make_scoped_refptr(new NetLogSourceParameter( | |
958 "source_dependency", req->request_net_log().source()))); | |
mmenke
2011/12/22 16:18:49
May be a little simpler to just always add the res
szym
2011/12/28 01:24:10
Done.
| |
959 | |
960 req->set_job(this); | |
961 requests_.push_back(req); | |
962 | |
963 if (priority_tracker_.Add(req->info().priority())) { | |
964 net_log_.AddEvent( | |
965 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_PRIORITY_UPDATED, | |
966 make_scoped_refptr(new NetLogIntegerParameter( | |
967 "priority", priority()))); | |
968 } | |
969 | |
970 // TODO(szym): Check if this is still needed. | |
971 if (!req->info().is_speculative()) { | |
972 had_non_speculative_request_ = true; | |
973 if (proc_job_) | |
974 proc_job_->set_had_non_speculative_request(); | |
975 } | |
976 } | |
977 | |
978 void CancelRequest(Request* req) { | |
979 DCHECK_EQ(key_.hostname, req->info().hostname()); | |
980 DCHECK(!req->was_canceled()); | |
981 // Don't remove it from |requests_| just mark it canceled. | |
982 req->MarkAsCanceled(); | |
983 LogCancelRequest(req->source_net_log(), req->request_net_log(), | |
984 req->info()); | |
985 if (priority_tracker_.Remove(req->info().priority())) { | |
986 net_log_.AddEvent( | |
987 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_PRIORITY_UPDATED, | |
988 make_scoped_refptr(new NetLogIntegerParameter( | |
989 "priority", priority()))); | |
990 } | |
991 } | |
992 | |
993 // Called by resolver in destructor. | |
994 void Cancel() { | |
995 if (is_running()) { | |
996 proc_job_->Cancel(); | |
997 proc_job_ = NULL; | |
mmenke
2011/12/22 17:34:30
nit: I think calling release would be a little cl
mmenke
2011/12/22 17:55:14
Oops...Nevermind. Forgot that release with scoped
| |
998 } | |
999 if (!net_log_.net_log()) | |
mmenke
2011/12/22 16:18:49
Since this generally only happens during testing,
szym
2011/12/22 16:56:57
I don't think cancellations will happen only durin
mmenke
2011/12/22 17:34:30
Sorry, I mean net_log_ will probably only be null
| |
1000 return; | |
1001 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); | |
1002 for (RequestsList::const_iterator it = requests_.begin(); | |
1003 it != requests_.end(); ++it) { | |
1004 Request* req = *it; | |
1005 if (req->was_canceled()) | |
1006 continue; | |
1007 DCHECK_EQ(this, req->job()); | |
1008 LogCancelRequest(req->source_net_log(), req->request_net_log(), | |
1009 req->info()); | |
1010 } | |
1011 } | |
1012 | |
1013 // Called by ProcJob when it completes. | |
1014 void OnProcJobComplete(int net_error, int os_error, | |
1015 const AddressList& addrlist) { | |
1016 DCHECK(is_running()); | |
1017 proc_job_ = NULL; | |
1018 net_error_ = net_error; | |
1019 os_error_ = os_error; | |
1020 CompleteRequests(addrlist); | |
1021 } | |
1022 | |
1023 void Abort() { | |
1024 // Job should only be aborted if it's running. | |
1025 DCHECK(is_running()); | |
1026 proc_job_->Cancel(); | |
1027 proc_job_ = NULL; | |
1028 net_error_ = ERR_ABORTED; | |
1029 os_error_ = 0; | |
1030 CompleteRequests(AddressList()); | |
1031 } | |
1032 | |
1033 bool was_evicted() const { | |
1034 return net_error_ == ERR_HOST_RESOLVER_QUEUE_TOO_LARGE; | |
1035 } | |
1036 | |
1037 bool is_running() const { | |
1038 return proc_job_.get() != NULL; | |
1039 } | |
1040 | |
1041 // PriorityDispatch::Job interface. | |
1042 virtual void Start() OVERRIDE { | |
1043 DCHECK(!is_running()); | |
1044 handle_ = PriorityDispatch::Handle(); | |
1045 if (net_log_.net_log()) { | |
mmenke
2011/12/22 16:18:49
nit: Again, don't think this is needed.
| |
1046 // Log end of queue. | |
mmenke
2011/12/22 16:18:49
You don't actually seem to be logging anything her
szym
2011/12/22 16:56:57
Oops. Leftovers. Done.
| |
1047 for (RequestsList::const_iterator it = requests_.begin(); | |
1048 it != requests_.end(); ++it) { | |
1049 Request* req = *it; | |
1050 if (req->was_canceled()) | |
1051 continue; | |
1052 DCHECK_EQ(this, req->job()); | |
1024 } | 1053 } |
1025 } | 1054 } |
1055 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_STARTED, NULL); | |
1056 | |
1057 proc_job_ = new ProcJob( | |
1058 key_, | |
1059 resolver_->proc_params_, | |
1060 base::Bind(&Job::OnProcJobComplete, base::Unretained(this)), | |
1061 net_log_); | |
1062 | |
1063 if (had_non_speculative_request_) | |
1064 proc_job_->set_had_non_speculative_request(); | |
1065 // Start() could be called from within Resolve(), hence it must NOT directly | |
1066 // call OnProcJobComplete. | |
mmenke
2011/12/22 16:18:49
nit: To be a little clearer, I think you should a
szym
2011/12/28 01:24:10
Done.
| |
1067 proc_job_->Start(); | |
1068 } | |
1069 | |
1070 virtual void OnEvicted() OVERRIDE { | |
1071 // Must not be running. | |
1072 DCHECK(!is_running()); | |
1073 handle_ = PriorityDispatch::Handle(); | |
1074 | |
1075 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_EVICTED, NULL); | |
1076 | |
1077 // NOTE: This signals to OnJobFinished that this job never ran. | |
1078 net_error_ = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE; | |
1079 os_error_ = 0; | |
1080 CompleteRequests(AddressList()); | |
1026 } | 1081 } |
1027 | 1082 |
1028 private: | 1083 private: |
1029 typedef std::deque<Request*> PendingRequestsQueue; | 1084 // Completes all Requests. Calls OnJobFinished and deletes self as needed. |
1030 | 1085 void CompleteRequests(const AddressList& addrlist) { |
1031 // Maximum number of concurrent jobs allowed to be started for requests | 1086 CHECK(resolver_); |
1032 // belonging to this pool. | 1087 |
1033 size_t max_outstanding_jobs_; | 1088 scoped_ptr<Job> self_deleter; |
1034 | 1089 if (net_error_ != ERR_ABORTED) { |
1035 // The current number of running jobs that were started for requests | 1090 if (!was_evicted()) |
1036 // belonging to this pool. | 1091 resolver_->OnJobFinished(this, addrlist); |
mmenke
2011/12/22 16:18:49
This is only called in response to OnProcJobComple
szym
2011/12/22 16:56:57
It makes sense for me to move the !was_evicted che
mmenke
2011/12/22 17:34:30
Hmm...Actually, don't we need to call OnJobFinishe
szym
2011/12/28 01:24:10
That's correct, although that was already done in
| |
1037 size_t num_outstanding_jobs_; | 1092 if (resolver_->RemoveJob(this)) |
1038 | 1093 self_deleter.reset(this); |
1039 // The maximum number of requests we allow to be waiting on a job, | 1094 // else: We are in AbortAllInProgressJobs() or Resolve() which will take |
1040 // for this pool. | 1095 // care of deleting this. |
1041 size_t max_pending_requests_; | 1096 } |
1042 | 1097 |
1043 // The requests which are waiting to be started for this pool. | 1098 // Complete all of the requests that were attached to the job. |
1044 PendingRequestsQueue pending_requests_[NUM_PRIORITIES]; | 1099 for (RequestsList::const_iterator it = requests_.begin(); |
1100 it != requests_.end(); ++it) { | |
1101 Request* req = *it; | |
1102 | |
1103 if (req->was_canceled()) | |
1104 continue; | |
1105 | |
1106 DCHECK_EQ(this, req->job()); | |
1107 // Update the net log and notify registered observers. | |
1108 LogFinishRequest(req->source_net_log(), req->request_net_log(), | |
1109 req->info(), net_error_, os_error_); | |
1110 | |
1111 req->OnComplete(net_error_, addrlist); | |
1112 | |
1113 // Check if the resolver was destroyed as a result of running the | |
1114 // callback. If it was, we could continue, but we choose to bail. | |
1115 if (!resolver_) | |
1116 return; | |
1117 } | |
1118 } | |
1119 | |
1120 // Used to call OnJobFinished and RemoveJob. | |
1121 base::WeakPtr<HostResolverImpl> resolver_; | |
1122 | |
1123 Key key_; | |
1124 | |
1125 // Tracks the highest priority across requests_. | |
mmenke
2011/12/22 16:18:49
nit: |requests|
szym
2011/12/28 01:24:10
Done.
| |
1126 PriorityTracker priority_tracker_; | |
1127 | |
1128 bool had_non_speculative_request_; | |
1129 | |
1130 BoundNetLog net_log_; | |
1131 | |
1132 // Store result here in case the job fails fast in Resolve(). | |
1133 int net_error_; | |
1134 int os_error_; | |
1135 | |
1136 // A ProcJob created and started when this Job is dispatched.. | |
1137 scoped_refptr<ProcJob> proc_job_; | |
1138 | |
1139 // All Requests waiting for the result of this Job. Some can be canceled. | |
1140 RequestsList requests_; | |
1141 | |
1142 // A handle used by HostResolverImpl in |dispatcher_|. | |
1143 PriorityDispatch::Handle handle_; | |
1045 }; | 1144 }; |
1046 | 1145 |
1047 //----------------------------------------------------------------------------- | 1146 //----------------------------------------------------------------------------- |
1048 | 1147 |
1148 HostResolverImpl::ProcJobParams::ProcJobParams( | |
1149 HostResolverProc* resolver_proc, | |
1150 size_t max_retry_attempts) | |
1151 : resolver_proc_(resolver_proc), | |
1152 max_retry_attempts_(max_retry_attempts), | |
1153 unresponsive_delay_(base::TimeDelta::FromMilliseconds(6000)), | |
1154 retry_factor_(2) { | |
1155 } | |
1156 | |
1157 HostResolverImpl::ProcJobParams::~ProcJobParams() {} | |
1158 | |
1049 HostResolverImpl::HostResolverImpl( | 1159 HostResolverImpl::HostResolverImpl( |
1050 HostResolverProc* resolver_proc, | |
1051 HostCache* cache, | 1160 HostCache* cache, |
1052 size_t max_jobs, | 1161 const PriorityDispatch::Limits& job_limits, |
1053 size_t max_retry_attempts, | 1162 const ProcJobParams& proc_params, |
1054 NetLog* net_log) | 1163 NetLog* net_log) |
1055 : cache_(cache), | 1164 : cache_(cache), |
1056 max_jobs_(max_jobs), | 1165 dispatch_(job_limits, job_limits.Total() * 100u), |
1057 max_retry_attempts_(max_retry_attempts), | 1166 proc_params_(proc_params), |
1058 unresponsive_delay_(base::TimeDelta::FromMilliseconds(6000)), | |
1059 retry_factor_(2), | |
1060 next_job_id_(0), | |
1061 resolver_proc_(resolver_proc), | |
1062 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), | 1167 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), |
1063 ipv6_probe_monitoring_(false), | 1168 ipv6_probe_monitoring_(false), |
1064 additional_resolver_flags_(0), | 1169 additional_resolver_flags_(0), |
1065 net_log_(net_log) { | 1170 net_log_(net_log) { |
1066 DCHECK_GT(max_jobs, 0u); | 1171 |
1067 | 1172 |
1068 // Maximum of 4 retry attempts for host resolution. | 1173 // Maximum of 4 retry attempts for host resolution. |
1069 static const size_t kDefaultMaxRetryAttempts = 4u; | 1174 static const size_t kDefaultMaxRetryAttempts = 4u; |
1070 | 1175 |
1071 if (max_retry_attempts_ == HostResolver::kDefaultRetryAttempts) | 1176 if (proc_params_.max_retry_attempts_ == HostResolver::kDefaultRetryAttempts) |
1072 max_retry_attempts_ = kDefaultMaxRetryAttempts; | 1177 proc_params_.max_retry_attempts_ = kDefaultMaxRetryAttempts; |
1073 | |
1074 // It is cumbersome to expose all of the constraints in the constructor, | |
1075 // so we choose some defaults, which users can override later. | |
1076 job_pools_[POOL_NORMAL] = new JobPool(max_jobs, 100u * max_jobs); | |
1077 | 1178 |
1078 #if defined(OS_WIN) | 1179 #if defined(OS_WIN) |
1079 EnsureWinsockInit(); | 1180 EnsureWinsockInit(); |
1080 #endif | 1181 #endif |
1081 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 1182 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
1082 if (HaveOnlyLoopbackAddresses()) | 1183 if (HaveOnlyLoopbackAddresses()) |
1083 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; | 1184 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; |
1084 #endif | 1185 #endif |
1085 NetworkChangeNotifier::AddIPAddressObserver(this); | 1186 NetworkChangeNotifier::AddIPAddressObserver(this); |
1086 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | 1187 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
1087 #if !defined(OS_ANDROID) | 1188 #if !defined(OS_ANDROID) |
1088 EnsureDnsReloaderInit(); | 1189 EnsureDnsReloaderInit(); |
1089 #endif | 1190 #endif |
1090 NetworkChangeNotifier::AddDNSObserver(this); | 1191 NetworkChangeNotifier::AddDNSObserver(this); |
1091 #endif | 1192 #endif |
1092 } | 1193 } |
1093 | 1194 |
1094 HostResolverImpl::~HostResolverImpl() { | 1195 HostResolverImpl::~HostResolverImpl() { |
1095 // Cancel the outstanding jobs. Those jobs may contain several attached | 1196 // Cancel the outstanding jobs. Those jobs may contain several attached |
1096 // requests, which will also be cancelled. | 1197 // requests, which will also be canceled. |
1097 DiscardIPv6ProbeJob(); | 1198 DiscardIPv6ProbeJob(); |
1098 | 1199 |
1099 CancelAllJobs(); | 1200 CancelAllJobs(); |
1100 | 1201 |
1101 // In case we are being deleted during the processing of a callback. | |
1102 if (cur_completing_job_) | |
1103 cur_completing_job_->Cancel(); | |
1104 | |
1105 NetworkChangeNotifier::RemoveIPAddressObserver(this); | 1202 NetworkChangeNotifier::RemoveIPAddressObserver(this); |
1106 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | 1203 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
1107 NetworkChangeNotifier::RemoveDNSObserver(this); | 1204 NetworkChangeNotifier::RemoveDNSObserver(this); |
1108 #endif | 1205 #endif |
1109 | |
1110 // Delete the job pools. | |
1111 for (size_t i = 0u; i < arraysize(job_pools_); ++i) | |
1112 delete job_pools_[i]; | |
1113 } | 1206 } |
1114 | 1207 |
1115 void HostResolverImpl::SetPoolConstraints(JobPoolIndex pool_index, | 1208 void HostResolverImpl::SetMaxQueuedJobs(size_t max_queued) { |
1116 size_t max_outstanding_jobs, | 1209 dispatch_.SetMaxQueued(max_queued); |
1117 size_t max_pending_requests) { | |
1118 DCHECK(CalledOnValidThread()); | |
1119 CHECK_GE(pool_index, 0); | |
1120 CHECK_LT(pool_index, POOL_COUNT); | |
1121 CHECK(jobs_.empty()) << "Can only set constraints during setup"; | |
1122 JobPool* pool = job_pools_[pool_index]; | |
1123 pool->SetConstraints(max_outstanding_jobs, max_pending_requests); | |
1124 } | 1210 } |
1125 | 1211 |
1126 int HostResolverImpl::Resolve(const RequestInfo& info, | 1212 int HostResolverImpl::Resolve(const RequestInfo& info, |
1127 AddressList* addresses, | 1213 AddressList* addresses, |
1128 const CompletionCallback& callback, | 1214 const CompletionCallback& callback, |
1129 RequestHandle* out_req, | 1215 RequestHandle* out_req, |
1130 const BoundNetLog& source_net_log) { | 1216 const BoundNetLog& source_net_log) { |
1131 DCHECK(addresses); | 1217 DCHECK(addresses); |
1132 DCHECK(CalledOnValidThread()); | 1218 DCHECK(CalledOnValidThread()); |
1133 DCHECK_EQ(false, callback.is_null()); | 1219 DCHECK_EQ(false, callback.is_null()); |
1134 | 1220 |
1135 // Make a log item for the request. | 1221 // Make a log item for the request. |
1136 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, | 1222 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, |
1137 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST); | 1223 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST); |
1138 | 1224 |
1139 // Update the net log and notify registered observers. | 1225 LogStartRequest(source_net_log, request_net_log, info); |
1140 OnStartRequest(source_net_log, request_net_log, info); | |
1141 | 1226 |
1142 // Build a key that identifies the request in the cache and in the | 1227 // Build a key that identifies the request in the cache and in the |
1143 // outstanding jobs map. | 1228 // outstanding jobs map. |
1144 Key key = GetEffectiveKeyForRequest(info); | 1229 Key key = GetEffectiveKeyForRequest(info); |
1145 | 1230 |
1146 int rv = ResolveHelper(key, info, addresses, request_net_log); | 1231 int rv = ResolveHelper(key, info, addresses, request_net_log); |
1147 if (rv != ERR_DNS_CACHE_MISS) { | 1232 if (rv != ERR_DNS_CACHE_MISS) { |
1148 OnFinishRequest(source_net_log, request_net_log, info, | 1233 LogFinishRequest(source_net_log, request_net_log, info, rv, |
1149 rv, | 1234 0 /* os_error (unknown since from cache) */); |
1150 0 /* os_error (unknown since from cache) */); | |
1151 return rv; | 1235 return rv; |
1152 } | 1236 } |
1153 | 1237 |
1154 // Create a handle for this request, and pass it back to the user if they | 1238 // Next we need to attach our request to a "job". This job is responsible for |
1155 // asked for it (out_req != NULL). | 1239 // calling "getaddrinfo(hostname)" on a worker thread. |
1156 Request* req = new Request(source_net_log, request_net_log, info, | 1240 |
1157 callback, addresses); | 1241 JobMap::iterator jobit = jobs_.find(key); |
1242 Job* job; | |
1243 if (jobit == jobs_.end()) { | |
1244 // Create new Job. | |
1245 job = new Job(this, key, info.priority(), request_net_log); | |
1246 job->handle() = dispatch_.Add(job, job->priority()); | |
1247 int net_error = job->net_error(); | |
1248 if (net_error != ERR_IO_PENDING) { | |
1249 // Early finish caused by eviction. | |
1250 DCHECK(job->was_evicted()); | |
1251 LogFinishRequest(source_net_log, request_net_log, info, net_error, 0); | |
1252 delete job; | |
1253 return net_error; | |
1254 } | |
1255 jobs_.insert(jobit, std::make_pair(key, job)); | |
1256 } else { | |
1257 job = jobit->second; | |
1258 } | |
1259 | |
1260 // Can't complete synchronously. Create and attach request. | |
1261 Request* req = new Request(source_net_log, request_net_log, info, callback, | |
1262 addresses); | |
1263 job->AddRequest(req); | |
1264 if (!job->handle().is_null()) | |
1265 job->handle() = dispatch_.Update(job->handle(), job->priority()); | |
1158 if (out_req) | 1266 if (out_req) |
1159 *out_req = reinterpret_cast<RequestHandle>(req); | 1267 *out_req = reinterpret_cast<RequestHandle>(req); |
1160 | 1268 |
1161 // Next we need to attach our request to a "job". This job is responsible for | 1269 DCHECK_EQ(ERR_IO_PENDING, job->net_error()); |
1162 // calling "getaddrinfo(hostname)" on a worker thread. | 1270 // Completion happens during Job::CompleteRequests(). |
1163 scoped_refptr<Job> job; | |
1164 | |
1165 // If there is already an outstanding job to resolve |key|, use | |
1166 // it. This prevents starting concurrent resolves for the same hostname. | |
1167 job = FindOutstandingJob(key); | |
1168 if (job) { | |
1169 job->AddRequest(req); | |
1170 } else { | |
1171 JobPool* pool = GetPoolForRequest(req); | |
1172 if (CanCreateJobForPool(*pool)) { | |
1173 CreateAndStartJob(req); | |
1174 } else { | |
1175 return EnqueueRequest(pool, req); | |
1176 } | |
1177 } | |
1178 | |
1179 // Completion happens during OnJobComplete(Job*). | |
1180 return ERR_IO_PENDING; | 1271 return ERR_IO_PENDING; |
1181 } | 1272 } |
1182 | 1273 |
1183 int HostResolverImpl::ResolveHelper(const Key& key, | 1274 int HostResolverImpl::ResolveHelper(const Key& key, |
1184 const RequestInfo& info, | 1275 const RequestInfo& info, |
1185 AddressList* addresses, | 1276 AddressList* addresses, |
1186 const BoundNetLog& request_net_log) { | 1277 const BoundNetLog& request_net_log) { |
1187 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. | 1278 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
1188 // On Windows it gives the default interface's address, whereas on Linux it | 1279 // On Windows it gives the default interface's address, whereas on Linux it |
1189 // gives an error. We will make it fail on all platforms for consistency. | 1280 // gives an error. We will make it fail on all platforms for consistency. |
(...skipping 12 matching lines...) Expand all Loading... | |
1202 AddressList* addresses, | 1293 AddressList* addresses, |
1203 const BoundNetLog& source_net_log) { | 1294 const BoundNetLog& source_net_log) { |
1204 DCHECK(CalledOnValidThread()); | 1295 DCHECK(CalledOnValidThread()); |
1205 DCHECK(addresses); | 1296 DCHECK(addresses); |
1206 | 1297 |
1207 // Make a log item for the request. | 1298 // Make a log item for the request. |
1208 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, | 1299 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, |
1209 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST); | 1300 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST); |
1210 | 1301 |
1211 // Update the net log and notify registered observers. | 1302 // Update the net log and notify registered observers. |
1212 OnStartRequest(source_net_log, request_net_log, info); | 1303 LogStartRequest(source_net_log, request_net_log, info); |
1213 | 1304 |
1214 // Build a key that identifies the request in the cache and in the | |
1215 // outstanding jobs map. | |
1216 Key key = GetEffectiveKeyForRequest(info); | 1305 Key key = GetEffectiveKeyForRequest(info); |
1217 | 1306 |
1218 int rv = ResolveHelper(key, info, addresses, request_net_log); | 1307 int rv = ResolveHelper(key, info, addresses, request_net_log); |
1219 OnFinishRequest(source_net_log, request_net_log, info, | 1308 LogFinishRequest(source_net_log, request_net_log, info, rv, |
1220 rv, | 1309 0 /* os_error (unknown since from cache) */); |
1221 0 /* os_error (unknown since from cache) */); | |
1222 return rv; | 1310 return rv; |
1223 } | 1311 } |
1224 | 1312 |
1225 // See OnJobComplete(Job*) for why it is important not to clean out | |
1226 // cancelled requests from Job::requests_. | |
1227 void HostResolverImpl::CancelRequest(RequestHandle req_handle) { | 1313 void HostResolverImpl::CancelRequest(RequestHandle req_handle) { |
1228 DCHECK(CalledOnValidThread()); | 1314 DCHECK(CalledOnValidThread()); |
1229 Request* req = reinterpret_cast<Request*>(req_handle); | 1315 Request* req = reinterpret_cast<Request*>(req_handle); |
1230 DCHECK(req); | 1316 DCHECK(req); |
1231 | 1317 |
1232 scoped_ptr<Request> request_deleter; // Frees at end of function. | 1318 Job* job = req->job(); |
1233 | 1319 DCHECK(job); |
1234 if (!req->job()) { | 1320 job->CancelRequest(req); |
1235 // If the request was not attached to a job yet, it must have been | 1321 if (job->num_active_requests()) { |
1236 // enqueued into a pool. Remove it from that pool's queue. | 1322 if (!job->handle().is_null()) |
1237 // Otherwise if it was attached to a job, the job is responsible for | 1323 job->handle() = dispatch_.Update(job->handle(), job->priority()); |
1238 // deleting it. | |
1239 JobPool* pool = GetPoolForRequest(req); | |
1240 pool->RemovePendingRequest(req); | |
1241 request_deleter.reset(req); | |
1242 } else { | 1324 } else { |
1243 req->request_net_log().EndEvent( | 1325 // Cancel |job|. |
1244 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, NULL); | 1326 PriorityDispatch::Handle handle = job->handle(); |
1327 if (RemoveJob(job)) | |
1328 delete job; | |
1329 if (!handle.is_null()) { | |
1330 dispatch_.Cancel(handle); | |
1331 } else { | |
1332 dispatch_.OnJobFinished(); | |
1333 } | |
1245 } | 1334 } |
1246 | |
1247 // NULL out the fields of req, to mark it as cancelled. | |
1248 req->MarkAsCancelled(); | |
1249 OnCancelRequest(req->source_net_log(), req->request_net_log(), req->info()); | |
1250 } | 1335 } |
1251 | 1336 |
1252 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) { | 1337 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) { |
1253 DCHECK(CalledOnValidThread()); | 1338 DCHECK(CalledOnValidThread()); |
1254 ipv6_probe_monitoring_ = false; | 1339 ipv6_probe_monitoring_ = false; |
1255 DiscardIPv6ProbeJob(); | 1340 DiscardIPv6ProbeJob(); |
1256 default_address_family_ = address_family; | 1341 default_address_family_ = address_family; |
1257 } | 1342 } |
1258 | 1343 |
1259 AddressFamily HostResolverImpl::GetDefaultAddressFamily() const { | 1344 AddressFamily HostResolverImpl::GetDefaultAddressFamily() const { |
(...skipping 18 matching lines...) Expand all Loading... | |
1278 DCHECK(addresses); | 1363 DCHECK(addresses); |
1279 DCHECK(net_error); | 1364 DCHECK(net_error); |
1280 IPAddressNumber ip_number; | 1365 IPAddressNumber ip_number; |
1281 if (!ParseIPLiteralToNumber(key.hostname, &ip_number)) | 1366 if (!ParseIPLiteralToNumber(key.hostname, &ip_number)) |
1282 return false; | 1367 return false; |
1283 | 1368 |
1284 DCHECK_EQ(key.host_resolver_flags & | 1369 DCHECK_EQ(key.host_resolver_flags & |
1285 ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY | | 1370 ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY | |
1286 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6), | 1371 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6), |
1287 0) << " Unhandled flag"; | 1372 0) << " Unhandled flag"; |
1288 bool ipv6_disabled = default_address_family_ == ADDRESS_FAMILY_IPV4 && | 1373 bool ipv6_disabled = (default_address_family_ == ADDRESS_FAMILY_IPV4) && |
1289 !ipv6_probe_monitoring_; | 1374 !ipv6_probe_monitoring_; |
1290 *net_error = OK; | 1375 *net_error = OK; |
1291 if (ip_number.size() == 16 && ipv6_disabled) { | 1376 if ((ip_number.size() == kIPv6AddressSize) && ipv6_disabled) { |
1292 *net_error = ERR_NAME_NOT_RESOLVED; | 1377 *net_error = ERR_NAME_NOT_RESOLVED; |
1293 } else { | 1378 } else { |
1294 *addresses = AddressList::CreateFromIPAddressWithCname( | 1379 *addresses = AddressList::CreateFromIPAddressWithCname( |
1295 ip_number, info.port(), | 1380 ip_number, info.port(), |
1296 (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)); | 1381 (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)); |
1297 } | 1382 } |
1298 return true; | 1383 return true; |
1299 } | 1384 } |
1300 | 1385 |
1301 bool HostResolverImpl::ServeFromCache(const Key& key, | 1386 bool HostResolverImpl::ServeFromCache(const Key& key, |
(...skipping 11 matching lines...) Expand all Loading... | |
1313 if (!cache_entry) | 1398 if (!cache_entry) |
1314 return false; | 1399 return false; |
1315 | 1400 |
1316 request_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT, NULL); | 1401 request_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT, NULL); |
1317 *net_error = cache_entry->error; | 1402 *net_error = cache_entry->error; |
1318 if (*net_error == OK) | 1403 if (*net_error == OK) |
1319 *addresses = CreateAddressListUsingPort(cache_entry->addrlist, info.port()); | 1404 *addresses = CreateAddressListUsingPort(cache_entry->addrlist, info.port()); |
1320 return true; | 1405 return true; |
1321 } | 1406 } |
1322 | 1407 |
1323 void HostResolverImpl::AddOutstandingJob(Job* job) { | 1408 void HostResolverImpl::OnJobFinished(Job* job, const AddressList& addrlist) { |
1324 scoped_refptr<Job>& found_job = jobs_[job->key()]; | 1409 DCHECK(job); |
1325 DCHECK(!found_job); | 1410 // Signal dispatcher that a slot has opened. |
1326 found_job = job; | 1411 dispatch_.OnJobFinished(); |
1327 | 1412 // Write result to the cache. |
1328 JobPool* pool = GetPoolForRequest(job->initial_request()); | 1413 if (cache_.get()) |
1329 pool->AdjustNumOutstandingJobs(1); | 1414 cache_->Set(job->key(), job->net_error(), addrlist, |
1415 base::TimeTicks::Now()); | |
1330 } | 1416 } |
1331 | 1417 |
1332 HostResolverImpl::Job* HostResolverImpl::FindOutstandingJob(const Key& key) { | 1418 bool HostResolverImpl::RemoveJob(Job* job) { |
1333 JobMap::iterator it = jobs_.find(key); | 1419 DCHECK(job); |
1334 if (it != jobs_.end()) | 1420 return jobs_.erase(job->key()) > 0; |
1335 return it->second; | |
1336 return NULL; | |
1337 } | 1421 } |
1338 | 1422 |
1339 void HostResolverImpl::RemoveOutstandingJob(Job* job) { | 1423 // static |
1340 JobMap::iterator it = jobs_.find(job->key()); | 1424 void HostResolverImpl::LogStartRequest(const BoundNetLog& source_net_log, |
1341 DCHECK(it != jobs_.end()); | 1425 const BoundNetLog& request_net_log, |
1342 DCHECK_EQ(it->second.get(), job); | 1426 const RequestInfo& info) { |
1343 jobs_.erase(it); | |
1344 | |
1345 JobPool* pool = GetPoolForRequest(job->initial_request()); | |
1346 pool->AdjustNumOutstandingJobs(-1); | |
1347 } | |
1348 | |
1349 void HostResolverImpl::OnJobComplete(Job* job, | |
1350 int net_error, | |
1351 int os_error, | |
1352 const AddressList& addrlist) { | |
1353 RemoveOutstandingJob(job); | |
1354 | |
1355 // Write result to the cache. | |
1356 if (cache_.get()) | |
1357 cache_->Set(job->key(), net_error, addrlist, base::TimeTicks::Now()); | |
1358 | |
1359 OnJobCompleteInternal(job, net_error, os_error, addrlist); | |
1360 } | |
1361 | |
1362 void HostResolverImpl::AbortJob(Job* job) { | |
1363 OnJobCompleteInternal(job, ERR_ABORTED, 0 /* no os_error */, AddressList()); | |
1364 } | |
1365 | |
1366 void HostResolverImpl::OnJobCompleteInternal( | |
1367 Job* job, | |
1368 int net_error, | |
1369 int os_error, | |
1370 const AddressList& addrlist) { | |
1371 // Make a note that we are executing within OnJobComplete() in case the | |
1372 // HostResolver is deleted by a callback invocation. | |
1373 DCHECK(!cur_completing_job_); | |
1374 cur_completing_job_ = job; | |
1375 | |
1376 // Try to start any queued requests now that a job-slot has freed up. | |
1377 ProcessQueuedRequests(); | |
1378 | |
1379 // Complete all of the requests that were attached to the job. | |
1380 for (RequestsList::const_iterator it = job->requests().begin(); | |
1381 it != job->requests().end(); ++it) { | |
1382 Request* req = *it; | |
1383 if (!req->was_cancelled()) { | |
1384 DCHECK_EQ(job, req->job()); | |
1385 req->request_net_log().EndEvent( | |
1386 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, NULL); | |
1387 | |
1388 // Update the net log and notify registered observers. | |
1389 OnFinishRequest(req->source_net_log(), req->request_net_log(), | |
1390 req->info(), net_error, os_error); | |
1391 | |
1392 req->OnComplete(net_error, addrlist); | |
1393 | |
1394 // Check if the job was cancelled as a result of running the callback. | |
1395 // (Meaning that |this| was deleted). | |
1396 if (job->was_cancelled()) | |
1397 return; | |
1398 } | |
1399 } | |
1400 | |
1401 cur_completing_job_ = NULL; | |
1402 } | |
1403 | |
1404 void HostResolverImpl::OnStartRequest(const BoundNetLog& source_net_log, | |
1405 const BoundNetLog& request_net_log, | |
1406 const RequestInfo& info) { | |
1407 source_net_log.BeginEvent( | 1427 source_net_log.BeginEvent( |
1408 NetLog::TYPE_HOST_RESOLVER_IMPL, | 1428 NetLog::TYPE_HOST_RESOLVER_IMPL, |
1409 make_scoped_refptr(new NetLogSourceParameter( | 1429 make_scoped_refptr(new NetLogSourceParameter( |
1410 "source_dependency", request_net_log.source()))); | 1430 "source_dependency", request_net_log.source()))); |
1411 | 1431 |
1412 request_net_log.BeginEvent( | 1432 request_net_log.BeginEvent( |
1413 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, | 1433 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, |
1414 make_scoped_refptr(new RequestInfoParameters( | 1434 make_scoped_refptr(new RequestInfoParameters( |
1415 info, source_net_log.source()))); | 1435 info, source_net_log.source()))); |
1416 } | 1436 } |
1417 | 1437 |
1418 void HostResolverImpl::OnFinishRequest(const BoundNetLog& source_net_log, | 1438 // static |
1419 const BoundNetLog& request_net_log, | 1439 void HostResolverImpl::LogFinishRequest(const BoundNetLog& source_net_log, |
1420 const RequestInfo& info, | 1440 const BoundNetLog& request_net_log, |
1421 int net_error, | 1441 const RequestInfo& info, |
1422 int os_error) { | 1442 int net_error, |
1423 bool was_resolved = net_error == OK; | 1443 int os_error) { |
1424 | |
1425 // Log some extra parameters on failure for synchronous requests. | |
1426 scoped_refptr<NetLog::EventParameters> params; | 1444 scoped_refptr<NetLog::EventParameters> params; |
1427 if (!was_resolved) { | 1445 if (net_error != OK) { |
1428 params = new HostResolveFailedParams(0, net_error, os_error); | 1446 params = new HostResolveFailedParams(0, net_error, os_error); |
1429 } | 1447 } |
1430 | 1448 |
1431 request_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, params); | 1449 request_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, params); |
1432 source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL, NULL); | 1450 source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL, NULL); |
1433 } | 1451 } |
1434 | 1452 |
1435 void HostResolverImpl::OnCancelRequest(const BoundNetLog& source_net_log, | 1453 // static |
1436 const BoundNetLog& request_net_log, | 1454 void HostResolverImpl::LogCancelRequest(const BoundNetLog& source_net_log, |
1437 const RequestInfo& info) { | 1455 const BoundNetLog& request_net_log, |
1456 const RequestInfo& info) { | |
1438 request_net_log.AddEvent(NetLog::TYPE_CANCELLED, NULL); | 1457 request_net_log.AddEvent(NetLog::TYPE_CANCELLED, NULL); |
1439 request_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, NULL); | 1458 request_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, NULL); |
1440 source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL, NULL); | 1459 source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL, NULL); |
1441 } | 1460 } |
1442 | 1461 |
1443 void HostResolverImpl::DiscardIPv6ProbeJob() { | 1462 void HostResolverImpl::DiscardIPv6ProbeJob() { |
1444 if (ipv6_probe_job_.get()) { | 1463 if (ipv6_probe_job_.get()) { |
1445 ipv6_probe_job_->Cancel(); | 1464 ipv6_probe_job_->Cancel(); |
1446 ipv6_probe_job_ = NULL; | 1465 ipv6_probe_job_ = NULL; |
1447 } | 1466 } |
1448 } | 1467 } |
1449 | 1468 |
1450 void HostResolverImpl::IPv6ProbeSetDefaultAddressFamily( | 1469 void HostResolverImpl::IPv6ProbeSetDefaultAddressFamily( |
1451 AddressFamily address_family) { | 1470 AddressFamily address_family) { |
1452 DCHECK(address_family == ADDRESS_FAMILY_UNSPECIFIED || | 1471 DCHECK(address_family == ADDRESS_FAMILY_UNSPECIFIED || |
1453 address_family == ADDRESS_FAMILY_IPV4); | 1472 address_family == ADDRESS_FAMILY_IPV4); |
1454 if (default_address_family_ != address_family) { | 1473 if (default_address_family_ != address_family) { |
1455 VLOG(1) << "IPv6Probe forced AddressFamily setting to " | 1474 VLOG(1) << "IPv6Probe forced AddressFamily setting to " |
1456 << ((address_family == ADDRESS_FAMILY_UNSPECIFIED) ? | 1475 << ((address_family == ADDRESS_FAMILY_UNSPECIFIED) ? |
1457 "ADDRESS_FAMILY_UNSPECIFIED" : "ADDRESS_FAMILY_IPV4"); | 1476 "ADDRESS_FAMILY_UNSPECIFIED" : "ADDRESS_FAMILY_IPV4"); |
1458 } | 1477 } |
1459 default_address_family_ = address_family; | 1478 default_address_family_ = address_family; |
1460 // Drop reference since the job has called us back. | 1479 // Drop reference since the job has called us back. |
1461 DiscardIPv6ProbeJob(); | 1480 DiscardIPv6ProbeJob(); |
1462 } | 1481 } |
1463 | 1482 |
1464 bool HostResolverImpl::CanCreateJobForPool(const JobPool& pool) const { | |
1465 DCHECK_LE(jobs_.size(), max_jobs_); | |
1466 | |
1467 // We can't create another job if it would exceed the global total. | |
1468 if (jobs_.size() + 1 > max_jobs_) | |
1469 return false; | |
1470 | |
1471 // Check whether the pool's constraints are met. | |
1472 return pool.CanCreateJob(); | |
1473 } | |
1474 | |
1475 // static | |
1476 HostResolverImpl::JobPoolIndex HostResolverImpl::GetJobPoolIndexForRequest( | |
1477 const Request* req) { | |
1478 return POOL_NORMAL; | |
1479 } | |
1480 | |
1481 void HostResolverImpl::ProcessQueuedRequests() { | |
1482 // Find the highest priority request that can be scheduled. | |
1483 Request* top_req = NULL; | |
1484 for (size_t i = 0; i < arraysize(job_pools_); ++i) { | |
1485 JobPool* pool = job_pools_[i]; | |
1486 if (pool->HasPendingRequests() && CanCreateJobForPool(*pool)) { | |
1487 top_req = pool->RemoveTopPendingRequest(); | |
1488 break; | |
1489 } | |
1490 } | |
1491 | |
1492 if (!top_req) | |
1493 return; | |
1494 | |
1495 scoped_refptr<Job> job(CreateAndStartJob(top_req)); | |
1496 | |
1497 // Search for any other pending request which can piggy-back off this job. | |
1498 for (size_t pool_i = 0; pool_i < POOL_COUNT; ++pool_i) { | |
1499 JobPool* pool = job_pools_[pool_i]; | |
1500 pool->MoveRequestsToJob(job); | |
1501 } | |
1502 } | |
1503 | |
1504 HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( | 1483 HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( |
1505 const RequestInfo& info) const { | 1484 const RequestInfo& info) const { |
1506 HostResolverFlags effective_flags = | 1485 HostResolverFlags effective_flags = |
1507 info.host_resolver_flags() | additional_resolver_flags_; | 1486 info.host_resolver_flags() | additional_resolver_flags_; |
1508 AddressFamily effective_address_family = info.address_family(); | 1487 AddressFamily effective_address_family = info.address_family(); |
1509 if (effective_address_family == ADDRESS_FAMILY_UNSPECIFIED && | 1488 if (effective_address_family == ADDRESS_FAMILY_UNSPECIFIED && |
1510 default_address_family_ != ADDRESS_FAMILY_UNSPECIFIED) { | 1489 default_address_family_ != ADDRESS_FAMILY_UNSPECIFIED) { |
1511 effective_address_family = default_address_family_; | 1490 effective_address_family = default_address_family_; |
1512 if (ipv6_probe_monitoring_) | 1491 if (ipv6_probe_monitoring_) |
1513 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; | 1492 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
1514 } | 1493 } |
1515 return Key(info.hostname(), effective_address_family, effective_flags); | 1494 return Key(info.hostname(), effective_address_family, effective_flags); |
1516 } | 1495 } |
1517 | 1496 |
1518 HostResolverImpl::Job* HostResolverImpl::CreateAndStartJob(Request* req) { | |
1519 DCHECK(CanCreateJobForPool(*GetPoolForRequest(req))); | |
1520 Key key = GetEffectiveKeyForRequest(req->info()); | |
1521 | |
1522 req->request_net_log().AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB, | |
1523 NULL); | |
1524 | |
1525 scoped_refptr<Job> job(new Job(next_job_id_++, this, key, | |
1526 req->request_net_log(), net_log_)); | |
1527 job->AddRequest(req); | |
1528 AddOutstandingJob(job); | |
1529 job->Start(); | |
1530 | |
1531 return job.get(); | |
1532 } | |
1533 | |
1534 int HostResolverImpl::EnqueueRequest(JobPool* pool, Request* req) { | |
1535 scoped_ptr<Request> req_evicted_from_queue( | |
1536 pool->InsertPendingRequest(req)); | |
1537 | |
1538 // If the queue has become too large, we need to kick something out. | |
1539 if (req_evicted_from_queue.get()) { | |
1540 Request* r = req_evicted_from_queue.get(); | |
1541 int error = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE; | |
1542 | |
1543 OnFinishRequest(r->source_net_log(), r->request_net_log(), r->info(), error, | |
1544 0 /* os_error (not applicable) */); | |
1545 | |
1546 if (r == req) | |
1547 return error; | |
1548 | |
1549 r->OnComplete(error, AddressList()); | |
1550 } | |
1551 | |
1552 return ERR_IO_PENDING; | |
1553 } | |
1554 | |
1555 void HostResolverImpl::CancelAllJobs() { | 1497 void HostResolverImpl::CancelAllJobs() { |
1556 JobMap jobs; | 1498 STLDeleteValues(&jobs_); |
1557 jobs.swap(jobs_); | |
1558 for (JobMap::iterator it = jobs.begin(); it != jobs.end(); ++it) | |
1559 it->second->Cancel(); | |
1560 } | 1499 } |
1561 | 1500 |
1562 void HostResolverImpl::AbortAllInProgressJobs() { | 1501 void HostResolverImpl::AbortAllInProgressJobs() { |
1563 for (size_t i = 0; i < arraysize(job_pools_); ++i) | 1502 base::WeakPtr<HostResolverImpl> self = AsWeakPtr(); |
1564 job_pools_[i]->ResetNumOutstandingJobs(); | 1503 // Scan |jobs_| for running jobs and abort them. |
1565 JobMap jobs; | 1504 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ) { |
mmenke
2011/12/22 16:18:49
Looks to me like some of this complexity could be
szym
2011/12/22 16:56:57
True, although this would change the current behav
mmenke
2011/12/22 17:34:30
Hmm... Good point. I'm really not fond of the cu
szym
2011/12/28 01:24:10
Done. Job::Abort deletes the job.
| |
1566 jobs.swap(jobs_); | 1505 Job* job = it->second; |
1567 for (JobMap::iterator it = jobs.begin(); it != jobs.end(); ++it) { | 1506 if (!job->is_running()) { |
1568 AbortJob(it->second); | 1507 // Keep it in |dispatch_|. |
1569 it->second->Cancel(); | 1508 DCHECK(!job->handle().is_null()); |
1509 ++it; | |
1510 continue; | |
1511 } | |
1512 job->Abort(); | |
1513 // Check if resolver was deleted in a request callback. | |
1514 if (!self) | |
1515 return; | |
1516 delete job; | |
1517 jobs_.erase(it++); | |
1518 dispatch_.OnJobFinished(); | |
1570 } | 1519 } |
1571 } | 1520 } |
1572 | 1521 |
1573 void HostResolverImpl::OnIPAddressChanged() { | 1522 void HostResolverImpl::OnIPAddressChanged() { |
1574 if (cache_.get()) | 1523 if (cache_.get()) |
1575 cache_->clear(); | 1524 cache_->clear(); |
1576 if (ipv6_probe_monitoring_) { | 1525 if (ipv6_probe_monitoring_) { |
1577 DiscardIPv6ProbeJob(); | 1526 DiscardIPv6ProbeJob(); |
1578 ipv6_probe_job_ = new IPv6ProbeJob(this); | 1527 ipv6_probe_job_ = new IPv6ProbeJob(this); |
1579 ipv6_probe_job_->Start(); | 1528 ipv6_probe_job_->Start(); |
(...skipping 16 matching lines...) Expand all Loading... | |
1596 // resolv.conf changes so we don't need to do anything to clear that cache. | 1545 // resolv.conf changes so we don't need to do anything to clear that cache. |
1597 if (cache_.get()) | 1546 if (cache_.get()) |
1598 cache_->clear(); | 1547 cache_->clear(); |
1599 // Existing jobs will have been sent to the original server so they need to | 1548 // Existing jobs will have been sent to the original server so they need to |
1600 // be aborted. TODO(Craig): Should these jobs be restarted? | 1549 // be aborted. TODO(Craig): Should these jobs be restarted? |
1601 AbortAllInProgressJobs(); | 1550 AbortAllInProgressJobs(); |
1602 // |this| may be deleted inside AbortAllInProgressJobs(). | 1551 // |this| may be deleted inside AbortAllInProgressJobs(). |
1603 } | 1552 } |
1604 | 1553 |
1605 } // namespace net | 1554 } // namespace net |
OLD | NEW |