Skip to content

Commit

Permalink
Set default timeout to 30 seconds and make it configurable in Network…
Browse files Browse the repository at this point in the history
…Configuration
  • Loading branch information
matus-tomlein committed Jan 30, 2024
1 parent 2904a74 commit 7fe3cf0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Sources/Core/Emitter/Emitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,16 @@ class Emitter: EmitterEventProcessing {
requestHeaders: [String: String]? = nil,
serverAnonymisation: Bool? = nil,
eventStore: EventStore? = nil,
timeout: TimeInterval = EmitterDefaults.emitTimeout,
builder: ((Emitter) -> (Void))? = nil) {
self.namespace = namespace
self.eventStore = eventStore ?? Emitter.defaultEventStore(namespace: namespace)

let defaultNetworkConnection = DefaultNetworkConnection(
urlString: urlEndpoint,
httpMethod: method ?? EmitterDefaults.httpMethod,
customPostPath: customPostPath
customPostPath: customPostPath,
timeout: timeout
)
defaultNetworkConnection.requestHeaders = requestHeaders
defaultNetworkConnection.serverAnonymisation = serverAnonymisation ?? EmitterDefaults.serverAnonymisation
Expand Down
1 change: 1 addition & 0 deletions Sources/Core/Tracker/ServiceProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ class ServiceProvider: NSObject, ServiceProviderProtocol {
requestHeaders: self.networkConfiguration.requestHeaders,
serverAnonymisation: self.emitterConfiguration.serverAnonymisation,
eventStore: self.emitterConfiguration.eventStore,
timeout: self.networkConfiguration.timeout,
builder: builder
)
}
Expand Down
21 changes: 19 additions & 2 deletions Sources/Snowplow/Configurations/NetworkConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@ public class NetworkConfiguration: SerializableConfiguration, ConfigurationProto
set { _requestHeaders = newValue }
}

private var _timeout: TimeInterval?
/// The maximum timeout for emitting events to the collector.
/// Defaults to 30 seconds.
@objc var timeout: TimeInterval {
get { return _timeout ?? sourceConfig?.timeout ?? EmitterDefaults.emitTimeout }
set { _timeout = newValue }
}

// MARK: - Internal

/// Fallback configuration to read from in case requested values are not present in this configuraiton.
internal var sourceConfig: NetworkConfiguration?

// TODO: add -> @property () NSInteger timeout;

internal override init() {
}
Expand Down Expand Up @@ -132,6 +138,14 @@ public class NetworkConfiguration: SerializableConfiguration, ConfigurationProto
self.requestHeaders = headers
return self
}

/// The maximum timeout for emitting events to the collector.
/// Defaults to 30 seconds.
@objc
public func timeout(_ timeout: TimeInterval) -> Self {
self.timeout = timeout
return self
}

// MARK: - NSCopying

Expand All @@ -144,6 +158,7 @@ public class NetworkConfiguration: SerializableConfiguration, ConfigurationProto
copy = NetworkConfiguration(endpoint: endpoint ?? "", method: method )
}
copy?.customPostPath = customPostPath
copy?.timeout = timeout
return copy!
}

Expand All @@ -159,6 +174,7 @@ public class NetworkConfiguration: SerializableConfiguration, ConfigurationProto
coder.encode(method.rawValue, forKey: "method")
coder.encode(customPostPath, forKey: "customPostPath")
coder.encode(requestHeaders, forKey: "requestHeaders")
coder.encode(timeout, forKey: "timeout")
}

required init?(coder: NSCoder) {
Expand All @@ -167,5 +183,6 @@ public class NetworkConfiguration: SerializableConfiguration, ConfigurationProto
_method = HttpMethodOptions(rawValue: coder.decodeInteger(forKey: "method"))
_customPostPath = coder.decodeObject(forKey: "customPostPath") as? String
_requestHeaders = coder.decodeObject(forKey: "requestHeaders") as? [String : String]
_timeout = coder.decodeObject(forKey: "timeout") as? TimeInterval
}
}
1 change: 1 addition & 0 deletions Sources/Snowplow/Emitter/EmitterDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public class EmitterDefaults {
public private(set) static var retryFailedRequests = true
public private(set) static var maxEventStoreSize: Int64 = 1000 // events
public private(set) static var maxEventStoreAge: TimeInterval = TimeInterval(60 * 60 * 24 * 30) // 30 days
public private(set) static var emitTimeout: TimeInterval = TimeInterval(30) // 30 seconds
}
12 changes: 6 additions & 6 deletions Sources/Snowplow/Network/DefaultNetworkConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class DefaultNetworkConnection: NSObject, NetworkConnection {
set(method) { _httpMethod = method; setup() }
}

private var _emitThreadPoolSize = 15
private var _emitThreadPoolSize = EmitterDefaults.emitThreadPoolSize
/// The number of threads used by the emitter.
@objc
public var emitThreadPoolSize: Int {
Expand All @@ -57,11 +57,11 @@ public class DefaultNetworkConnection: NSObject, NetworkConnection {

/// Maximum event size for a GET request.
@objc
public var byteLimitGet: Int = 40000
public var byteLimitGet: Int = EmitterDefaults.byteLimitGet

/// Maximum event size for a POST request.
@objc
public var byteLimitPost = 40000
public var byteLimitPost = EmitterDefaults.byteLimitPost

private var _customPostPath: String?
/// A custom path that is used on the endpoint to send requests.
Expand All @@ -78,9 +78,9 @@ public class DefaultNetworkConnection: NSObject, NetworkConnection {
@objc
public var serverAnonymisation = false
private var dataOperationQueue = OperationQueue()

/// Custom timeout for the requests
@objc
public var timeout: TimeInterval
private var timeout: TimeInterval

private var protocolClasses: [AnyClass]?
private var _urlSession: URLSession?
Expand All @@ -102,7 +102,7 @@ public class DefaultNetworkConnection: NSObject, NetworkConnection {
httpMethod: HttpMethodOptions = EmitterDefaults.httpMethod,
protocol: ProtocolOptions = EmitterDefaults.httpProtocol,
customPostPath: String? = nil,
timeout: TimeInterval = 60,
timeout: TimeInterval = EmitterDefaults.emitTimeout,
protocolClasses: [AnyClass]? = nil) {
self._urlString = urlString
self.timeout = timeout
Expand Down

0 comments on commit 7fe3cf0

Please sign in to comment.