Class AbstractStreamObserverAndPublisher<T>
- java.lang.Object
-
- com.salesforce.reactivegrpc.common.AbstractUnimplementedQueue<T>
-
- com.salesforce.reactivegrpc.common.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>
AbstractStreamObserverAndPublisherbridges the manual flow control idioms of gRPC and Reactive Streams. This class takes messages off of aCallStreamObserverand feeds them into aPublisherwhile respecting backpressure. In order to keep backpressure working at the async boundary betweenCallStreamObserveras an upstream, theAbstractSubscriberAndProducerenable manual flow-control by callingCallStreamObserver.disableAutoInboundFlowControl()at subscription time and prefetch specified byprefetchnumber 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 specifiedlimit. In addition,AbstractStreamObserverAndPublishertake an additional care for some exotic cases when upstream does not respect backpressure of the Reactive-StreamsSubscriberas the downstream, soAbstractStreamObserverAndPublisheremployeesQueuethat enqueue each incoming elements and in that way keeps excess of items. In turn, to avoidQueueoverwhelming, it is recommended to usesQueuewith a fixed size so in the caseQueue.offer(Object)returns false the sender thread will be blocked with a busy spin usingLockSupport.parkNanos(long)try to enqueue element intoQueueuntil downstream deque element from it.
-
-
Field Summary
Fields Modifier and Type Field Description protected static intDEFAULT_CHUNK_SIZEprotected booleanoutputFusedprotected io.grpc.stub.CallStreamObserver<?>subscriptionprotected static intTWO_THIRDS_OF_DEFAULT_CHUNK_SIZE
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancel()voidclear()protected voiddoOnCancel()booleanisCancelled()booleanisEmpty()voidonCompleted()voidonError(Throwable t)voidonNext(T t)protected voidonSubscribe(io.grpc.stub.CallStreamObserver<?> upstream)Tpoll()voidrequest(long n)intsize()voidsubscribe(org.reactivestreams.Subscriber<? super T> actual)-
Methods inherited from class com.salesforce.reactivegrpc.common.AbstractUnimplementedQueue
add, addAll, contains, containsAll, element, iterator, offer, offer, peek, remove, remove, removeAll, retainAll, toArray, toArray
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Collection
addAll, contains, containsAll, equals, hashCode, iterator, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
-
-
-
Field Detail
-
DEFAULT_CHUNK_SIZE
protected static final int DEFAULT_CHUNK_SIZE
- See Also:
- Constant Field Values
-
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)
-
onError
public void onError(Throwable t)
- Specified by:
onErrorin interfaceio.grpc.stub.StreamObserver<T>
-
onCompleted
public void onCompleted()
- Specified by:
onCompletedin interfaceio.grpc.stub.StreamObserver<T>
-
subscribe
public void subscribe(org.reactivestreams.Subscriber<? super T> actual)
- Specified by:
subscribein interfaceorg.reactivestreams.Publisher<T>
-
isCancelled
public boolean isCancelled()
-
request
public void request(long n)
- Specified by:
requestin interfaceorg.reactivestreams.Subscription
-
cancel
public void cancel()
- Specified by:
cancelin interfaceorg.reactivestreams.Subscription
-
doOnCancel
protected void doOnCancel()
-
poll
public T poll()
-
size
public int size()
- Specified by:
sizein interfaceCollection<T>- Overrides:
sizein classAbstractUnimplementedQueue<T>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceCollection<T>
-
clear
public void clear()
- Specified by:
clearin interfaceCollection<T>
-
-