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

Add Publisher.replay #2684

Merged
merged 6 commits into from
Oct 2, 2023
Merged

Add Publisher.replay #2684

merged 6 commits into from
Oct 2, 2023

Conversation

Scottmitch
Copy link
Member

Motivation:
Publisher.replay provides the ability to keep state that is preserved for multiple subscribers and across resubscribes.

@Scottmitch Scottmitch force-pushed the replay_publisher branch 3 times, most recently from 4d5db14 to c7dc329 Compare August 31, 2023 10:07
System.arraycopy(currSubs, 0, newSubs, 0, currSubs.length);
newSubs[currSubs.length] = multiSubscriber;
if (newSubscribersUpdater.compareAndSet(this, currSubs, newSubs)) {
addSubscriber(multiSubscriber);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note this code was moved to addSubscriber bcz replay requires that we acquire the lock before making the subscriber visible in the array (via newSubscribersUpdater) and this is preferred approach for multicast too.

@Scottmitch
Copy link
Member Author

This operator can be used to replace LatestValueSubscriber and potentially apply to cases like LoadBalancer.eventStream() (e.g. LoadBalancerReadySubscriber).

@Scottmitch Scottmitch force-pushed the replay_publisher branch 2 times, most recently from fd9369c to cac808a Compare September 4, 2023 12:20
@Scottmitch Scottmitch force-pushed the replay_publisher branch 8 times, most recently from dda1047 to 8d3d602 Compare September 19, 2023 01:05
@Scottmitch Scottmitch force-pushed the replay_publisher branch 2 times, most recently from d47fad3 to 3d6cc9a Compare September 30, 2023 00:42
@Scottmitch Scottmitch force-pushed the replay_publisher branch 2 times, most recently from 7f08898 to 758c2d0 Compare September 30, 2023 07:38
Motivation:
Publisher.replay provides the ability to keep state that is
preserved for multiple subscribers and across resubscribes.
Copy link
Contributor

@bryce-anderson bryce-anderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good other than some minor optimizations.
Thank you for your patience.

Comment on lines 162 to 177
public void accumulate(@Nullable final T t) {
final TimeStampSignal<T> signal = new TimeStampSignal<>(executor.currentTime(NANOSECONDS), t);
for (;;) {
final int qSize = queueSize;
if (qSize < maxItems) {
if (queueSizeUpdater.compareAndSet(this, qSize, qSize + 1)) {
items.add(signal);
break;
}
} else if (queueSizeUpdater.compareAndSet(this, qSize, qSize)) {
items.poll();
items.add(signal);
break;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also an opportunity to evict stale elements if we'd like. We can have a simple private evictStale() method and call it at the start of these public methods. Then the rest looks almost exactly like MostRecentReplayAccumulator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets defer this bcz LazyTimeLimitedReplayAccumulator can do the iteration and removal at the same time which is a bit harder to generalize.

@Scottmitch Scottmitch merged commit da1ea3b into apple:main Oct 2, 2023
15 checks passed
@Scottmitch Scottmitch deleted the replay_publisher branch October 2, 2023 23:23
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.

2 participants