File: quic_connectivity_monitor.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (126 lines) | stat: -rw-r--r-- 5,384 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_QUIC_QUIC_CONNECTIVITY_MONITOR_H_
#define NET_QUIC_QUIC_CONNECTIVITY_MONITOR_H_

#include <set>

#include "base/memory/raw_ptr.h"
#include "base/numerics/clamped_math.h"
#include "net/base/network_handle.h"
#include "net/quic/quic_chromium_client_session.h"

namespace net {

// Responsible for monitoring path degrading detection/recovery events on the
// default network interface.
// Reset all raw observations (reported by sessions) when the default network
// is changed, which happens either:
// - via OnDefaultNetworkUpdated if handles::NetworkHandle is supported on the
// platform;
// - via OnIPAddressChanged otherwise.
class NET_EXPORT_PRIVATE QuicConnectivityMonitor
    : public QuicChromiumClientSession::ConnectivityObserver {
 public:
  explicit QuicConnectivityMonitor(handles::NetworkHandle default_network);

  QuicConnectivityMonitor(const QuicConnectivityMonitor&) = delete;
  QuicConnectivityMonitor& operator=(const QuicConnectivityMonitor&) = delete;

  ~QuicConnectivityMonitor() override;

  // Records connectivity related stats to histograms.
  void RecordConnectivityStatsToHistograms(
      const std::string& platform_notification,
      handles::NetworkHandle affected_network) const;

  // Returns the number of sessions that are currently degrading on the default
  // network interface.
  size_t GetNumDegradingSessions() const;

  // Returns the number of reports received for |write_error_code| on
  // |default_network|.
  size_t GetCountForWriteErrorCode(int write_error_code) const;

  // Called to set up the initial default network, which happens when the
  // default network tracking is lost upon |this| creation.
  void SetInitialDefaultNetwork(handles::NetworkHandle default_network);

  // Called when handles::NetworkHandle is supported and the default network
  // interface used by the platform is updated.
  void OnDefaultNetworkUpdated(handles::NetworkHandle default_network);

  // Called when handles::NetworkHandle is NOT supported and the IP address of
  // the primary interface changes. This includes when the primary interface
  // itself changes.
  void OnIPAddressChanged();

  // Called when |session| is marked as going away due to IP address change.
  void OnSessionGoingAwayOnIPAddressChange(QuicChromiumClientSession* session);

  // QuicChromiumClientSession::ConnectivityObserver implementation.
  void OnSessionPathDegrading(QuicChromiumClientSession* session,
                              handles::NetworkHandle network) override;

  void OnSessionResumedPostPathDegrading(
      QuicChromiumClientSession* session,
      handles::NetworkHandle network) override;

  void OnSessionEncounteringWriteError(QuicChromiumClientSession* session,
                                       handles::NetworkHandle network,
                                       int error_code) override;

  void OnSessionClosedAfterHandshake(QuicChromiumClientSession* session,
                                     handles::NetworkHandle network,
                                     quic::ConnectionCloseSource source,
                                     quic::QuicErrorCode error_code) override;

  void OnSessionRegistered(QuicChromiumClientSession* session,
                           handles::NetworkHandle network) override;

  void OnSessionRemoved(QuicChromiumClientSession* session) override;

 private:
  // Size chosen per net.QuicSession.WriteError histogram.
  using WriteErrorMap = base::flat_map<int, size_t>;
  // The most common QuicErrorCode cared by this monitor is:
  // QUIC_PUBLIC_RESET by the peer, or
  // QUIC_PACKET_WRITE_ERROR/QUIC_TOO_MANY_RTOS by self.
  using QuicErrorCodeMap = base::flat_map<quic::QuicErrorCode, size_t>;

  // If handles::NetworkHandle is not supported, always set to
  // handles::kInvalidNetworkHandle.
  handles::NetworkHandle default_network_;
  // Sessions that are currently degrading on the |default_network_|.
  std::set<raw_ptr<QuicChromiumClientSession, SetExperimental>>
      degrading_sessions_;
  // Sessions that are currently active on the |default_network_|.
  std::set<raw_ptr<QuicChromiumClientSession, SetExperimental>>
      active_sessions_;

  // Number of sessions that have been active or created during the period of
  // a speculative connectivity failure.
  // The period of a speculative connectivity failure
  // - starts by the earliest detection of path degradation or a connectivity
  //   related packet write error,
  // - ends immediately by the detection of path recovery or a network change.
  // Use clamped math to cap number of sessions at INT_MAX.
  std::optional<base::ClampedNumeric<int>>
      num_sessions_active_during_current_speculative_connectivity_failure_;
  // Total number of sessions that has been degraded before any recovery,
  // including no longer active sessions.
  // Use clamped math to cap number of sessions at INT_MAX.
  base::ClampedNumeric<int> num_all_degraded_sessions_{0};

  // Map from the write error code to the corresponding number of reports.
  WriteErrorMap write_error_map_;
  QuicErrorCodeMap quic_error_map_;

  base::WeakPtrFactory<QuicConnectivityMonitor> weak_factory_{this};
};

}  // namespace net

#endif  // NET_QUIC_QUIC_CONNECTIVITY_MONITOR_H_