-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Add IterableAsyncQueue. #118
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
lib/shared/common/src/main/java/com/launchdarkly/sdk/collections/IterableAsyncQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.launchdarkly.sdk.collections; | ||
|
|
||
| import java.util.LinkedList; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** | ||
| * A thread-safe unbounded queue that provides asynchronous consumption via {@link CompletableFuture}. | ||
| * <p> | ||
| * This queue supports multiple concurrent producers and consumers. Items are delivered in FIFO order. | ||
| * The {@link #take()} method returns a {@link CompletableFuture} that either completes immediately | ||
| * if an item is available, or completes later when an item is added via {@link #put(Object)}. | ||
| * <p> | ||
| * When multiple consumers are waiting (i.e., multiple pending {@link #take()} calls), they are | ||
| * satisfied in FIFO order as items become available. | ||
| * <p> | ||
| * Null values are supported. | ||
| * | ||
| * @param <T> the type of elements held in this queue | ||
| */ | ||
| class IterableAsyncQueue<T> { | ||
| private final Object lock = new Object(); | ||
| private final LinkedList<T> queue = new LinkedList<>(); | ||
|
|
||
| private final LinkedList<CompletableFuture<T>> pendingFutures = new LinkedList<>(); | ||
|
|
||
| /** | ||
| * Adds an item to the queue. | ||
| * <p> | ||
| * If there is a consumer is waiting (a pending {@link #take()} call), the item is delivered | ||
| * directly to the oldest waiting consumer's future. Otherwise, the item is added to the | ||
| * queue for later consumption. | ||
| * <p> | ||
| * If a future returned by this method is completed or canceled by the caller, then the item associated | ||
| * with that call will not be delivered. It is recommended not to complete or cancel the future | ||
| * returned by {@link #take()} unless you are finished using the queue. | ||
| * | ||
| * @param item the item to add (maybe null) | ||
| */ | ||
| public void put(T item) { | ||
| CompletableFuture<T> pendingFuture = null; | ||
| synchronized (lock) { | ||
| CompletableFuture<T> nextFuture = pendingFutures.pollFirst(); | ||
| if(nextFuture != null) { | ||
| pendingFuture = nextFuture; | ||
| } else { | ||
| queue.addLast(item); | ||
| return; | ||
| } | ||
| } | ||
kinyoklion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Execute callback outside the lock. | ||
| pendingFuture.complete(item); | ||
| } | ||
| /** | ||
| * Retrieves and removes an item from the queue, returning a future that completes with the item. | ||
| * <p> | ||
| * If the queue contains items, returns an already-completed future with the oldest item. | ||
| * If the queue is empty, returns a future that will complete when an item becomes available | ||
| * via {@link #put(Object)}. | ||
| * | ||
| * @return a {@link CompletableFuture} that completes with the next item | ||
| */ | ||
| public CompletableFuture<T> take() { | ||
| synchronized (lock) { | ||
| if(!queue.isEmpty()) { | ||
| return CompletableFuture.completedFuture(queue.removeFirst()); | ||
| } | ||
| CompletableFuture<T> takeFuture = new CompletableFuture<>(); | ||
| pendingFutures.addLast(takeFuture); | ||
| return takeFuture; | ||
| } | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
lib/shared/common/src/main/java/com/launchdarkly/sdk/collections/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /** | ||
| * Collections for use in LaunchDarkly SDKs and components. | ||
| */ | ||
| package com.launchdarkly.sdk.collections; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.