OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/http/http_server_properties_impl.h" | 5 #include "net/http/http_server_properties_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 return; | 313 return; |
314 } | 314 } |
315 // This server's alternate protocol information is coming from a canonical | 315 // This server's alternate protocol information is coming from a canonical |
316 // server. Add an entry in the map for this server explicitly so that | 316 // server. Add an entry in the map for this server explicitly so that |
317 // it can be marked as broken. | 317 // it can be marked as broken. |
318 it = alternate_protocol_map_.Put(server, alternate); | 318 it = alternate_protocol_map_.Put(server, alternate); |
319 } | 319 } |
320 it->second.is_broken = true; | 320 it->second.is_broken = true; |
321 const AlternativeService alternative_service(alternate.protocol, | 321 const AlternativeService alternative_service(alternate.protocol, |
322 server.host(), alternate.port); | 322 server.host(), alternate.port); |
323 int count = ++broken_alternate_protocol_map_[alternative_service]; | 323 int count = ++recently_broken_alternative_services_[alternative_service]; |
324 base::TimeDelta delay = | 324 base::TimeDelta delay = |
325 base::TimeDelta::FromSeconds(kBrokenAlternateProtocolDelaySecs); | 325 base::TimeDelta::FromSeconds(kBrokenAlternateProtocolDelaySecs); |
326 base::TimeTicks when = base::TimeTicks::Now() + delay * (1 << (count - 1)); | 326 base::TimeTicks when = base::TimeTicks::Now() + delay * (1 << (count - 1)); |
327 broken_alternate_protocol_list_.push_back( | 327 auto result = broken_alternative_services_.insert( |
328 BrokenAlternateProtocolEntryWithTime(alternative_service, when)); | 328 std::make_pair(alternative_service, when)); |
329 // If alternative service is already in expiration queue, | |
Ryan Hamilton
2015/03/09 19:48:27
Since checking membership is constant time, how 'b
Bence
2015/03/09 21:24:35
Done.
| |
330 // then remove it, and enqueue with new expiration time. | |
331 if (!result.second) { | |
332 broken_alternative_services_.erase(result.first); | |
333 ignore_result(broken_alternative_services_.insert( | |
334 std::make_pair(alternative_service, when))); | |
335 return; | |
336 } | |
329 | 337 |
330 // Do not leave this host as canonical so that we don't infer the other | 338 // Do not leave this host as canonical so that we don't infer the other |
331 // hosts are also broken without testing them first. | 339 // hosts are also broken without testing them first. |
332 RemoveCanonicalHost(server); | 340 RemoveCanonicalHost(server); |
333 | 341 |
334 // If this is the only entry in the list, schedule an expiration task. | 342 // If this is the only entry in the list, schedule an expiration task. |
335 // Otherwise it will be rescheduled automatically when the pending task runs. | 343 // Otherwise it will be rescheduled automatically when the pending task runs. |
336 if (broken_alternate_protocol_list_.size() == 1) { | 344 if (broken_alternative_services_.size() == 1) { |
337 ScheduleBrokenAlternateProtocolMappingsExpiration(); | 345 ScheduleBrokenAlternateProtocolMappingsExpiration(); |
338 } | 346 } |
339 } | 347 } |
340 | 348 |
341 bool HttpServerPropertiesImpl::WasAlternateProtocolRecentlyBroken( | 349 bool HttpServerPropertiesImpl::WasAlternateProtocolRecentlyBroken( |
342 const HostPortPair& server) { | 350 const HostPortPair& server) { |
343 const AlternateProtocolInfo alternate_protocol = GetAlternateProtocol(server); | 351 const AlternateProtocolInfo alternate_protocol = GetAlternateProtocol(server); |
344 if (alternate_protocol.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) | 352 if (alternate_protocol.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) |
345 return false; | 353 return false; |
346 const AlternativeService alternative_service( | 354 const AlternativeService alternative_service( |
347 alternate_protocol.protocol, server.host(), alternate_protocol.port); | 355 alternate_protocol.protocol, server.host(), alternate_protocol.port); |
348 return ContainsKey(broken_alternate_protocol_map_, alternative_service); | 356 return ContainsKey(recently_broken_alternative_services_, |
357 alternative_service); | |
349 } | 358 } |
350 | 359 |
351 void HttpServerPropertiesImpl::ConfirmAlternateProtocol( | 360 void HttpServerPropertiesImpl::ConfirmAlternateProtocol( |
352 const HostPortPair& server) { | 361 const HostPortPair& server) { |
353 const AlternateProtocolInfo alternate_protocol = GetAlternateProtocol(server); | 362 const AlternateProtocolInfo alternate_protocol = GetAlternateProtocol(server); |
354 if (alternate_protocol.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) | 363 if (alternate_protocol.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) |
355 return; | 364 return; |
356 const AlternativeService alternative_service( | 365 const AlternativeService alternative_service( |
357 alternate_protocol.protocol, server.host(), alternate_protocol.port); | 366 alternate_protocol.protocol, server.host(), alternate_protocol.port); |
358 broken_alternate_protocol_map_.erase(alternative_service); | 367 recently_broken_alternative_services_.erase(alternative_service); |
359 } | 368 } |
360 | 369 |
361 void HttpServerPropertiesImpl::ClearAlternateProtocol( | 370 void HttpServerPropertiesImpl::ClearAlternateProtocol( |
362 const HostPortPair& server) { | 371 const HostPortPair& server) { |
363 AlternateProtocolMap::iterator it = alternate_protocol_map_.Peek(server); | 372 AlternateProtocolMap::iterator it = alternate_protocol_map_.Peek(server); |
364 if (it != alternate_protocol_map_.end()) | 373 if (it != alternate_protocol_map_.end()) |
365 alternate_protocol_map_.Erase(it); | 374 alternate_protocol_map_.Erase(it); |
366 | 375 |
367 RemoveCanonicalHost(server); | 376 RemoveCanonicalHost(server); |
368 } | 377 } |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
497 return; | 506 return; |
498 | 507 |
499 if (!canonical->second.Equals(server)) | 508 if (!canonical->second.Equals(server)) |
500 return; | 509 return; |
501 | 510 |
502 canonical_host_to_origin_map_.erase(canonical->first); | 511 canonical_host_to_origin_map_.erase(canonical->first); |
503 } | 512 } |
504 | 513 |
505 void HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings() { | 514 void HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings() { |
506 base::TimeTicks now = base::TimeTicks::Now(); | 515 base::TimeTicks now = base::TimeTicks::Now(); |
507 while (!broken_alternate_protocol_list_.empty()) { | 516 while (!broken_alternative_services_.empty()) { |
508 BrokenAlternateProtocolEntryWithTime entry_with_time = | 517 BrokenAlternativeServices::iterator it = |
509 broken_alternate_protocol_list_.front(); | 518 broken_alternative_services_.begin(); |
510 if (now < entry_with_time.when) { | 519 if (now < it->second) { |
511 break; | 520 break; |
512 } | 521 } |
513 | 522 |
514 const AlternativeService& alternative_service = | 523 const AlternativeService& alternative_service = it->first; |
515 entry_with_time.alternative_service; | |
516 ClearAlternateProtocol( | 524 ClearAlternateProtocol( |
517 HostPortPair(alternative_service.host, alternative_service.port)); | 525 HostPortPair(alternative_service.host, alternative_service.port)); |
518 broken_alternate_protocol_list_.pop_front(); | 526 broken_alternative_services_.erase(it); |
519 } | 527 } |
520 ScheduleBrokenAlternateProtocolMappingsExpiration(); | 528 ScheduleBrokenAlternateProtocolMappingsExpiration(); |
521 } | 529 } |
522 | 530 |
523 void | 531 void |
524 HttpServerPropertiesImpl::ScheduleBrokenAlternateProtocolMappingsExpiration() { | 532 HttpServerPropertiesImpl::ScheduleBrokenAlternateProtocolMappingsExpiration() { |
525 if (broken_alternate_protocol_list_.empty()) { | 533 if (broken_alternative_services_.empty()) { |
526 return; | 534 return; |
527 } | 535 } |
528 base::TimeTicks now = base::TimeTicks::Now(); | 536 base::TimeTicks now = base::TimeTicks::Now(); |
529 base::TimeTicks when = broken_alternate_protocol_list_.front().when; | 537 base::TimeTicks when = broken_alternative_services_.front().second; |
530 base::TimeDelta delay = when > now ? when - now : base::TimeDelta(); | 538 base::TimeDelta delay = when > now ? when - now : base::TimeDelta(); |
531 base::MessageLoop::current()->PostDelayedTask( | 539 base::MessageLoop::current()->PostDelayedTask( |
532 FROM_HERE, | 540 FROM_HERE, |
533 base::Bind( | 541 base::Bind( |
534 &HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings, | 542 &HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings, |
535 weak_ptr_factory_.GetWeakPtr()), | 543 weak_ptr_factory_.GetWeakPtr()), |
536 delay); | 544 delay); |
537 } | 545 } |
538 | 546 |
539 } // namespace net | 547 } // namespace net |
OLD | NEW |