broken_alternative_services.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright (c) 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_HTTP_BROKEN_ALTERNATIVE_SERVICES_H_
  5. #define NET_HTTP_BROKEN_ALTERNATIVE_SERVICES_H_
  6. #include <list>
  7. #include <set>
  8. #include "base/containers/lru_cache.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/time/time.h"
  12. #include "base/timer/timer.h"
  13. #include "net/base/network_isolation_key.h"
  14. #include "net/http/alternative_service.h"
  15. namespace base {
  16. class TickClock;
  17. }
  18. namespace net {
  19. // Contains information about a broken alternative service, and the context in
  20. // which it's known to be broken.
  21. struct NET_EXPORT_PRIVATE BrokenAlternativeService {
  22. // If |use_network_isolation_key| is false, |network_isolation_key| is
  23. // ignored, and an empty NetworkIsolationKey is used instead.
  24. BrokenAlternativeService(const AlternativeService& alternative_service,
  25. const NetworkIsolationKey& network_isolation_key,
  26. bool use_network_isolation_key);
  27. ~BrokenAlternativeService();
  28. bool operator<(const BrokenAlternativeService& other) const;
  29. AlternativeService alternative_service;
  30. // The context in which the alternative service is known to be broken in. Used
  31. // to avoid cross-NetworkIsolationKey communication.
  32. NetworkIsolationKey network_isolation_key;
  33. };
  34. // Stores broken alternative services and when their brokenness expires.
  35. typedef std::list<std::pair<BrokenAlternativeService, base::TimeTicks>>
  36. BrokenAlternativeServiceList;
  37. // Stores how many times an alternative service has been marked broken.
  38. class RecentlyBrokenAlternativeServices
  39. : public base::LRUCache<BrokenAlternativeService, int> {
  40. public:
  41. explicit RecentlyBrokenAlternativeServices(
  42. int max_recently_broken_alternative_service_entries)
  43. : base::LRUCache<BrokenAlternativeService, int>(
  44. max_recently_broken_alternative_service_entries) {}
  45. };
  46. // This class tracks HTTP alternative services that have been marked as broken.
  47. // The brokenness of an alt-svc will expire after some time according to an
  48. // exponential back-off formula: each time an alt-svc is marked broken, the
  49. // expiration delay will be some constant multiple of its previous expiration
  50. // delay. This prevents broken alt-svcs from being retried too often by the
  51. // network stack.
  52. //
  53. // Intended solely for use by HttpServerProperties.
  54. class NET_EXPORT_PRIVATE BrokenAlternativeServices {
  55. public:
  56. // Delegate to be used by owner so it can be notified when the brokenness of
  57. // an AlternativeService expires.
  58. class NET_EXPORT Delegate {
  59. public:
  60. // Called when a broken alternative service's expiration time is reached.
  61. virtual void OnExpireBrokenAlternativeService(
  62. const AlternativeService& expired_alternative_service,
  63. const NetworkIsolationKey& network_isolation_key) = 0;
  64. virtual ~Delegate() = default;
  65. };
  66. // |delegate| will be notified when a broken alternative service expires. It
  67. // must not be null.
  68. // |clock| is used for setting expiration times and scheduling the
  69. // expiration of broken alternative services. It must not be null.
  70. // |delegate| and |clock| are both unowned and must outlive this.
  71. BrokenAlternativeServices(int max_recently_broken_alternative_service_entries,
  72. Delegate* delegate,
  73. const base::TickClock* clock);
  74. BrokenAlternativeServices(const BrokenAlternativeServices&) = delete;
  75. void operator=(const BrokenAlternativeServices&) = delete;
  76. ~BrokenAlternativeServices();
  77. // Clears all broken and recently-broken alternative services (i.e. mark all
  78. // as not broken nor recently-broken).
  79. void Clear();
  80. // Marks |broken_alternative_service| as broken until an expiration delay
  81. // (determined by how many consecutive times it's been marked broken before).
  82. // After the delay, it will be in the recently broken state. However, when the
  83. // default network changes, the service will immediately be in the working
  84. // state.
  85. void MarkBrokenUntilDefaultNetworkChanges(
  86. const BrokenAlternativeService& broken_alternative_service);
  87. // Marks |broken_alternative_service| as broken until an expiration delay
  88. // (determined by how many consecutive times it's been marked broken before).
  89. // After the delay, it will be in the recently broken state. When the default
  90. // network changes, the brokenness state of this service remains unchanged.
  91. void MarkBroken(const BrokenAlternativeService& broken_alternative_service);
  92. // Marks |broken_alternative_service| as recently broken. Being recently
  93. // broken will cause WasAlternativeServiceRecentlyBroken(alternative_service,
  94. // network_isolation_key) to return true until Confirm(alternative_service,
  95. // network_isolation_key) is called.
  96. void MarkRecentlyBroken(
  97. const BrokenAlternativeService& broken_alternative_service);
  98. // Returns true if the alternative service is considered broken.
  99. bool IsBroken(
  100. const BrokenAlternativeService& broken_alternative_service) const;
  101. // If the alternative service is considered broken, returns true and sets
  102. // |brokenness_expiration| to the expiration time for that service.
  103. // Returns false otherwise.
  104. bool IsBroken(const BrokenAlternativeService& broken_alternative_service,
  105. base::TimeTicks* brokenness_expiration) const;
  106. // Returns true if MarkRecentlyBroken(alternative_service)
  107. // or MarkBroken(alternative_service) has been called and
  108. // Confirm(alternative_service) has not been called
  109. // afterwards (even if brokenness of |alternative_service| has expired).
  110. bool WasRecentlyBroken(
  111. const BrokenAlternativeService& broken_alternative_service);
  112. // Changes the alternative service to be considered as working.
  113. void Confirm(const BrokenAlternativeService& broken_alternative_service);
  114. // Clears all alternative services which were marked as broken until the
  115. // default network changed, those services will now be considered working.
  116. // Returns true if there was any broken alternative service affected by this
  117. // network change.
  118. bool OnDefaultNetworkChanged();
  119. // Sets broken and recently broken alternative services.
  120. // |broken_alternative_service_list|, |recently_broken_alternative_services|
  121. // must not be nullptr.
  122. //
  123. // If a broken/recently-broken alt svc that's being added is already stored,
  124. // the stored expiration/broken-count for that alt svc will be overwritten
  125. // with the new value.
  126. void SetBrokenAndRecentlyBrokenAlternativeServices(
  127. std::unique_ptr<BrokenAlternativeServiceList>
  128. broken_alternative_service_list,
  129. std::unique_ptr<RecentlyBrokenAlternativeServices>
  130. recently_broken_alternative_services);
  131. // If values are present, sets initial_delay_ and
  132. // exponential_backoff_on_initial_delay_ which are used to calculate delay of
  133. // broken alternative services.
  134. void SetDelayParams(
  135. absl::optional<base::TimeDelta> initial_delay,
  136. absl::optional<bool> exponential_backoff_on_initial_delay);
  137. const BrokenAlternativeServiceList& broken_alternative_service_list() const;
  138. const RecentlyBrokenAlternativeServices&
  139. recently_broken_alternative_services() const;
  140. private:
  141. // TODO (wangyix): modify HttpServerProperties unit tests so this friendness
  142. // is no longer required.
  143. friend class HttpServerPropertiesPeer;
  144. struct AlternativeServiceHash {
  145. size_t operator()(const net::AlternativeService& entry) const {
  146. return entry.protocol ^ std::hash<std::string>()(entry.host) ^ entry.port;
  147. }
  148. };
  149. typedef std::map<BrokenAlternativeService,
  150. BrokenAlternativeServiceList::iterator>
  151. BrokenMap;
  152. // Helper method that marks |broken_alternative_service| as broken until
  153. // an expiration delay (determined by how many consecutive times it's been
  154. // marked broken before). After the delay, it will be in the recently broken
  155. // state.
  156. void MarkBrokenImpl(
  157. const BrokenAlternativeService& broken_alternative_service);
  158. // Inserts |broken_alternative_service| and its |expiration| time into
  159. // |broken_alternative_service_list_| and |broken_alternative_service_map_|.
  160. // |it| is the position in |broken_alternative_service_list_| where it was
  161. // inserted.
  162. bool AddToBrokenListAndMap(
  163. const BrokenAlternativeService& broken_alternative_service,
  164. base::TimeTicks expiration,
  165. BrokenAlternativeServiceList::iterator* it);
  166. void ExpireBrokenAlternateProtocolMappings();
  167. void ScheduleBrokenAlternateProtocolMappingsExpiration();
  168. raw_ptr<Delegate> delegate_; // Unowned
  169. raw_ptr<const base::TickClock> clock_; // Unowned
  170. // List of <broken alt svc, expiration time> pairs sorted by expiration time.
  171. BrokenAlternativeServiceList broken_alternative_service_list_;
  172. // A map from broken alt-svcs to their iterator pointing to that alt-svc's
  173. // position in |broken_alternative_service_list_|.
  174. BrokenMap broken_alternative_service_map_;
  175. // A set of broken alternative services on the current default
  176. // network. This will be cleared every time the default network changes.
  177. std::set<BrokenAlternativeService>
  178. broken_alternative_services_on_default_network_;
  179. // Maps broken alternative services to how many times they've been marked
  180. // broken.
  181. RecentlyBrokenAlternativeServices recently_broken_alternative_services_;
  182. // Used for scheduling the task that expires the brokenness of alternative
  183. // services.
  184. base::OneShotTimer expiration_timer_;
  185. // Delay for the 1st time alternative service is marked broken.
  186. base::TimeDelta initial_delay_;
  187. // If true, the delay for broken alternative service =
  188. // initial_delay_for_broken_alternative_service * (1 << broken_count).
  189. // Otherwise, the delay would be initial_delay_for_broken_alternative_service,
  190. // 5min, 10min.. and so on.
  191. bool exponential_backoff_on_initial_delay_ = true;
  192. base::WeakPtrFactory<BrokenAlternativeServices> weak_ptr_factory_{this};
  193. };
  194. } // namespace net
  195. #endif // NET_HTTP_BROKEN_ALTERNATIVE_SERVICES_H_