Class AbstractSubscriberAndProducer<T>

  • Type Parameters:
    T - T
    All Implemented Interfaces:
    Runnable, org.reactivestreams.Subscriber<T>
    Direct Known Subclasses:
    AbstractSubscriberAndClientProducer, AbstractSubscriberAndServerProducer

    public abstract class AbstractSubscriberAndProducer<T>
    extends Object
    implements org.reactivestreams.Subscriber<T>, Runnable
    ReactivePublisherBackpressureOnReadyHandler bridges the manual flow control idioms of Reactive Streams and gRPC. This class takes messages off of a Publisher and feeds them into a CallStreamObserver while respecting backpressure. This class is the inverse of AbstractStreamObserverAndPublisher.

    When a gRPC publisher's transport wants more data to transmit, the CallStreamObserver's onReady handler is signaled. This handler must keep transmitting messages until CallStreamObserver.isReady() ceases to be true.

    When a Publisher is subscribed to by a Subscriber, the Publisher hands the Subscriber a Subscription. When the Subscriber wants more messages from the Publisher, the Subscriber calls Subscription.request(long).

    To bridge the two idioms: when gRPC wants more messages, the onReadyHandler is called and run() calls the Subscription's request() method, asking the Publisher to produce another message. Since this class is also registered as the Publisher's Subscriber, the onNext(Object) method is called. onNext() passes the message to gRPC's StreamObserver.onNext(Object) method, and then calls request() again if CallStreamObserver.isReady() is true. The loop of request→pass→check is repeated until isReady() returns false, indicating that the outbound transmit buffer is full and that backpressure must be applied.

    • Field Detail

      • downstream

        protected volatile io.grpc.stub.CallStreamObserver<T> downstream
    • Constructor Detail

      • AbstractSubscriberAndProducer

        public AbstractSubscriberAndProducer()
    • Method Detail

      • subscribe

        public void subscribe​(io.grpc.stub.CallStreamObserver<T> downstream)
      • run

        public void run()
        Specified by:
        run in interface Runnable
      • cancel

        public void cancel()
      • isCanceled

        public boolean isCanceled()
      • onSubscribe

        public void onSubscribe​(org.reactivestreams.Subscription subscription)
        Specified by:
        onSubscribe in interface org.reactivestreams.Subscriber<T>
      • onNext

        public void onNext​(T t)
        Specified by:
        onNext in interface org.reactivestreams.Subscriber<T>
      • onError

        public void onError​(Throwable t)
        Specified by:
        onError in interface org.reactivestreams.Subscriber<T>
      • onComplete

        public void onComplete()
        Specified by:
        onComplete in interface org.reactivestreams.Subscriber<T>
      • fuse

        protected abstract org.reactivestreams.Subscription fuse​(org.reactivestreams.Subscription subscription)