Class FlowControlledBuffer<T,​R>

  • Type Parameters:
    T - the type of results this implementation buffers
    R - the type of message used for flow control in this buffer
    All Implemented Interfaces:
    io.grpc.stub.ClientResponseObserver<R,​T>, io.grpc.stub.StreamObserver<T>
    Direct Known Subclasses:
    AbstractBufferedStream, BufferedAggregateEventStream

    public abstract class FlowControlledBuffer<T,​R>
    extends FlowControlledStream<T,​R>
    Abstract implementation of the FlowControlledStream, adding buffering logic to the flow controlled stream.
    • Constructor Detail

      • FlowControlledBuffer

        public FlowControlledBuffer​(String clientId,
                                    int bufferSize,
                                    int refillBatch)
        Constructs a FlowControlledBuffer.
        Parameters:
        clientId - the client identifier which initiated this buffer
        bufferSize - the number of entries this buffer should hold
        refillBatch - the number of entries to be consumed prior to requesting new once
    • Method Detail

      • terminalMessage

        protected abstract T terminalMessage()
        Builds a terminal message of type T specifying when the this stream is closed.
        Returns:
        a terminal message of type T specifying when the this stream is closed
      • onNext

        public void onNext​(T value)
      • onError

        public void onError​(Throwable t)
      • onCompleted

        public void onCompleted()
      • close

        public void close()
      • getErrorResult

        public Throwable getErrorResult()
        Returns the error result, if any was recorded. This method may also yield a non-empty result when the buffer still contains messages for processing.
        Returns:
        the error result, if any was recorded, or else null
      • beforeStart

        public void beforeStart​(io.grpc.stub.ClientCallStreamObserver<R> requestStream)
        Specified by:
        beforeStart in interface io.grpc.stub.ClientResponseObserver<T,​R>
        Overrides:
        beforeStart in class FlowControlledStream<T,​R>
      • tryTakeNow

        protected T tryTakeNow()
        Try to retrieve an entry of type T from the buffer immediately. If none is present, null will be returned.
        Returns:
        an entry of type T from this buffer if present, otherwise null
      • tryTake

        protected T tryTake​(long timeout,
                            TimeUnit timeUnit)
                     throws InterruptedException
        Try to retrieve an entry of type T from the buffer, waiting for the duration of timeout in the given timeUnit. If none is present, null will be returned.
        Parameters:
        timeout - the duration to wait for an entry to become available in the buffer
        timeUnit - the TimeUnit used to specify the duration together with the timeout
        Returns:
        an entry of type T from this buffer if present, otherwise null. Timeouts will result in null as well.
        Throws:
        InterruptedException - while waiting for an entry to be taken
      • tryTake

        protected T tryTake​(long timeout,
                            TimeUnit timeUnit,
                            boolean exceptionOnTimeout)
                     throws InterruptedException
        Try to retrieve an entry of type T from the buffer, waiting for the duration of timeout in the given timeUnit. If none is present, null will be returned.
        Parameters:
        timeout - the duration to wait for an entry to become available in the buffer
        timeUnit - the TimeUnit used to specify the duration together with the timeout
        exceptionOnTimeout - Whether a TimeoutException should be thrown when the operation times out
        Returns:
        an entry of type T from this buffer if present, otherwise null
        Throws:
        InterruptedException - while waiting for an entry to be taken
        StreamTimeoutException - If there is no message available after waiting the allotted period
      • tryTake

        protected T tryTake()
                     throws InterruptedException
        Try to retrieve an entry of type T from the buffer, waiting indefinitely. If none is present because the buffer is closed, null will be returned.
        Returns:
        an entry of type T from this buffer if present, otherwise null
        Throws:
        InterruptedException - while waiting for an entry to be taken
      • take

        protected T take()
                  throws InterruptedException
        Take an entry of type T from the buffer, waiting indefinitely.
        Returns:
        an entry of type T from this buffer
        Throws:
        InterruptedException - while waiting for an entry to be taken
      • peek

        protected T peek()
        Retrieves, but does not remove, the first entry of this buffer, or returns null if the buffer is empty.
        Returns:
        the first entry of this buffer without removing it, or null if it is empty
      • isClosed

        protected boolean isClosed()
        Check whether this buffer has been closed off
        Returns:
        true if this buffer is closed, false otherwise