Class AbstractSubscriberAndProducer<T>
- java.lang.Object
-
- com.salesforce.reactivegrpc.common.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 aPublisherand feeds them into aCallStreamObserverwhile respecting backpressure. This class is the inverse ofAbstractStreamObserverAndPublisher.When a gRPC publisher's transport wants more data to transmit, the
CallStreamObserver's onReady handler is signaled. This handler must keep transmitting messages untilCallStreamObserver.isReady()ceases to be true.When a
Publisheris subscribed to by aSubscriber, thePublisherhands theSubscriberaSubscription. When theSubscriberwants more messages from thePublisher, theSubscribercallsSubscription.request(long).To bridge the two idioms: when gRPC wants more messages, the
onReadyHandleris called andrun()calls theSubscription'srequest()method, asking thePublisherto produce another message. Since this class is also registered as thePublisher'sSubscriber, theonNext(Object)method is called.onNext()passes the message to gRPC'sStreamObserver.onNext(Object)method, and then callsrequest()again ifCallStreamObserver.isReady()is true. The loop of request→pass→check is repeated untilisReady()returns false, indicating that the outbound transmit buffer is full and that backpressure must be applied.
-
-
Field Summary
Fields Modifier and Type Field Description protected io.grpc.stub.CallStreamObserver<T>downstream
-
Constructor Summary
Constructors Constructor Description AbstractSubscriberAndProducer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidcancel()protected abstract org.reactivestreams.Subscriptionfuse(org.reactivestreams.Subscription subscription)booleanisCanceled()voidonComplete()voidonError(Throwable t)voidonNext(T t)voidonSubscribe(org.reactivestreams.Subscription subscription)voidrun()voidsubscribe(io.grpc.stub.CallStreamObserver<T> downstream)
-
-
-
Field Detail
-
downstream
protected volatile io.grpc.stub.CallStreamObserver<T> downstream
-
-
Method Detail
-
subscribe
public void subscribe(io.grpc.stub.CallStreamObserver<T> downstream)
-
cancel
public void cancel()
-
isCanceled
public boolean isCanceled()
-
onSubscribe
public void onSubscribe(org.reactivestreams.Subscription subscription)
- Specified by:
onSubscribein interfaceorg.reactivestreams.Subscriber<T>
-
onError
public void onError(Throwable t)
- Specified by:
onErrorin interfaceorg.reactivestreams.Subscriber<T>
-
onComplete
public void onComplete()
- Specified by:
onCompletein interfaceorg.reactivestreams.Subscriber<T>
-
fuse
protected abstract org.reactivestreams.Subscription fuse(org.reactivestreams.Subscription subscription)
-
-