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

Side by Side Diff: content/common/net/url_fetcher_core.cc

Issue 10203002: Make URLRequestThrottlerManager a member of URLRequestContext. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make throttler_manager() optional. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 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 "content/common/net/url_fetcher_core.h" 5 #include "content/common/net/url_fetcher_core.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util_proxy.h" 8 #include "base/file_util_proxy.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop_proxy.h" 10 #include "base/message_loop_proxy.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/tracked_objects.h" 13 #include "base/tracked_objects.h"
14 #include "content/public/common/url_fetcher_delegate.h" 14 #include "content/public/common/url_fetcher_delegate.h"
15 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
16 #include "net/base/load_flags.h" 16 #include "net/base/load_flags.h"
17 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
18 #include "net/http/http_response_headers.h" 18 #include "net/http/http_response_headers.h"
19 #include "net/url_request/url_request_context.h"
19 #include "net/url_request/url_request_context_getter.h" 20 #include "net/url_request/url_request_context_getter.h"
20 #include "net/url_request/url_request_throttler_manager.h" 21 #include "net/url_request/url_request_throttler_manager.h"
21 22
22 namespace content { 23 namespace content {
23 24
24 static const int kBufferSize = 4096; 25 static const int kBufferSize = 4096;
25 static const int kUploadProgressTimerInterval = 100; 26 static const int kUploadProgressTimerInterval = 100;
26 27
27 URLFetcherCore::Registry::Registry() {} 28 URLFetcherCore::Registry::Registry() {}
28 URLFetcherCore::Registry::~Registry() {} 29 URLFetcherCore::Registry::~Registry() {}
(...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 } 600 }
600 return write_complete; 601 return write_complete;
601 } 602 }
602 603
603 void URLFetcherCore::OnReadCompleted(net::URLRequest* request, 604 void URLFetcherCore::OnReadCompleted(net::URLRequest* request,
604 int bytes_read) { 605 int bytes_read) {
605 DCHECK(request == request_); 606 DCHECK(request == request_);
606 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); 607 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
607 608
608 url_ = request->url(); 609 url_ = request->url();
609 url_throttler_entry_ = 610 net::URLRequestThrottlerManager* throttler_manager =
610 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); 611 request->context()->throttler_manager();
612 if (throttler_manager) {
613 url_throttler_entry_ = throttler_manager->RegisterRequestUrl(url_);
614 }
611 615
612 bool waiting_on_write = false; 616 bool waiting_on_write = false;
613 do { 617 do {
614 if (!request_->status().is_success() || bytes_read <= 0) 618 if (!request_->status().is_success() || bytes_read <= 0)
615 break; 619 break;
616 620
617 current_response_bytes_ += bytes_read; 621 current_response_bytes_ += bytes_read;
618 InformDelegateDownloadProgress(); 622 InformDelegateDownloadProgress();
619 InformDelegateDownloadDataIfNecessary(bytes_read); 623 InformDelegateDownloadDataIfNecessary(bytes_read);
620 624
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); 657 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
654 base::TimeDelta backoff_delay; 658 base::TimeDelta backoff_delay;
655 659
656 // Checks the response from server. 660 // Checks the response from server.
657 if (response_code_ >= 500 || 661 if (response_code_ >= 500 ||
658 status_.error() == net::ERR_TEMPORARILY_THROTTLED) { 662 status_.error() == net::ERR_TEMPORARILY_THROTTLED) {
659 // When encountering a server error, we will send the request again 663 // When encountering a server error, we will send the request again
660 // after backoff time. 664 // after backoff time.
661 ++num_retries_; 665 ++num_retries_;
662 666
663 // Note that backoff_delay may be 0 because (a) the URLRequestThrottler 667 // Note that backoff_delay may be 0 because (a) the
664 // code does not necessarily back off on the first error, and (b) it 668 // URLRequestThrottler code does not necessarily back off on the
willchan no longer on Chromium 2012/05/02 16:40:07 URLRequestThrottlerManager?
Jói 2012/05/02 16:45:28 Changed to "URLRequestThrottlerManager and related
665 // only backs off on some of the 5xx status codes. 669 // first error, (b) it only backs off on some of the 5xx status
670 // codes, (c) not all URLRequestContexts have a throttler manager.
666 base::TimeTicks backoff_release_time = GetBackoffReleaseTime(); 671 base::TimeTicks backoff_release_time = GetBackoffReleaseTime();
667 backoff_delay = backoff_release_time - base::TimeTicks::Now(); 672 backoff_delay = backoff_release_time - base::TimeTicks::Now();
668 if (backoff_delay < base::TimeDelta()) 673 if (backoff_delay < base::TimeDelta())
669 backoff_delay = base::TimeDelta(); 674 backoff_delay = base::TimeDelta();
670 675
671 if (automatically_retry_on_5xx_ && num_retries_ <= max_retries_) { 676 if (automatically_retry_on_5xx_ && num_retries_ <= max_retries_) {
672 StartOnIOThread(); 677 StartOnIOThread();
673 return; 678 return;
674 } 679 }
675 } else { 680 } else {
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 792
788 request_->Start(); 793 request_->Start();
789 } 794 }
790 795
791 void URLFetcherCore::StartURLRequestWhenAppropriate() { 796 void URLFetcherCore::StartURLRequestWhenAppropriate() {
792 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); 797 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
793 798
794 if (was_cancelled_) 799 if (was_cancelled_)
795 return; 800 return;
796 801
802 DCHECK(request_context_getter_);
803
804 int64 delay = 0LL;
797 if (original_url_throttler_entry_ == NULL) { 805 if (original_url_throttler_entry_ == NULL) {
798 original_url_throttler_entry_ = 806 net::URLRequestThrottlerManager* manager =
799 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl( 807 request_context_getter_->GetURLRequestContext()->throttler_manager();
800 original_url_); 808 if (manager) {
809 original_url_throttler_entry_ =
810 manager->RegisterRequestUrl(original_url_);
811 }
812 }
813 if (original_url_throttler_entry_ != NULL) {
814 delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest(
815 GetBackoffReleaseTime());
801 } 816 }
802 817
803 int64 delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest(
804 GetBackoffReleaseTime());
805 if (delay == 0) { 818 if (delay == 0) {
806 StartURLRequest(); 819 StartURLRequest();
807 } else { 820 } else {
808 MessageLoop::current()->PostDelayedTask( 821 MessageLoop::current()->PostDelayedTask(
809 FROM_HERE, base::Bind(&URLFetcherCore::StartURLRequest, this), 822 FROM_HERE, base::Bind(&URLFetcherCore::StartURLRequest, this),
810 base::TimeDelta::FromMilliseconds(delay)); 823 base::TimeDelta::FromMilliseconds(delay));
811 } 824 }
812 } 825 }
813 826
814 void URLFetcherCore::CancelURLRequest() { 827 void URLFetcherCore::CancelURLRequest() {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
925 } 938 }
926 939
927 void URLFetcherCore::ReleaseRequest() { 940 void URLFetcherCore::ReleaseRequest() {
928 upload_progress_checker_timer_.reset(); 941 upload_progress_checker_timer_.reset();
929 request_.reset(); 942 request_.reset();
930 g_registry.Get().RemoveURLFetcherCore(this); 943 g_registry.Get().RemoveURLFetcherCore(this);
931 } 944 }
932 945
933 base::TimeTicks URLFetcherCore::GetBackoffReleaseTime() { 946 base::TimeTicks URLFetcherCore::GetBackoffReleaseTime() {
934 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); 947 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
935 DCHECK(original_url_throttler_entry_ != NULL);
936 948
937 base::TimeTicks original_url_backoff = 949 if (original_url_throttler_entry_) {
938 original_url_throttler_entry_->GetExponentialBackoffReleaseTime(); 950 base::TimeTicks original_url_backoff =
939 base::TimeTicks destination_url_backoff; 951 original_url_throttler_entry_->GetExponentialBackoffReleaseTime();
940 if (url_throttler_entry_ != NULL && 952 base::TimeTicks destination_url_backoff;
941 original_url_throttler_entry_ != url_throttler_entry_) { 953 if (url_throttler_entry_ != NULL &&
942 destination_url_backoff = 954 original_url_throttler_entry_ != url_throttler_entry_) {
943 url_throttler_entry_->GetExponentialBackoffReleaseTime(); 955 destination_url_backoff =
956 url_throttler_entry_->GetExponentialBackoffReleaseTime();
957 }
958
959 return original_url_backoff > destination_url_backoff ?
960 original_url_backoff : destination_url_backoff;
961 } else {
962 return base::TimeTicks();
944 } 963 }
945
946 return original_url_backoff > destination_url_backoff ?
947 original_url_backoff : destination_url_backoff;
948 } 964 }
949 965
950 } // namespace content 966 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698