Top Description Fields Constructors Methods
io.undertow.conduits

public Class RateLimitingStreamSinkConduit

extends AbstractStreamSinkConduit<StreamSinkConduit>
Class Inheritance
Imports
org.xnio.channels.StreamSourceChannel, org.xnio.conduits.AbstractStreamSinkConduit, .StreamSinkConduit, java.io.IOException, .InterruptedIOException, java.nio.ByteBuffer, java.nio.channels.FileChannel, java.util.concurrent.TimeUnit, io.undertow.util.WorkerUtils

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

Modifier and TypeField and Description
private int
private final int
private long
private boolean
private long
private final long
private boolean

Constructor Summary

AccessConstructor and Description
public
RateLimitingStreamSinkConduit(StreamSinkConduit
The next conduit
next
,
int
The number of bytes that are allowed per time frame
bytes
,
long
The time frame
time
,
TimeUnit
The time unit
timeUnit
)

Method Summary

Modifier and TypeMethod and Description
public void
awaitWritable()

Overrides org.xnio.conduits.AbstractSinkConduit.awaitWritable.

Implements org.xnio.conduits.SinkConduit.awaitWritable.

Block until this channel becomes writable again.

public void
awaitWritable(long
the time to wait
time
,
TimeUnit
the time unit
timeUnit
)

Overrides org.xnio.conduits.AbstractSinkConduit.awaitWritable.

Implements org.xnio.conduits.SinkConduit.awaitWritable.

Block until this conduit becomes writable again, or until the timeout expires.

private boolean
private void
private void
handleWritten(long written)

public boolean
isWriteResumed()

Overrides org.xnio.conduits.AbstractSinkConduit.isWriteResumed.

Implements org.xnio.conduits.SinkConduit.isWriteResumed.

Determine whether write notifications are currently enabled.

public void
resumeWrites()

Overrides org.xnio.conduits.AbstractSinkConduit.resumeWrites.

Implements org.xnio.conduits.SinkConduit.resumeWrites.

Indicate that the conduit's WriteReadyHandler should be invoked as soon as data can be written without blocking.

public void
suspendWrites()

Overrides org.xnio.conduits.AbstractSinkConduit.suspendWrites.

Implements org.xnio.conduits.SinkConduit.suspendWrites.

Indicate that calling the conduit's WriteReadyHandler should be suspended.

public long
transferFrom(FileChannel
the file to read from
src
,
long
the position within the file from which the transfer is to begin
position
,
long
the number of bytes to be transferred
count
)

Overrides org.xnio.conduits.AbstractStreamSinkConduit.transferFrom.

Implements org.xnio.conduits.StreamSinkConduit.transferFrom.

Transfer bytes into this conduit from the given file.

public long
transferFrom(StreamSourceChannel
the source to read from
source
,
long
the number of bytes to be transferred
count
,
ByteBuffer
the buffer to copy through.
throughBuffer
)

Overrides org.xnio.conduits.AbstractStreamSinkConduit.transferFrom.

Implements org.xnio.conduits.StreamSinkConduit.transferFrom.

Transfers bytes from the given channel source.

public void
wakeupWrites()

Overrides org.xnio.conduits.AbstractSinkConduit.wakeupWrites.

Implements org.xnio.conduits.SinkConduit.wakeupWrites.

Indicate that the conduit's WriteReadyHandler should be invoked immediately, and then again as soon as data can be written without blocking.

public int
write(ByteBuffer
the buffer containing data to write
src
)

Overrides org.xnio.conduits.AbstractStreamSinkConduit.write.

Implements org.xnio.conduits.StreamSinkConduit.write.

Writes a sequence of bytes to this conduit from the given buffer.

public long
write(ByteBuffer[]
the buffers containing data to write
srcs
,
int
the offset into the buffer array
offs
,
int
the number of buffers to write
len
)

Overrides org.xnio.conduits.AbstractStreamSinkConduit.write.

Implements org.xnio.conduits.StreamSinkConduit.write.

Writes a sequence of bytes to this conduit from the given buffers.

public int
writeFinal(ByteBuffer
The data to write
src
)

Overrides org.xnio.conduits.AbstractStreamSinkConduit.writeFinal.

Implements org.xnio.conduits.StreamSinkConduit.writeFinal.

Writes some data to the conduit, with the same semantics as write(java.nio.ByteBuffer).

public long
writeFinal(ByteBuffer[]
The buffers from which bytes are to be retrieved
srcs
,
int
The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length
offs
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
len
)

Overrides org.xnio.conduits.AbstractStreamSinkConduit.writeFinal.

Implements org.xnio.conduits.StreamSinkConduit.writeFinal.

Writes some data to the conduit, with the same semantics as write(java.nio.ByteBuffer[], int, int).

Field Detail

byteCountback to summary
private int byteCount
bytesback to summary
private final int bytes
nextSendTimeback to summary
private long nextSendTime
scheduledback to summary
private boolean scheduled
startTimeback to summary
private long startTime
timeback to summary
private final long time
writesResumedback to summary
private boolean writesResumed

Constructor Detail

RateLimitingStreamSinkConduitback to summary
public RateLimitingStreamSinkConduit(StreamSinkConduit next, int bytes, long time, TimeUnit timeUnit)
Parameters
next:StreamSinkConduit

The next conduit

bytes:int

The number of bytes that are allowed per time frame

time:long

The time frame

timeUnit:TimeUnit

The time unit

Method Detail

awaitWritableback to summary
public void awaitWritable() throws IOException

Overrides org.xnio.conduits.AbstractSinkConduit.awaitWritable.

Implements org.xnio.conduits.SinkConduit.awaitWritable.

Doc from org.xnio.conduits.SinkConduit.awaitWritable.

Block until this channel becomes writable again. This method may return spuriously before the channel becomes writable.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
awaitWritableback to summary
public void awaitWritable(long time, TimeUnit timeUnit) throws IOException

Overrides org.xnio.conduits.AbstractSinkConduit.awaitWritable.

Implements org.xnio.conduits.SinkConduit.awaitWritable.

Doc from org.xnio.conduits.SinkConduit.awaitWritable.

Block until this conduit becomes writable again, or until the timeout expires. This method may return spuriously before the conduit becomes writable or the timeout expires.

Parameters
time:long

the time to wait

timeUnit:TimeUnit

the time unit

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
canSendback to summary
private boolean canSend()
handleWritesResumedWhenBlockedback to summary
private void handleWritesResumedWhenBlocked()
handleWrittenback to summary
private void handleWritten(long written)
isWriteResumedback to summary
public boolean isWriteResumed()

Overrides org.xnio.conduits.AbstractSinkConduit.isWriteResumed.

Implements org.xnio.conduits.SinkConduit.isWriteResumed.

Doc from org.xnio.conduits.SinkConduit.isWriteResumed.

Determine whether write notifications are currently enabled.

Returns:boolean

true if write notifications are enabled

Annotations
@Override
resumeWritesback to summary
public void resumeWrites()

Overrides org.xnio.conduits.AbstractSinkConduit.resumeWrites.

Implements org.xnio.conduits.SinkConduit.resumeWrites.

Doc from org.xnio.conduits.SinkConduit.resumeWrites.

Indicate that the conduit's WriteReadyHandler should be invoked as soon as data can be written without blocking.

Annotations
@Override
suspendWritesback to summary
public void suspendWrites()

Overrides org.xnio.conduits.AbstractSinkConduit.suspendWrites.

Implements org.xnio.conduits.SinkConduit.suspendWrites.

Doc from org.xnio.conduits.SinkConduit.suspendWrites.

Indicate that calling the conduit's WriteReadyHandler should be suspended.

Annotations
@Override
transferFromback to summary
public long transferFrom(FileChannel src, long position, long count) throws IOException

Overrides org.xnio.conduits.AbstractStreamSinkConduit.transferFrom.

Implements org.xnio.conduits.StreamSinkConduit.transferFrom.

Doc from org.xnio.conduits.StreamSinkConduit.transferFrom.

Transfer bytes into this conduit from the given file.

Parameters
src:FileChannel

the file to read from

position:long

the position within the file from which the transfer is to begin

count:long

the number of bytes to be transferred

Returns:long

the number of bytes (possibly 0) that were actually transferred

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
transferFromback to summary
public long transferFrom(StreamSourceChannel source, long count, ByteBuffer throughBuffer) throws IOException

Overrides org.xnio.conduits.AbstractStreamSinkConduit.transferFrom.

Implements org.xnio.conduits.StreamSinkConduit.transferFrom.

Doc from org.xnio.conduits.StreamSinkConduit.transferFrom.

Transfers bytes from the given channel source. On entry, throughBuffer will be cleared. On exit, the buffer will be flipped for emptying, and may be empty or may contain data. If this method returns a value less than count, then the remaining data in throughBuffer may contain data read from source which must be written to this channel to complete the operation.

Parameters
source:StreamSourceChannel

the source to read from

count:long

the number of bytes to be transferred

throughBuffer:ByteBuffer

the buffer to copy through.

Returns:long

the number of bytes (possibly 0) that were actually transferred, or -1 if the end of input was reached

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
wakeupWritesback to summary
public void wakeupWrites()

Overrides org.xnio.conduits.AbstractSinkConduit.wakeupWrites.

Implements org.xnio.conduits.SinkConduit.wakeupWrites.

Doc from org.xnio.conduits.SinkConduit.wakeupWrites.

Indicate that the conduit's WriteReadyHandler should be invoked immediately, and then again as soon as data can be written without blocking.

Annotations
@Override
writeback to summary
public int write(ByteBuffer src) throws IOException

Overrides org.xnio.conduits.AbstractStreamSinkConduit.write.

Implements org.xnio.conduits.StreamSinkConduit.write.

Doc from org.xnio.conduits.StreamSinkConduit.write.

Writes a sequence of bytes to this conduit from the given buffer.

Parameters
src:ByteBuffer

the buffer containing data to write

Returns:int

the number of bytes written, possibly 0

Annotations
@Override
Exceptions
IOException:
if an error occurs
writeback to summary
public long write(ByteBuffer[] srcs, int offs, int len) throws IOException

Overrides org.xnio.conduits.AbstractStreamSinkConduit.write.

Implements org.xnio.conduits.StreamSinkConduit.write.

Doc from org.xnio.conduits.StreamSinkConduit.write.

Writes a sequence of bytes to this conduit from the given buffers.

Parameters
srcs:ByteBuffer[]

the buffers containing data to write

offs:int

the offset into the buffer array

len:int

the number of buffers to write

Returns:long

the number of bytes written, possibly 0

Annotations
@Override
Exceptions
IOException:
if an error occurs
writeFinalback to summary
public int writeFinal(ByteBuffer src) throws IOException

Overrides org.xnio.conduits.AbstractStreamSinkConduit.writeFinal.

Implements org.xnio.conduits.StreamSinkConduit.writeFinal.

Doc from org.xnio.conduits.StreamSinkConduit.writeFinal.

Writes some data to the conduit, with the same semantics as write(java.nio.ByteBuffer). If all the data is written out then the conduit will have its writes terminated. Semantically this method is equivalent to: int rem = src.remaining(); int written = conduit.write(src); if(written == rem) { conduit.terminateWrites() }

Parameters
src:ByteBuffer

The data to write

Returns:int

The amount of data that was actually written.

Annotations
@Override
writeFinalback to summary
public long writeFinal(ByteBuffer[] srcs, int offs, int len) throws IOException

Overrides org.xnio.conduits.AbstractStreamSinkConduit.writeFinal.

Implements org.xnio.conduits.StreamSinkConduit.writeFinal.

Doc from org.xnio.conduits.StreamSinkConduit.writeFinal.

Writes some data to the conduit, with the same semantics as write(java.nio.ByteBuffer[], int, int). If all the data is written out then the conduit will have its writes terminated.

Parameters
srcs:ByteBuffer[]

The buffers from which bytes are to be retrieved

offs:int

The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length

len:int

The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset

Returns:long

The amount of data that was actually written

Annotations
@Override