Index: components/cronet/stale_host_resolver.h |
diff --git a/components/cronet/stale_host_resolver.h b/components/cronet/stale_host_resolver.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5b939ef8ef12883fa8439adb95407a5804037a46 |
--- /dev/null |
+++ b/components/cronet/stale_host_resolver.h |
@@ -0,0 +1,98 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_CRONET_STALE_HOST_RESOLVER_H_ |
+#define COMPONENTS_CRONET_STALE_HOST_RESOLVER_H_ |
+ |
+#include <set> |
+ |
+#include "net/dns/host_resolver.h" |
+#include "net/dns/host_resolver_impl.h" |
+ |
+namespace cronet { |
+ |
+// A HostResolver that wraps another HostResolver that uses it to make requests |
Randy Smith (Not in Mondays)
2016/06/08 19:20:03
nit: We're no longer wrapping an arbitrary HostRes
Julia Tuttle
2016/06/10 17:31:44
Done.
|
+// but "impatiently" returns stale data (if available and usable) after a delay, |
+// to reduce DNS latency at the expense of accuracy. |
+class NET_EXPORT StaleHostResolver : public net::HostResolver { |
+ public: |
+ struct NET_EXPORT StaleOptions { |
+ // How long to wait before returning stale data, if available. |
+ base::TimeDelta delay; |
+ |
+ // If positive, how long stale data can be past the expiration time before |
+ // it's considered unusable. If zero or negative, stale data can be used |
+ // indefinitely. |
+ base::TimeDelta max_expired_time; |
+ |
+ // If set, stale data from previous networks is usable; if clear, it's not. |
+ // |
+ // If the other network had a working, correct DNS setup, this can increase |
+ // the availability of useful stale results. |
+ // |
+ // If the other network had a broken (e.g. hijacked for captive portal) DNS |
+ // setup, this will instead end up returning useless results. |
+ bool allow_other_network; |
+ |
+ // If positive, the maximum number of times a stale entry can be used. If |
+ // zero, there is no limit. |
+ int max_stale_uses; |
+ }; |
+ |
+ // Creates a StaleHostResolver that uses |inner_resolver| for actual |
+ // resolution, but potentially returns stale data according to |
+ // |stale_options|. |
+ StaleHostResolver(std::unique_ptr<net::HostResolverImpl> inner_resolver, |
+ const StaleOptions& stale_options); |
+ |
+ // Cancels all pending calls to Resolve, and does not call their callbacks. |
+ ~StaleHostResolver() override; |
+ |
+ // Resolves as a regular HostResolver, but if stale data is available and |
+ // usable (according to the options passed to the constructor), and fresh data |
+ // is not returned before the specified delay, returns the stale data instead. |
+ // |
+ // If stale data is returned, the StaleHostResolver allows the underlying |
+ // request to continue in order to repopulate the cache. |
Randy Smith (Not in Mondays)
2016/06/08 19:20:03
I'm a bit torn about all the commenting on the met
Julia Tuttle
2016/06/10 17:31:44
Done.
|
+ // |
+ // |addresses| must remain valid until the request completes (synchronously or |
+ // via |callback|) or is canceled (via |CancelRequest| or |
+ // |~StaleHostResolver()|). |
Randy Smith (Not in Mondays)
2016/06/08 19:20:03
This is actually a comment on the interface, not t
Randy Smith (Not in Mondays)
2016/06/08 19:20:03
nit: Looks like line break in the wrong place?
Julia Tuttle
2016/06/10 17:31:44
Done.
Julia Tuttle
2016/06/10 17:31:45
Done.
|
+ int Resolve(const RequestInfo& info, |
+ net::RequestPriority priority, |
+ net::AddressList* addresses, |
+ const net::CompletionCallback& callback, |
+ RequestHandle* out_req, |
+ const net::BoundNetLog& net_log) override; |
+ |
+ // Cancels a request created by |Resolve|. |
+ // |
+ // It is not valid to cancel a request once it has returned a result, even if |
+ // the result was stale data and the underlying request on the inner resolver |
+ // is still running. |
Randy Smith (Not in Mondays)
2016/06/08 19:20:03
This seems like a comment that applies to the inte
Julia Tuttle
2016/06/10 17:31:45
Done.
|
+ void CancelRequest(RequestHandle req) override; |
+ |
+ // The remaining public methods pass through to the inner resolver: |
+ int ResolveFromCache(const RequestInfo& info, |
+ net::AddressList* addresses, |
+ const net::BoundNetLog& net_log) override; |
+ void SetDnsClientEnabled(bool enabled) override; |
+ net::HostCache* GetHostCache() override; |
+ std::unique_ptr<base::Value> GetDnsConfigAsValue() const override; |
+ |
+ private: |
+ class Request; |
+ |
+ void OnRequestComplete(Request* request, |
+ const net::CompletionCallback& outer_callback, |
+ int error); |
+ |
+ std::unique_ptr<net::HostResolverImpl> resolver_; |
+ StaleOptions options_; |
+ std::set<Request*> pending_requests_; |
Randy Smith (Not in Mondays)
2016/06/08 19:20:03
These are owned by this class, right? Would std::
Julia Tuttle
2016/06/10 17:31:45
It wouldn't be ideal; Requests delete themselves w
|
+}; |
+ |
+} // namespace cronet |
+ |
+#endif // COMPONENTS_CRONET_STALE_HOST_RESOLVER_H_ |