-
Notifications
You must be signed in to change notification settings - Fork 1
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: awaitReconcile race with contextChanges #185
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,12 +14,15 @@ import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.TimeoutCancellationException | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.drop | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withTimeout | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.encodeToJsonElement | ||
|
@@ -77,10 +80,23 @@ class Confidence internal constructor( | |
return flagResolver.resolve(flags, getContext()) | ||
} | ||
|
||
suspend fun awaitReconciliation() { | ||
if (currentFetchJob != null) { | ||
currentFetchJob?.join() | ||
activate() | ||
suspend fun awaitReconciliation(timeoutMillis: Long = 5000) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this support multiple concurrent calls? My concern is: |
||
try { | ||
withTimeout(timeoutMillis) { | ||
while (currentFetchJob == null || | ||
currentFetchJob?.isCompleted == true || | ||
currentFetchJob?.isCancelled == true | ||
Comment on lines
+87
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe replace these with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm, perhaps. However I'm not certain having this is a great common timeout is great (imagine you call this function when we are actually NOT doing a network call, then it'll always just wait for timeoutMillis)... I might push an update. Another approach would be to "delay" until |
||
) { | ||
// If this function is called just after a putContext it will likely just delay once. | ||
delay(1) | ||
Comment on lines
+90
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the un-predictability of when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please explain more, not sure I understand the concern. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what I am getting at is: are there more idiomatic ways to achieve this observing behaviour? I am looking at |
||
} | ||
if (currentFetchJob != null) { | ||
currentFetchJob?.join() | ||
activate() | ||
} | ||
} | ||
} catch (e: TimeoutCancellationException) { | ||
debugLogger?.logMessage("awaitReconciliation() timed out after $timeoutMillis") | ||
} | ||
} | ||
|
||
|
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.
Why did this part of the API change? 🤔
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.
I have no idea 🤷