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 new identify method with time out support #344

Merged
merged 1 commit into from
Feb 21, 2024
Merged
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
32 changes: 31 additions & 1 deletion LaunchDarkly/LaunchDarkly/LDClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,42 @@ public class LDClient {
}

let identifyTask = Task(work: work, sheddable: sheddable) { [self] result in
os_log("%s identity completion with result %s", log: config.logger, type: .debug, typeName(and: #function), String(describing: result))
os_log("%s identify completed with result %s", log: config.logger, type: .debug, typeName(and: #function), String(describing: result))
completion(IdentifyResult(from: result))
}
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)
tanderson-ld marked this conversation as resolved.
Show resolved Hide resolved
}
}

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