Class FifoBuffer

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class FifoBuffer
    extends Object
    implements Closeable
    FIFO buffer for a reader thread and a writer thread to collaborate. Unlike a ring buffer, which uses a fixed memory regardless of the number of bytes currently in the buffer, this implementation uses a single linked list to reduce the memory footprint when the reader closely follows the writer, regardless of the capacity limit set in the constructor. In trilead, the writer puts the data we receive from the network, and the user code acts as a reader. A user code normally drains the buffer more quickly than what the network delivers, so this implementation saves memory while simultaneously allowing us to advertise a bigger window size for a large latency network.
    Kohsuke Kawaguchi
    • Constructor Detail

      • FifoBuffer

        public FifoBuffer​(int pageSize,
                          int limit)
      • FifoBuffer

        public FifoBuffer​(int limit)
      • FifoBuffer

        public FifoBuffer​(Object lock,
                          int pageSize,
                          int limit)
    • Method Detail

      • setLimit

        public void setLimit​(int newLimit)
        Set limit to the number of maximum bytes this buffer can hold. Write methods will block if the size reaches the limit.
      • readable

        public int readable()
        Number of bytes available in this buffer that are readable.
        -1 if the buffer is closed and there's no more data to read. May return non-negative value if the buffer close is requested, but not performed
      • writable

        public int writable()
        Number of bytes writable.
        Number of bytes we can write to the buffer. If the buffer is closed, may return the value beyond the limit (JENKINS-37514)
      • isClosed

        public boolean isClosed()
        Returns true if the write end of the buffer is already closed, and that no more new data will arrive. Note that there still might be a data left in the buffer to be read. The method may also return true when the close operation is actually requested.
      • getCloseCause

        public FifoBuffer.CloseCause getCloseCause()
        Returns Exception with stacktrace of the command, which invoked the buffer close.
        Close cause or null
      • send

        public int send​(WritableByteChannel ch)
                 throws IOException
        Read from this buffer write as much as possible to the channel until the channel gets filled up.
        number of bytes that were written. -1 if this buffer is EOF-ed and there will never be any more data to write.
      • writeNonBlock

        public int writeNonBlock​(ByteBuffer buf)
        Non-blocking write.
        Number of writes written, possibly 0.
      • receive

        public int receive​(ReadableByteChannel ch)
                    throws IOException
        Read bytes from a channel and stores it into this buffer.
        number of bytes read, or -1 if the given channel has reached EOF and no further read is possible.
        IOException - receive error
      • close

        public void close()
        Indicates that there will be no more write. Once the remaining bytes are drained by the reader, the read method will start returning EOF signals.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
      • peek

        public int peek​(int offset,
                        byte[] data,
                        int start,
                        int len)
        Peek the specified number of bytes (len) at the specified offset in this buffer (offset) and places it into the specified position (start) of the array (data)
        number of bytes actually peeked. Can be 0 if the offset goes beyond the current readable size in this buffer. Never negative.
      • peek

        public int peek​(int offset,
                        byte[] data)
      • readNonBlocking

        public int readNonBlocking​(byte[] buf)