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

Side by Side Diff: net/http/http_server_properties_manager.cc

Issue 2171743002: Do not persist HttpServerProperties to disk that often. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Re: #7. Created 4 years, 5 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_manager.h" 5 #include "net/http/http_server_properties_manager.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "net/base/ip_address.h" 17 #include "net/base/ip_address.h"
18 #include "net/base/port_util.h" 18 #include "net/base/port_util.h"
19 #include "url/gurl.h" 19 #include "url/gurl.h"
20 20
21 namespace net { 21 namespace net {
22 22
23 namespace { 23 namespace {
24 24
25 // Time to wait before starting an update the http_server_properties_impl_ cache 25 // Time to wait before starting an update the http_server_properties_impl_ cache
26 // from preferences. Scheduling another update during this period will reset the 26 // from preferences. Scheduling another update during this period will reset the
27 // timer. 27 // timer.
28 const int64_t kUpdateCacheDelayMs = 1000; 28 const int64_t kUpdateCacheDelayMs = 1000; // 1 second
29 29
30 // Time to wait before starting an update the preferences from the 30 // Time to wait before starting an update the preferences from the
31 // http_server_properties_impl_ cache. Scheduling another update during this 31 // http_server_properties_impl_ cache. Scheduling another update during this
32 // period will reset the timer. 32 // period will reset the timer.
33 const int64_t kUpdatePrefsDelayMs = 60000; 33 const int64_t kUpdatePrefsDelayMs = 60000; // 1 minute
34
35 // Maximum time to wait between ScheduleUpdatePrefsOnNetworkThread() calls from
36 // SetAlternativeService() and SetAlternativeServices().
37 const int64_t kAlternativeServicesUpdatePrefsMs = 300000; // 5 minutes
34 38
35 // "version" 0 indicates, http_server_properties doesn't have "version" 39 // "version" 0 indicates, http_server_properties doesn't have "version"
36 // property. 40 // property.
37 const int kMissingVersion = 0; 41 const int kMissingVersion = 0;
38 42
39 // The version number of persisted http_server_properties. 43 // The version number of persisted http_server_properties.
40 const int kVersionNumber = 5; 44 const int kVersionNumber = 5;
41 45
42 // Persist 200 MRU AlternateProtocolHostPortPairs. 46 // Persist 200 MRU AlternateProtocolHostPortPairs.
43 const int kMaxAlternateProtocolHostsToPersist = 200; 47 const int kMaxAlternateProtocolHostsToPersist = 200;
(...skipping 30 matching lines...) Expand all
74 // HttpServerPropertiesManager 78 // HttpServerPropertiesManager
75 79
76 HttpServerPropertiesManager::PrefDelegate::~PrefDelegate() {} 80 HttpServerPropertiesManager::PrefDelegate::~PrefDelegate() {}
77 81
78 HttpServerPropertiesManager::HttpServerPropertiesManager( 82 HttpServerPropertiesManager::HttpServerPropertiesManager(
79 PrefDelegate* pref_delegate, 83 PrefDelegate* pref_delegate,
80 scoped_refptr<base::SequencedTaskRunner> network_task_runner) 84 scoped_refptr<base::SequencedTaskRunner> network_task_runner)
81 : pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), 85 : pref_task_runner_(base::ThreadTaskRunnerHandle::Get()),
82 pref_delegate_(pref_delegate), 86 pref_delegate_(pref_delegate),
83 setting_prefs_(false), 87 setting_prefs_(false),
84 network_task_runner_(network_task_runner) { 88 network_task_runner_(network_task_runner),
89 last_update_prefs_from_cache_(
90 base::Time::Now() -
91 base::TimeDelta::FromSeconds(kAlternativeServicesUpdatePrefsMs)) {
Ryan Hamilton 2016/07/22 23:31:35 Should this be "FromMilliseconds()"? (Here and els
85 DCHECK(pref_delegate_); 92 DCHECK(pref_delegate_);
86 pref_weak_ptr_factory_.reset( 93 pref_weak_ptr_factory_.reset(
87 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); 94 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
88 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); 95 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr();
89 pref_cache_update_timer_.reset(new base::OneShotTimer); 96 pref_cache_update_timer_.reset(new base::OneShotTimer);
90 pref_delegate_->StartListeningForUpdates( 97 pref_delegate_->StartListeningForUpdates(
91 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, 98 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged,
92 base::Unretained(this))); 99 base::Unretained(this)));
93 } 100 }
94 101
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 const url::SchemeHostPort& origin) { 195 const url::SchemeHostPort& origin) {
189 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 196 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
190 return http_server_properties_impl_->GetAlternativeServices(origin); 197 return http_server_properties_impl_->GetAlternativeServices(origin);
191 } 198 }
192 199
193 bool HttpServerPropertiesManager::SetAlternativeService( 200 bool HttpServerPropertiesManager::SetAlternativeService(
194 const url::SchemeHostPort& origin, 201 const url::SchemeHostPort& origin,
195 const AlternativeService& alternative_service, 202 const AlternativeService& alternative_service,
196 base::Time expiration) { 203 base::Time expiration) {
197 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 204 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
198 const bool changed = http_server_properties_impl_->SetAlternativeService( 205 const bool changed =
199 origin, alternative_service, expiration); 206 http_server_properties_impl_->SetAlternativeService(
207 origin, alternative_service, expiration) ||
208 (last_update_prefs_from_cache_ +
209 base::TimeDelta::FromSeconds(kAlternativeServicesUpdatePrefsMs) <
210 base::Time::Now());
200 if (changed) { 211 if (changed) {
201 ScheduleUpdatePrefsOnNetworkThread(SET_ALTERNATIVE_SERVICES); 212 ScheduleUpdatePrefsOnNetworkThread(SET_ALTERNATIVE_SERVICES);
202 } 213 }
203 return changed; 214 return changed;
204 } 215 }
205 216
206 bool HttpServerPropertiesManager::SetAlternativeServices( 217 bool HttpServerPropertiesManager::SetAlternativeServices(
207 const url::SchemeHostPort& origin, 218 const url::SchemeHostPort& origin,
208 const AlternativeServiceInfoVector& alternative_service_info_vector) { 219 const AlternativeServiceInfoVector& alternative_service_info_vector) {
209 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 220 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
210 const bool changed = http_server_properties_impl_->SetAlternativeServices( 221 const bool changed =
211 origin, alternative_service_info_vector); 222 http_server_properties_impl_->SetAlternativeServices(
223 origin, alternative_service_info_vector) ||
224 (last_update_prefs_from_cache_ +
225 base::TimeDelta::FromSeconds(kAlternativeServicesUpdatePrefsMs) <
226 base::Time::Now());
212 if (changed) { 227 if (changed) {
213 ScheduleUpdatePrefsOnNetworkThread(SET_ALTERNATIVE_SERVICES); 228 ScheduleUpdatePrefsOnNetworkThread(SET_ALTERNATIVE_SERVICES);
214 } 229 }
215 return changed; 230 return changed;
216 } 231 }
217 232
218 void HttpServerPropertiesManager::MarkAlternativeServiceBroken( 233 void HttpServerPropertiesManager::MarkAlternativeServiceBroken(
219 const AlternativeService& alternative_service) { 234 const AlternativeService& alternative_service) {
220 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 235 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
221 http_server_properties_impl_->MarkAlternativeServiceBroken( 236 http_server_properties_impl_->MarkAlternativeServiceBroken(
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 } 904 }
890 905
891 // This is required so we can set this as the callback for a timer. 906 // This is required so we can set this as the callback for a timer.
892 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread() { 907 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread() {
893 UpdatePrefsFromCacheOnNetworkThread(base::Closure()); 908 UpdatePrefsFromCacheOnNetworkThread(base::Closure());
894 } 909 }
895 910
896 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread( 911 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread(
897 const base::Closure& completion) { 912 const base::Closure& completion) {
898 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 913 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
914 last_update_prefs_from_cache_ = base::Time::Now();
899 915
900 // It is in MRU order. 916 // It is in MRU order.
901 base::ListValue* spdy_server_list = new base::ListValue; 917 base::ListValue* spdy_server_list = new base::ListValue;
902 http_server_properties_impl_->GetSpdyServerList( 918 http_server_properties_impl_->GetSpdyServerList(
903 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist); 919 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist);
904 920
905 SpdySettingsMap* spdy_settings_map = 921 SpdySettingsMap* spdy_settings_map =
906 new SpdySettingsMap(kMaxSpdySettingsHostsToPersist); 922 new SpdySettingsMap(kMaxSpdySettingsHostsToPersist);
907 const SpdySettingsMap& main_map = 923 const SpdySettingsMap& main_map =
908 http_server_properties_impl_->spdy_settings_map(); 924 http_server_properties_impl_->spdy_settings_map();
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
1249 quic_servers_dict); 1265 quic_servers_dict);
1250 } 1266 }
1251 1267
1252 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { 1268 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() {
1253 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); 1269 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
1254 if (!setting_prefs_) 1270 if (!setting_prefs_)
1255 ScheduleUpdateCacheOnPrefThread(); 1271 ScheduleUpdateCacheOnPrefThread();
1256 } 1272 }
1257 1273
1258 } // namespace net 1274 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698