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

feat: add timeout to fetchAndActivate #160

Merged
merged 11 commits into from
Jul 16, 2024
20 changes: 17 additions & 3 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ public class Confidence: ConfidenceEventSender {
storage: storage,
context: context,
parent: self,
debugLogger: debugLogger)
debugLogger: debugLogger
)
}

private func withLock(callback: @escaping (Confidence) -> Void) {
Expand All @@ -297,6 +298,7 @@ extension Confidence {
// Can be configured
internal var region: ConfidenceRegion = .global
internal var initialContext: ConfidenceStruct = [:]
internal var timeout: Double = 10

// Injectable for testing
internal var flagApplier: FlagApplier?
Expand Down Expand Up @@ -356,6 +358,14 @@ extension Confidence {
return self
}

/**
Set the timeout for the network request, defaulting to 10 seconds.
*/
public func withTimeout(timeout: Double) -> Builder {
nickybondarenko marked this conversation as resolved.
Show resolved Hide resolved
self.timeout = timeout
return self
}

/**
Build the Confidence instance.
*/
Expand All @@ -368,7 +378,8 @@ extension Confidence {
}
let options = ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret),
region: region)
region: region,
timeoutIntervalForRequest: timeout)
let metadata = ConfidenceMetadata(
name: sdkId,
version: "0.2.4") // x-release-please-version
Expand All @@ -377,7 +388,10 @@ extension Confidence {
metadata: metadata,
debugLogger: debugLogger
)
let httpClient = NetworkClient(baseUrl: BaseUrlMapper.from(region: options.region))
let httpClient = NetworkClient(
baseUrl: BaseUrlMapper.from(region: options.region),
timeoutIntervalForRequests: options.timeoutIntervalForRequest
)
let flagApplier = flagApplier ?? FlagApplierWithRetries(
httpClient: httpClient,
storage: DefaultStorage(filePath: "confidence.flags.apply"),
Expand Down
5 changes: 4 additions & 1 deletion Sources/Confidence/ConfidenceClientOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ struct ConfidenceClientOptions {
public var credentials: ConfidenceClientCredentials
public var region: ConfidenceRegion
public var initializationStrategy: InitializationStrategy
public var timeoutIntervalForRequest: Double

public init(
credentials: ConfidenceClientCredentials,
region: ConfidenceRegion? = nil,
initializationStrategy: InitializationStrategy = .fetchAndActivate
initializationStrategy: InitializationStrategy = .fetchAndActivate,
timeoutIntervalForRequest: Double
) {
self.credentials = credentials
self.region = region ?? .global
self.initializationStrategy = initializationStrategy
self.timeoutIntervalForRequest = timeoutIntervalForRequest
}
}

Expand Down
5 changes: 4 additions & 1 deletion Sources/Confidence/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ final class NetworkClient: HttpClient {
private let retry: Retry
private let session: URLSession
private let baseUrl: String
private var timeoutIntervalForRequests: Double

public init(
session: URLSession? = nil,
baseUrl: String,
defaultHeaders: [String: String] = [:],
retry: Retry = .none
retry: Retry = .none,
timeoutIntervalForRequests: Double
) {
self.session =
session
Expand All @@ -24,6 +26,7 @@ final class NetworkClient: HttpClient {
self.headers = defaultHeaders
self.retry = retry
self.baseUrl = baseUrl
self.timeoutIntervalForRequests = timeoutIntervalForRequests
nickybondarenko marked this conversation as resolved.
Show resolved Hide resolved
}

public func post<T: Decodable>(
Expand Down
6 changes: 5 additions & 1 deletion Sources/Confidence/RemoteConfidenceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public class RemoteConfidenceClient: ConfidenceClient {
case .usa:
self.baseUrl = "https://events.us.confidence.dev/v1/events"
}
self.httpClient = NetworkClient(session: session, baseUrl: baseUrl)
self.httpClient = NetworkClient(
session: session,
baseUrl: baseUrl,
timeoutIntervalForRequests: options.timeoutIntervalForRequest
)
self.metadata = metadata
self.debugLogger = debugLogger
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Confidence/RemoteResolveConfidenceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class RemoteConfidenceResolveClient: ConfidenceResolveClient {
self.metadata = metadata
self.httpClient = NetworkClient(
session: session,
baseUrl: BaseUrlMapper.from(region: options.region))
baseUrl: BaseUrlMapper.from(region: options.region),
timeoutIntervalForRequests: options.timeoutIntervalForRequest)
}

// MARK: Resolver
Expand Down
4 changes: 4 additions & 0 deletions api/Confidence_public_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"name": "withRegion(region:)",
"declaration": "public func withRegion(region: ConfidenceRegion) -> Builder"
},
{
"name": "withTimeout(timeout:)",
"declaration": "public func withTimeout(timeout: Double) -> Builder"
},
{
"name": "build()",
"declaration": "public func build() -> Confidence"
Expand Down
Loading