-
Notifications
You must be signed in to change notification settings - Fork 3
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
fix: Fix FlagApplier async behaviour #70
Conversation
73ae9ce
to
f5b0d30
Compare
9895bdc
to
11e088c
Compare
Signed-off-by: Fabrizio Demaria <[email protected]>
957f127
to
52d4ad8
Compare
63fa498
to
3b1a06a
Compare
Signed-off-by: Fabrizio Demaria <[email protected]>
Signed-off-by: Fabrizio Demaria <[email protected]>
3b1a06a
to
223418c
Compare
DispatchQueue.main.asyncAfter(deadline: .now() + retryWait) { | ||
self.perform(request: request, retry: retry, completion: completion) | ||
} | ||
try? await Task.sleep(nanoseconds: UInt64(retryWait * 1_000_000_000)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure there is a better way to achieve this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like sleep
in with Task
is a fine way to schedule tasks in the newest Swift: https://www.swiftbysundell.com/articles/delaying-an-async-swift-task/
guard let httpResponse = response as? HTTPURLResponse else { | ||
completion(nil, nil, HttpClientError.invalidResponse) | ||
do { | ||
async let (data, response) = await self.session.data(for: request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dataTask
is not async, but this data
function is, hence this change
@@ -37,68 +37,66 @@ final class FlagApplierWithRetries: FlagApplier { | |||
|
|||
public func apply(flagName: String, resolveToken: String) async { | |||
let applyTime = Date.backport.now | |||
let (data, added) = await cacheDataInteractor.add( | |||
async let (data, added) = await cacheDataInteractor.add( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't here the problem that the add function in the cacheDataInteractor is not defined async? then if it's defined as async we can just await it normally here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we try making the cache interactor add funciton async and using await when accessing cache in here ?
Signed-off-by: Fabrizio Demaria <[email protected]>
do { | ||
let (data, response) = try await self.session.data(for: request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error
is not returned from .data()
, while that was the case with .dataTask()
. However, .data()
is marked as throwing, so the do/catch wrapping should in practice replicate the exact same behaviour of .dataTask()
Looks good 👍 |
Issues with flaky tests seem to be caused by the async
apply
function inFlagApplier
not really waiting for the entire triggerBatch operation to finish (including network + updating the cache for apply status), before returning.Note: older context around the existing asynchronous setup: #35 (not sure how much this is relevant for this PR)