Skip to content

Commit

Permalink
feat: Add new identify method with time out support
Browse files Browse the repository at this point in the history
A callback provided to the `identify` method may not be executed for a
long time under the right circumstances (e.g. the network is slow or
unavailable).

Customers can now ensure their callbacks will fire within a specified
time interval by calling the new
`LDClient.identify(context:timeout:completion:)` method.
  • Loading branch information
keelerm84 committed Feb 20, 2024
1 parent 9054985 commit e9f5af8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
30 changes: 30 additions & 0 deletions LaunchDarkly/LaunchDarkly/LDClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,36 @@ public class LDClient {
identifyQueue.enqueue(request: identifyTask)
}

/**
Sets the LDContext into the LDClient inline with the behavior detailed on `LDClient.identify(context: completion:)`. Additionally,
this method will ensure the `completion` parameter will be called within the specified time interval.
Note that the `completion` method being invoked does not mean that the identify request has been cancelled. The identify request will
continue attempting to complete as it would with `LDClient.identify(context: completion:)`. Subsequent identify requests queued behind
a timed out request will remain blocked (or shed) until the in flight request completes.
- parameter context: The LDContext set with the desired context.
- parameter timeout: The upper time limit before the `completion` callback will be invoked.
- parameter completion: Closure called when the embedded `setOnlineIdentify` call completes, subject to throttling delays.
*/
public func identify(context: LDContext, timeout: TimeInterval, completion: @escaping ((_ result: IdentifyResult) -> Void)) {
var cancel = false

DispatchQueue.global().asyncAfter(deadline: .now() + timeout) {
guard !cancel else { return }

cancel = true
completion(.timeout)
}

identify(context: context) { result in
guard !cancel else { return }

cancel = true
completion(result)
}
}

func internalIdentify(newContext: LDContext, completion: (() -> Void)? = nil) {
var updatedContext = newContext
if config.autoEnvAttributes {
Expand Down
6 changes: 5 additions & 1 deletion LaunchDarkly/LaunchDarkly/Models/IdentifyResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ public enum IdentifyResult {
*/
case error
/**
The identify request has been replaced with a subsequent request. See `LDClient.identify(context: completion:)` for more details.
The identify request has been replaced with a subsequent request. Read `LDClient.identify(context: completion:)` for more details.
*/
case shed
/**
The identify request exceeded some time out parameter. Read `LDClient.identify(context: timeout: completion)` for more details.
*/
case timeout

init(from: TaskResult) {
switch from {
Expand Down

0 comments on commit e9f5af8

Please sign in to comment.