Skip to content

Commit

Permalink
Add connection tester failure pixels (#881)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/1206580121312550/1207743877093953/f

iOS PR: duckduckgo/iOS#3049
macOS PR: duckduckgo/macos-browser#2948

What kind of version bump will this require?: Patch

## Description

Adds pixels to track connection tester failures and recovery.  These should give us a better idea about how users are faring.
  • Loading branch information
diegoreymendez authored Jul 6, 2024
1 parent 5954412 commit 0746af0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Common
final class NetworkProtectionConnectionTester {
enum Result {
case connected
case reconnected
case reconnected(failureCount: Int)
case disconnected(failureCount: Int)
}

Expand Down Expand Up @@ -267,9 +267,8 @@ final class NetworkProtectionConnectionTester {
if failureCount == 0 {
resultHandler(.connected)
} else if failureCount > 0 {
resultHandler(.reconnected(failureCount: failureCount))
failureCount = 0

resultHandler(.reconnected)
}
}

Expand Down
40 changes: 39 additions & 1 deletion Sources/NetworkProtection/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {

public enum Event {
case userBecameActive
case connectionTesterStatusChange(_ status: ConnectionTesterStatus, server: String)
case reportConnectionAttempt(attempt: ConnectionAttempt)
case tunnelStartAttempt(_ step: TunnelStartAttemptStep)
case tunnelStopAttempt(_ step: TunnelStopAttemptStep)
Expand Down Expand Up @@ -64,6 +65,16 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
case failure
}

public enum ConnectionTesterStatus {
case failed(duration: Duration)
case recovered(duration: Duration, failureCount: Int)

public enum Duration: String {
case immediate
case extended
}
}

// MARK: - Error Handling

public enum TunnelError: LocalizedError, CustomNSError, SilentErrorConvertible {
Expand Down Expand Up @@ -309,23 +320,50 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {

// MARK: - Connection tester

private static let connectionTesterExtendedFailuresCount = 8
private var isConnectionTesterEnabled: Bool = true

@MainActor
private lazy var connectionTester: NetworkProtectionConnectionTester = {
NetworkProtectionConnectionTester(timerQueue: timerQueue, log: .networkProtectionConnectionTesterLog) { @MainActor [weak self] result in
guard let self else { return }

let serverName = lastSelectedServerInfo?.name ?? "Unknown"

switch result {
case .connected:
self.tunnelHealth.isHavingConnectivityIssues = false
self.updateBandwidthAnalyzerAndRekeyIfExpired()

case .reconnected:
case .reconnected(let failureCount):
providerEvents.fire(
.connectionTesterStatusChange(
.recovered(duration: .immediate, failureCount: failureCount),
server: serverName))

if failureCount >= Self.connectionTesterExtendedFailuresCount {
providerEvents.fire(
.connectionTesterStatusChange(
.recovered(duration: .extended, failureCount: failureCount),
server: serverName))
}

self.tunnelHealth.isHavingConnectivityIssues = false
self.updateBandwidthAnalyzerAndRekeyIfExpired()

case .disconnected(let failureCount):
if failureCount == 1 {
providerEvents.fire(
.connectionTesterStatusChange(
.failed(duration: .immediate),
server: serverName))
} else if failureCount == 8 {
providerEvents.fire(
.connectionTesterStatusChange(
.failed(duration: .extended),
server: serverName))
}

self.tunnelHealth.isHavingConnectivityIssues = true
self.bandwidthAnalyzer.reset()
}
Expand Down
1 change: 1 addition & 0 deletions Sources/PixelKit/PixelKit+Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Foundation
public extension PixelKit {

enum Parameters: Hashable {
public static let count = "count"
public static let duration = "duration"
public static let test = "test"
public static let appVersion = "appVersion"
Expand Down

0 comments on commit 0746af0

Please sign in to comment.