Class AbstractStreamObserverAndPublisher<T>

  • Type Parameters:
    T - T
    All Implemented Interfaces:
    io.grpc.stub.StreamObserver<T>, Iterable<T>, Collection<T>, Queue<T>, org.reactivestreams.Publisher<T>, org.reactivestreams.Subscription
    Direct Known Subclasses:
    AbstractClientStreamObserverAndPublisher, AbstractServerStreamObserverAndPublisher

    public abstract class AbstractStreamObserverAndPublisher<T>
    extends AbstractUnimplementedQueue<T>
    implements org.reactivestreams.Publisher<T>, io.grpc.stub.StreamObserver<T>, org.reactivestreams.Subscription, Queue<T>
    AbstractStreamObserverAndPublisher bridges the manual flow control idioms of gRPC and Reactive Streams. This class takes messages off of a CallStreamObserver and feeds them into a Publisher while respecting backpressure. In order to keep backpressure working at the async boundary between CallStreamObserver as an upstream, the AbstractSubscriberAndProducer enable manual flow-control by calling CallStreamObserver.disableAutoInboundFlowControl() at subscription time and prefetch specified by prefetch number of elements at first and then keeps the number of requested element on the same level once the upstream sent amount of elements reach the specified limit. In addition, AbstractStreamObserverAndPublisher take an additional care for some exotic cases when upstream does not respect backpressure of the Reactive-Streams Subscriber as the downstream, so AbstractStreamObserverAndPublisher employees Queue that enqueue each incoming elements and in that way keeps excess of items. In turn, to avoid Queue overwhelming, it is recommended to uses Queue with a fixed size so in the case Queue.offer(Object) returns false the sender thread will be blocked with a busy spin using LockSupport.parkNanos(long) try to enqueue element into Queue until downstream deque element from it.
    • Field Detail

      • TWO_THIRDS_OF_DEFAULT_CHUNK_SIZE

        protected static final int TWO_THIRDS_OF_DEFAULT_CHUNK_SIZE
        See Also:
        Constant Field Values
      • outputFused

        protected volatile boolean outputFused
      • subscription

        protected volatile io.grpc.stub.CallStreamObserver<?> subscription
    • Method Detail

      • onSubscribe

        protected void onSubscribe​(io.grpc.stub.CallStreamObserver<?> upstream)
      • onNext

        public void onNext​(T t)
        Specified by:
        onNext in interface io.grpc.stub.StreamObserver<T>
      • onError

        public void onError​(Throwable t)
        Specified by:
        onError in interface io.grpc.stub.StreamObserver<T>
      • onCompleted

        public void onCompleted()
        Specified by:
        onCompleted in interface io.grpc.stub.StreamObserver<T>
      • subscribe

        public void subscribe​(org.reactivestreams.Subscriber<? super T> actual)
        Specified by:
        subscribe in interface org.reactivestreams.Publisher<T>
      • isCancelled

        public boolean isCancelled()
      • request

        public void request​(long n)
        Specified by:
        request in interface org.reactivestreams.Subscription
      • cancel

        public void cancel()
        Specified by:
        cancel in interface org.reactivestreams.Subscription
      • doOnCancel

        protected void doOnCancel()