Class RateLimitingStreamSinkConduit

  • All Implemented Interfaces:
    org.xnio.conduits.Conduit, org.xnio.conduits.SinkConduit, org.xnio.conduits.StreamSinkConduit

    public class RateLimitingStreamSinkConduit
    extends org.xnio.conduits.AbstractStreamSinkConduit<org.xnio.conduits.StreamSinkConduit>
    Class that implements the token bucket algorithm.

    Allows send speed to be throttled

    Note that throttling is applied after an initial write, so if a big write is performed initially it may be a while before it can write again.

    Author:
    Stuart Douglas
    • Field Summary

      • Fields inherited from class org.xnio.conduits.AbstractConduit

        next
    • Constructor Summary

      Constructors 
      Constructor Description
      RateLimitingStreamSinkConduit​(org.xnio.conduits.StreamSinkConduit next, int bytes, long time, java.util.concurrent.TimeUnit timeUnit)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void awaitWritable()  
      void awaitWritable​(long time, java.util.concurrent.TimeUnit timeUnit)  
      boolean isWriteResumed()  
      void resumeWrites()  
      void suspendWrites()  
      long transferFrom​(java.nio.channels.FileChannel src, long position, long count)  
      long transferFrom​(org.xnio.channels.StreamSourceChannel source, long count, java.nio.ByteBuffer throughBuffer)  
      void wakeupWrites()  
      int write​(java.nio.ByteBuffer src)  
      long write​(java.nio.ByteBuffer[] srcs, int offs, int len)  
      int writeFinal​(java.nio.ByteBuffer src)  
      long writeFinal​(java.nio.ByteBuffer[] srcs, int offs, int len)  
      • Methods inherited from class org.xnio.conduits.AbstractSinkConduit

        flush, getWriteThread, isWriteShutdown, setWriteReadyHandler, terminateWrites, truncateWrites
      • Methods inherited from class org.xnio.conduits.AbstractConduit

        getWorker
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface org.xnio.conduits.Conduit

        getWorker
      • Methods inherited from interface org.xnio.conduits.SinkConduit

        flush, getWriteThread, isWriteShutdown, setWriteReadyHandler, terminateWrites, truncateWrites
    • Constructor Detail

      • RateLimitingStreamSinkConduit

        public RateLimitingStreamSinkConduit​(org.xnio.conduits.StreamSinkConduit next,
                                             int bytes,
                                             long time,
                                             java.util.concurrent.TimeUnit timeUnit)
        Parameters:
        next - The next conduit
        bytes - The number of bytes that are allowed per time frame
        time - The time frame
        timeUnit - The time unit
    • Method Detail

      • write

        public int write​(java.nio.ByteBuffer src)
                  throws java.io.IOException
        Specified by:
        write in interface org.xnio.conduits.StreamSinkConduit
        Overrides:
        write in class org.xnio.conduits.AbstractStreamSinkConduit<org.xnio.conduits.StreamSinkConduit>
        Throws:
        java.io.IOException
      • transferFrom

        public long transferFrom​(java.nio.channels.FileChannel src,
                                 long position,
                                 long count)
                          throws java.io.IOException
        Specified by:
        transferFrom in interface org.xnio.conduits.StreamSinkConduit
        Overrides:
        transferFrom in class org.xnio.conduits.AbstractStreamSinkConduit<org.xnio.conduits.StreamSinkConduit>
        Throws:
        java.io.IOException
      • transferFrom

        public long transferFrom​(org.xnio.channels.StreamSourceChannel source,
                                 long count,
                                 java.nio.ByteBuffer throughBuffer)
                          throws java.io.IOException
        Specified by:
        transferFrom in interface org.xnio.conduits.StreamSinkConduit
        Overrides:
        transferFrom in class org.xnio.conduits.AbstractStreamSinkConduit<org.xnio.conduits.StreamSinkConduit>
        Throws:
        java.io.IOException
      • write

        public long write​(java.nio.ByteBuffer[] srcs,
                          int offs,
                          int len)
                   throws java.io.IOException
        Specified by:
        write in interface org.xnio.conduits.StreamSinkConduit
        Overrides:
        write in class org.xnio.conduits.AbstractStreamSinkConduit<org.xnio.conduits.StreamSinkConduit>
        Throws:
        java.io.IOException
      • writeFinal

        public int writeFinal​(java.nio.ByteBuffer src)
                       throws java.io.IOException
        Specified by:
        writeFinal in interface org.xnio.conduits.StreamSinkConduit
        Overrides:
        writeFinal in class org.xnio.conduits.AbstractStreamSinkConduit<org.xnio.conduits.StreamSinkConduit>
        Throws:
        java.io.IOException
      • writeFinal

        public long writeFinal​(java.nio.ByteBuffer[] srcs,
                               int offs,
                               int len)
                        throws java.io.IOException
        Specified by:
        writeFinal in interface org.xnio.conduits.StreamSinkConduit
        Overrides:
        writeFinal in class org.xnio.conduits.AbstractStreamSinkConduit<org.xnio.conduits.StreamSinkConduit>
        Throws:
        java.io.IOException
      • resumeWrites

        public void resumeWrites()
        Specified by:
        resumeWrites in interface org.xnio.conduits.SinkConduit
        Overrides:
        resumeWrites in class org.xnio.conduits.AbstractSinkConduit<org.xnio.conduits.StreamSinkConduit>
      • suspendWrites

        public void suspendWrites()
        Specified by:
        suspendWrites in interface org.xnio.conduits.SinkConduit
        Overrides:
        suspendWrites in class org.xnio.conduits.AbstractSinkConduit<org.xnio.conduits.StreamSinkConduit>
      • wakeupWrites

        public void wakeupWrites()
        Specified by:
        wakeupWrites in interface org.xnio.conduits.SinkConduit
        Overrides:
        wakeupWrites in class org.xnio.conduits.AbstractSinkConduit<org.xnio.conduits.StreamSinkConduit>
      • isWriteResumed

        public boolean isWriteResumed()
        Specified by:
        isWriteResumed in interface org.xnio.conduits.SinkConduit
        Overrides:
        isWriteResumed in class org.xnio.conduits.AbstractSinkConduit<org.xnio.conduits.StreamSinkConduit>
      • awaitWritable

        public void awaitWritable()
                           throws java.io.IOException
        Specified by:
        awaitWritable in interface org.xnio.conduits.SinkConduit
        Overrides:
        awaitWritable in class org.xnio.conduits.AbstractSinkConduit<org.xnio.conduits.StreamSinkConduit>
        Throws:
        java.io.IOException
      • awaitWritable

        public void awaitWritable​(long time,
                                  java.util.concurrent.TimeUnit timeUnit)
                           throws java.io.IOException
        Specified by:
        awaitWritable in interface org.xnio.conduits.SinkConduit
        Overrides:
        awaitWritable in class org.xnio.conduits.AbstractSinkConduit<org.xnio.conduits.StreamSinkConduit>
        Throws:
        java.io.IOException