1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_QUIC_QUIC_CONNECTIVITY_MONITOR_H_
6 #define NET_QUIC_QUIC_CONNECTIVITY_MONITOR_H_
10 #include "base/numerics/clamped_math.h"
11 #include "net/base/network_handle.h"
12 #include "net/quic/quic_chromium_client_session.h"
16 // Responsible for monitoring path degrading detection/recovery events on the
17 // default network interface.
18 // Reset all raw observations (reported by sessions) when the default network
19 // is changed, which happens either:
20 // - via OnDefaultNetworkUpdated if handles::NetworkHandle is supported on the
22 // - via OnIPAddressChanged otherwise.
23 class NET_EXPORT_PRIVATE QuicConnectivityMonitor
24 : public QuicChromiumClientSession::ConnectivityObserver {
26 explicit QuicConnectivityMonitor(handles::NetworkHandle default_network);
28 QuicConnectivityMonitor(const QuicConnectivityMonitor&) = delete;
29 QuicConnectivityMonitor& operator=(const QuicConnectivityMonitor&) = delete;
31 ~QuicConnectivityMonitor() override;
33 // Records connectivity related stats to histograms.
34 void RecordConnectivityStatsToHistograms(
35 const std::string& platform_notification,
36 handles::NetworkHandle affected_network) const;
38 // Returns the number of sessions that are currently degrading on the default
40 size_t GetNumDegradingSessions() const;
42 // Returns the number of reports received for |write_error_code| on
44 size_t GetCountForWriteErrorCode(int write_error_code) const;
46 // Called to set up the initial default network, which happens when the
47 // default network tracking is lost upon |this| creation.
48 void SetInitialDefaultNetwork(handles::NetworkHandle default_network);
50 // Called when handles::NetworkHandle is supported and the default network
51 // interface used by the platform is updated.
52 void OnDefaultNetworkUpdated(handles::NetworkHandle default_network);
54 // Called when handles::NetworkHandle is NOT supported and the IP address of
55 // the primary interface changes. This includes when the primary interface
57 void OnIPAddressChanged();
59 // Called when |session| is marked as going away due to IP address change.
60 void OnSessionGoingAwayOnIPAddressChange(QuicChromiumClientSession* session);
62 // QuicChromiumClientSession::ConnectivityObserver implementation.
63 void OnSessionPathDegrading(QuicChromiumClientSession* session,
64 handles::NetworkHandle network) override;
66 void OnSessionResumedPostPathDegrading(
67 QuicChromiumClientSession* session,
68 handles::NetworkHandle network) override;
70 void OnSessionEncounteringWriteError(QuicChromiumClientSession* session,
71 handles::NetworkHandle network,
72 int error_code) override;
74 void OnSessionClosedAfterHandshake(QuicChromiumClientSession* session,
75 handles::NetworkHandle network,
76 quic::ConnectionCloseSource source,
77 quic::QuicErrorCode error_code) override;
79 void OnSessionRegistered(QuicChromiumClientSession* session,
80 handles::NetworkHandle network) override;
82 void OnSessionRemoved(QuicChromiumClientSession* session) override;
85 // Size chosen per net.QuicSession.WriteError histogram.
86 using WriteErrorMap = base::flat_map<int, size_t>;
87 // The most common QuicErrorCode cared by this monitor is:
88 // QUIC_PUBLIC_RESET by the peer, or
89 // QUIC_PACKET_WRITE_ERROR/QUIC_TOO_MANY_RTOS by self.
90 using QuicErrorCodeMap = base::flat_map<quic::QuicErrorCode, size_t>;
92 // If handles::NetworkHandle is not supported, always set to
93 // handles::kInvalidNetworkHandle.
94 handles::NetworkHandle default_network_;
95 // Sessions that are currently degrading on the |default_network_|.
96 std::set<QuicChromiumClientSession*> degrading_sessions_;
97 // Sessions that are currently active on the |default_network_|.
98 std::set<QuicChromiumClientSession*> active_sessions_;
100 // Number of sessions that have been active or created during the period of
101 // a speculative connectivity failure.
102 // The period of a speculative connectivity failure
103 // - starts by the earliest detection of path degradation or a connectivity
104 // related packet write error,
105 // - ends immediately by the detection of path recovery or a network change.
106 // Use clamped math to cap number of sessions at INT_MAX.
107 absl::optional<base::ClampedNumeric<int>>
108 num_sessions_active_during_current_speculative_connectivity_failure_;
109 // Total number of sessions that has been degraded before any recovery,
110 // including no longer active sessions.
111 // Use clamped math to cap number of sessions at INT_MAX.
112 base::ClampedNumeric<int> num_all_degraded_sessions_{0};
114 // Map from the write error code to the corresponding number of reports.
115 WriteErrorMap write_error_map_;
116 QuicErrorCodeMap quic_error_map_;
118 base::WeakPtrFactory<QuicConnectivityMonitor> weak_factory_{this};
123 #endif // NET_QUIC_QUIC_CONNECTIVITY_MONITOR_H_