Skip to content

feat: Add a timed flushBlocking that waits for delivery - #281

Draft
bradbunce wants to merge 1 commit into
launchdarkly:mainfrom
launchdarkly-labs:feat/flush-blocking-wait
Draft

bradbunce wants to merge 1 commit into
launchdarkly:mainfrom
launchdarkly-labs:feat/flush-blocking-wait

Conversation

@bradbunce

Copy link
Copy Markdown

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Tested on JDK 17 locally. CI runs JDK 8. Not tested on Java 11 or Android. The new interface method is a Java 8 default method, which Android's D8 desugars at every API level.

Related issues

  • Found while building a client-side Java SDK on top of java-sdk-internal. A desktop app needs a way to send its pending events before it exits, without waiting forever if the network is down.

Describe the solution you've provided

flushBlocking() returns once the dispatcher has handed the payload to a delivery thread, not when the delivery is done. A caller that flushes and then exits can lose the events. The only way to wait for delivery today is waitUntilInactive(), which is package-private, has no timeout, and blocks the dispatcher thread while it waits.

This adds boolean flushBlocking(long timeoutMillis):

  • It waits, on the caller's thread, for any delivery in progress to finish, so the flush finds a free worker instead of being skipped because they are all busy.
  • It posts a FLUSH and waits for the dispatcher to handle it. The dispatcher counts the payload as busy before it completes the message, so the next wait can't miss it.
  • It waits for that delivery to finish.
  • All three steps share one deadline. It returns false if the deadline passes first or the processor is closed, and true otherwise.

On the EventProcessor interface it's a default method that calls flushBlocking() and returns true, so existing implementations and mocks still compile.

One related change: triggerFlush now calls notifyAll() instead of notify() when it gives up a busy slot, because a caller of the new method and the dispatcher can now wait on the count at the same time. The worker's release already used notifyAll().

Tests, in DefaultEventProcessorTest:

  • flushBlockingWithTimeoutWaitsForDelivery
  • flushBlockingWithTimeoutReturnsFalseIfDeliveryDoesNotFinishInTime
  • flushBlockingWithTimeoutReturnsFalseIfEarlierDeliveryDoesNotFinishInTime
  • flushBlockingWithTimeoutSucceedsWithNothingToSend
  • flushBlockingWithTimeoutReturnsFalseAfterClose
  • defaultFlushBlockingWithTimeoutCallsFlushBlocking

All 375 tests in lib/shared/internal pass, and DefaultEventProcessorTest passed 5 repeated runs.

Describe alternatives you've considered

  • Changing flushBlocking() itself to wait for delivery. That would change behavior for current callers, and with no timeout it could hang an app with no network.
  • Making waitUntilInactive() public. It has no timeout and blocks the dispatcher thread while it waits.

Additional context

Two paths are marked COVERAGE: because tests can't trigger them reliably: a full inbox, and the dispatcher not reaching the FLUSH message before the deadline.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant