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

Simplify the MultiSubscribe operator implementation #1257

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
*/
package io.smallrye.mutiny.operators.multi;

import static io.smallrye.mutiny.helpers.Subscriptions.CANCELLED;

import java.util.concurrent.Executor;
import java.util.concurrent.Flow;
import java.util.concurrent.Flow.Subscription;
import java.util.concurrent.RejectedExecutionException;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.helpers.ParameterValidation;
import io.smallrye.mutiny.subscription.MultiSubscriber;

/**
* Subscribes to the upstream asynchronously using the given executor.
* Subscribes to the upstream asynchronously using the given executor, and ensure all
* {@link Flow.Subscription#request(long)} calls happen from that executor.
*
* @param <T> the type of item
*/
Expand Down Expand Up @@ -55,43 +57,31 @@ static final class SubscribeOnProcessor<T> extends MultiOperatorProcessor<T, T>
this.executor = executor;
}

@Override
public void onSubscribe(Subscription subscription) {
if (compareAndSetUpstreamSubscription(null, subscription)) {
downstream.onSubscribe(this);
} else {
subscription.cancel();
}
}

void requestUpstream(final long n, final Subscription s) {
try {
executor.execute(() -> s.request(n));
} catch (RejectedExecutionException rejected) {
super.onFailure(rejected);
}
}

void scheduleSubscription(Multi<? extends T> upstream, Flow.Subscriber<? super T> downstream) {
public void scheduleSubscription(Multi<? extends T> upstream, MultiSubscriber<? super T> downstream) {
try {
executor.execute(() -> upstream.subscribe().withSubscriber(this));
} catch (RejectedExecutionException rejected) {
if (!isDone()) {
downstream.onError(rejected);
}
} catch (RejectedExecutionException rejection) {
onFailure(rejection);
}
}

@Override
public void onItem(T t) {
downstream.onItem(t);
}

@Override
public void request(long n) {
if (n > 0) {
Subscription subscription = getUpstreamSubscription();
requestUpstream(n, subscription);
public void request(long numberOfItems) {
if (numberOfItems <= 0) {
onFailure(new IllegalArgumentException("Invalid number of request, must be greater than 0"));
return;
}
if (!isDone()) {
try {
executor.execute(() -> {
Flow.Subscription subscription = getUpstreamSubscription();
if (subscription != CANCELLED) {
subscription.request(numberOfItems);
}
});
} catch (RejectedExecutionException rejected) {
onFailure(rejected);
}
}
}
}
Expand Down