Search in sources :

Example 1 with DelayedCancellable

use of io.servicetalk.concurrent.internal.DelayedCancellable in project servicetalk by apple.

the class AbstractSubmitCompletable method handleSubscribe.

@Override
protected final void handleSubscribe(final CompletableSource.Subscriber subscriber) {
    DelayedCancellable cancellable = new DelayedCancellable();
    subscriber.onSubscribe(cancellable);
    final Cancellable eCancellable;
    try {
        eCancellable = runExecutor.execute(() -> {
            try {
                runnable().run();
            } catch (Throwable cause) {
                subscriber.onError(cause);
                return;
            }
            subscriber.onComplete();
        });
    } catch (Throwable cause) {
        // We assume that runExecutor.execute() either throws or executes the Runnable. Hence we do not have to
        // protect against duplicate terminal events.
        subscriber.onError(cause);
        return;
    }
    cancellable.delayedCancellable(eCancellable);
}
Also used : DelayedCancellable(io.servicetalk.concurrent.internal.DelayedCancellable) Cancellable(io.servicetalk.concurrent.Cancellable) DelayedCancellable(io.servicetalk.concurrent.internal.DelayedCancellable)

Example 2 with DelayedCancellable

use of io.servicetalk.concurrent.internal.DelayedCancellable in project servicetalk by apple.

the class CompletableProcessor method handleSubscribe.

@Override
protected void handleSubscribe(Subscriber subscriber) {
    // We must subscribe before adding subscriber the the queue. Otherwise it is possible that this
    // Completable has been terminated and the subscriber may be notified before onSubscribe is called.
    // We used a DelayedCancellable to avoid the case where the Subscriber will synchronously cancel and then
    // we would add the subscriber to the queue and possibly never (until termination) dereference the subscriber.
    DelayedCancellable delayedCancellable = new DelayedCancellable();
    subscriber.onSubscribe(delayedCancellable);
    if (stack.push(subscriber)) {
        delayedCancellable.delayedCancellable(() -> {
            // Cancel in this case will just cleanup references from the queue to ensure we don't prevent GC of
            // these references.
            stack.relaxedRemove(subscriber);
        });
    }
}
Also used : DelayedCancellable(io.servicetalk.concurrent.internal.DelayedCancellable)

Example 3 with DelayedCancellable

use of io.servicetalk.concurrent.internal.DelayedCancellable in project servicetalk by apple.

the class AutoOnSubscribeCompletableSubscriberFunction method apply.

@Override
public Subscriber apply(final Subscriber subscriber) {
    final DelayedCancellable subscription = new DelayedCancellable();
    subscriber.onSubscribe(subscription);
    return new DelegatingCompletableSubscriber(subscriber) {

        @Override
        public void onSubscribe(final Cancellable s) {
            subscription.delayedCancellable(s);
        }
    };
}
Also used : DelayedCancellable(io.servicetalk.concurrent.internal.DelayedCancellable) Cancellable(io.servicetalk.concurrent.Cancellable) DelayedCancellable(io.servicetalk.concurrent.internal.DelayedCancellable)

Example 4 with DelayedCancellable

use of io.servicetalk.concurrent.internal.DelayedCancellable in project servicetalk by apple.

the class TimerCompletable method handleSubscribe.

@Override
protected void handleSubscribe(final Subscriber subscriber) {
    DelayedCancellable cancellable = new DelayedCancellable();
    subscriber.onSubscribe(cancellable);
    try {
        cancellable.delayedCancellable(timeoutExecutor.schedule(subscriber::onComplete, delayNs, NANOSECONDS));
    } catch (Throwable cause) {
        subscriber.onError(cause);
    }
}
Also used : DelayedCancellable(io.servicetalk.concurrent.internal.DelayedCancellable)

Example 5 with DelayedCancellable

use of io.servicetalk.concurrent.internal.DelayedCancellable in project servicetalk by apple.

the class SingleProcessor method handleSubscribe.

@Override
protected void handleSubscribe(final Subscriber<? super T> subscriber) {
    // We must subscribe before adding subscriber the the queue. Otherwise it is possible that this
    // Single has been terminated and the subscriber may be notified before onSubscribe is called.
    // We used a DelayedCancellable to avoid the case where the Subscriber will synchronously cancel and then
    // we would add the subscriber to the queue and possibly never (until termination) dereference the subscriber.
    DelayedCancellable delayedCancellable = new DelayedCancellable();
    subscriber.onSubscribe(delayedCancellable);
    if (stack.push(subscriber)) {
        delayedCancellable.delayedCancellable(() -> {
            // Cancel in this case will just cleanup references from the queue to ensure we don't prevent GC of
            // these references.
            stack.relaxedRemove(subscriber);
        });
    }
}
Also used : DelayedCancellable(io.servicetalk.concurrent.internal.DelayedCancellable)

Aggregations

DelayedCancellable (io.servicetalk.concurrent.internal.DelayedCancellable)10 Cancellable (io.servicetalk.concurrent.Cancellable)4 SubscribableSingle (io.servicetalk.concurrent.api.internal.SubscribableSingle)2 ChannelPipeline (io.netty.channel.ChannelPipeline)1 Subscriber (io.servicetalk.concurrent.CompletableSource.Subscriber)1 Subscriber (io.servicetalk.concurrent.SingleSource.Subscriber)1 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)1 DefaultExecutionContext (io.servicetalk.transport.api.DefaultExecutionContext)1