Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to change URLSessionConfiguration for uploading signals #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/TelemetryClient/SignalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ internal class SignalManager: SignalManageable {

private var signalCache: SignalCache<SignalPostBody>
let configuration: TelemetryManagerConfiguration
private let urlSession: URLSession
private var sendTimer: Timer?

init(configuration: TelemetryManagerConfiguration) {
self.configuration = configuration

urlSession = URLSession(configuration: configuration.urlSessionConfiguration)

// We automatically load any old signals from disk on initialisation
signalCache = SignalCache(logHandler: configuration.logHandler)

Expand Down Expand Up @@ -183,10 +186,7 @@ private extension SignalManager {
self.configuration.logHandler?.log(.debug, message: String(data: urlRequest.httpBody!, encoding: .utf8)!)

/// Wait for connectivity
let config = URLSessionConfiguration.default
config.waitsForConnectivity = true
let session = URLSession(configuration: config)
let task = session.dataTask(with: urlRequest, completionHandler: completionHandler)
let task = self.urlSession.dataTask(with: urlRequest, completionHandler: completionHandler)
task.resume()
}
}
Expand Down
26 changes: 26 additions & 0 deletions Sources/TelemetryClient/TelemetryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ public final class TelemetryManagerConfiguration {
/// Defaults to an empty array.
public var metadataEnrichers: [SignalEnricher] = []

/// The ``URLSessionConfiguration`` to use for sending signals.
///
/// You can use this to set a custom `URLSessionConfiguration`,
/// which can be useful for e.g.
/// - providing a background session configuration instead,
/// - changing configuration values like `tlsMinimumSupportedProtocol`,
/// if your app has to comply with certain security standards.
///
/// The actual ``URLSession`` used for sending signals will be created
/// on base of this configuration, but stays private to `SignalManager`.
///
/// Defaults to a URLSession on base of the default configuration.
///
/// - Note: `waitsForConnectivity` will be always overriden and set
/// to `true`.
///
public lazy var urlSessionConfiguration: URLSessionConfiguration = {
let config = URLSessionConfiguration.default
config.waitsForConnectivity = true
return config
}() {
willSet {
newValue.waitsForConnectivity = true
}
}

public init(appID: String, salt: String? = nil, baseURL: URL? = nil) {
telemetryAppID = appID

Expand Down