Class DefaultHttp2LocalFlowController
- All Implemented Interfaces:
Http2FlowController
,Http2LocalFlowController
Http2LocalFlowController
.
This class is NOT thread safe. The assumption is all methods must be invoked from a single thread.
Typically this thread is the event loop thread for the ChannelHandlerContext
managed by this class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class
Flow control state that does autorefill of the flow control window when the data is received.private class
Flow control window state for an individual stream.private static interface
An abstraction which provides specific extensions used by local flow control.private final class
Provides a means to iterate over all active streams and increment the flow control windows. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Http2Connection
private ChannelHandlerContext
static final float
The default ratio of window size to initial window size below which aWINDOW_UPDATE
is sent to expand the window.private Http2FrameWriter
private int
private static final DefaultHttp2LocalFlowController.FlowState
The local flow control state for a single stream that is not in a state where flow controlled frames cannot be exchanged.private final Http2Connection.PropertyKey
private float
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultHttp2LocalFlowController
(Http2Connection connection) DefaultHttp2LocalFlowController
(Http2Connection connection, float windowUpdateRatio, boolean autoRefillConnectionWindow) Constructs a controller with the given settings. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Set theChannelHandlerContext
for which to apply flow control on.private static void
checkValidRatio
(float ratio) private boolean
consumeAllBytes
(DefaultHttp2LocalFlowController.FlowState state, int numBytes) boolean
consumeBytes
(Http2Stream stream, int numBytes) Indicates that the application has consumed a number of bytes for the given stream and is therefore ready to receive more data from the remote endpoint.frameWriter
(Http2FrameWriter frameWriter) Sets the writer to be use for sendingWINDOW_UPDATE
frames.void
incrementWindowSize
(Http2Stream stream, int delta) Increments the size of the stream's flow control window by the given delta.int
Gets the connection-wide initial flow control window size that is used as the basis for new stream flow control windows.void
initialWindowSize
(int newWindowSize) Sets the connection-wide initial flow control window and updates all stream windows (but not the connection stream window) by the delta.int
initialWindowSize
(Http2Stream stream) Get the initial flow control window size for the given stream.private static boolean
isClosed
(Http2Stream stream) void
receiveFlowControlledFrame
(Http2Stream stream, ByteBuf data, int padding, boolean endOfStream) Receives an inboundDATA
frame from the remote endpoint and applies flow control policies to it for both thestream
as well as the connection.state
(Http2Stream stream) int
unconsumedBytes
(Http2Stream stream) The number of bytes for the given stream that have been received but not yet consumed by the application.int
windowSize
(Http2Stream stream) Get the portion of the flow control window for the given stream that is currently available for sending/receiving frames which are subject to flow control.float
The window update ratio is used to determine when a window update must be sent.void
windowUpdateRatio
(float ratio) The window update ratio is used to determine when a window update must be sent.float
windowUpdateRatio
(Http2Stream stream) The window update ratio is used to determine when a window update must be sent.void
windowUpdateRatio
(Http2Stream stream, float ratio) The window update ratio is used to determine when a window update must be sent.
-
Field Details
-
DEFAULT_WINDOW_UPDATE_RATIO
public static final float DEFAULT_WINDOW_UPDATE_RATIOThe default ratio of window size to initial window size below which aWINDOW_UPDATE
is sent to expand the window.- See Also:
-
connection
-
stateKey
-
frameWriter
-
ctx
-
windowUpdateRatio
private float windowUpdateRatio -
initialWindowSize
private int initialWindowSize -
REDUCED_FLOW_STATE
The local flow control state for a single stream that is not in a state where flow controlled frames cannot be exchanged.
-
-
Constructor Details
-
DefaultHttp2LocalFlowController
-
DefaultHttp2LocalFlowController
public DefaultHttp2LocalFlowController(Http2Connection connection, float windowUpdateRatio, boolean autoRefillConnectionWindow) Constructs a controller with the given settings.- Parameters:
connection
- the connection state.windowUpdateRatio
- the window percentage below which to send aWINDOW_UPDATE
.autoRefillConnectionWindow
- iftrue
, effectively disables the connection window in the flow control algorithm as they will always refill automatically without requiring the application to consume the bytes. When enabled, the maximum bytes you must be prepared to queue is proportional tomaximum number of concurrent streams * the initial window size per stream
(SETTINGS_MAX_CONCURRENT_STREAMS SETTINGS_INITIAL_WINDOW_SIZE).
-
-
Method Details
-
frameWriter
Description copied from interface:Http2LocalFlowController
Sets the writer to be use for sendingWINDOW_UPDATE
frames. This must be called before any flow controlled data is received.- Specified by:
frameWriter
in interfaceHttp2LocalFlowController
- Parameters:
frameWriter
- the HTTP/2 frame writer.
-
channelHandlerContext
Description copied from interface:Http2FlowController
Set theChannelHandlerContext
for which to apply flow control on.This must be called to properly initialize the
Http2FlowController
. Not calling this is considered a programming error.- Specified by:
channelHandlerContext
in interfaceHttp2FlowController
- Parameters:
ctx
- TheChannelHandlerContext
for which to apply flow control on.
-
initialWindowSize
Description copied from interface:Http2FlowController
Sets the connection-wide initial flow control window and updates all stream windows (but not the connection stream window) by the delta.Represents the value for SETTINGS_INITIAL_WINDOW_SIZE. This method should only be called by Netty (not users) as a result of a receiving a
SETTINGS
frame.- Specified by:
initialWindowSize
in interfaceHttp2FlowController
- Parameters:
newWindowSize
- the new initial window size.- Throws:
Http2Exception
- thrown if any protocol-related error occurred.
-
initialWindowSize
public int initialWindowSize()Description copied from interface:Http2FlowController
Gets the connection-wide initial flow control window size that is used as the basis for new stream flow control windows.Represents the value for SETTINGS_INITIAL_WINDOW_SIZE. The initial value returned by this method must be
Http2CodecUtil.DEFAULT_WINDOW_SIZE
.- Specified by:
initialWindowSize
in interfaceHttp2FlowController
-
windowSize
Description copied from interface:Http2FlowController
Get the portion of the flow control window for the given stream that is currently available for sending/receiving frames which are subject to flow control. This quantity is measured in number of bytes.- Specified by:
windowSize
in interfaceHttp2FlowController
-
initialWindowSize
Description copied from interface:Http2LocalFlowController
Get the initial flow control window size for the given stream. This quantity is measured in number of bytes. Note the unavailable window portion can be calculated byHttp2FlowController.initialWindowSize()
-Http2FlowController.windowSize(Http2Stream)
.- Specified by:
initialWindowSize
in interfaceHttp2LocalFlowController
-
incrementWindowSize
Description copied from interface:Http2FlowController
Increments the size of the stream's flow control window by the given delta.In the case of a
Http2RemoteFlowController
this is called upon receipt of aWINDOW_UPDATE
frame from the remote endpoint to mirror the changes to the window size.For a
Http2LocalFlowController
this can be called to request the expansion of the window size published by this endpoint. It is up to the implementation, however, as to when aWINDOW_UPDATE
is actually sent.- Specified by:
incrementWindowSize
in interfaceHttp2FlowController
- Parameters:
stream
- The subject stream. UseHttp2Connection.connectionStream()
for requesting the size of the connection window.delta
- the change in size of the flow control window.- Throws:
Http2Exception
- thrown if a protocol-related error occurred.
-
consumeBytes
Description copied from interface:Http2LocalFlowController
Indicates that the application has consumed a number of bytes for the given stream and is therefore ready to receive more data from the remote endpoint. The application must consume any bytes that it receives or the flow control window will collapse. Consuming bytes enables the flow controller to sendWINDOW_UPDATE
to restore a portion of the flow control window for the stream. Ifstream
isnull
or closed (i.e.Http2Stream.state()
method returnsHttp2Stream.State.CLOSED
), calling this method has no effect.- Specified by:
consumeBytes
in interfaceHttp2LocalFlowController
- Parameters:
stream
- the stream for which window space should be freed. The connection stream object must not be used. Ifstream
isnull
or closed (i.e.Http2Stream.state()
method returnsHttp2Stream.State.CLOSED
), calling this method has no effect.numBytes
- the number of bytes to be returned to the flow control window.- Returns:
- true if a
WINDOW_UPDATE
was sent, false otherwise. - Throws:
Http2Exception
- if the number of bytes returned exceeds theHttp2LocalFlowController.unconsumedBytes(Http2Stream)
for the stream.
-
consumeAllBytes
private boolean consumeAllBytes(DefaultHttp2LocalFlowController.FlowState state, int numBytes) throws Http2Exception - Throws:
Http2Exception
-
unconsumedBytes
Description copied from interface:Http2LocalFlowController
The number of bytes for the given stream that have been received but not yet consumed by the application.- Specified by:
unconsumedBytes
in interfaceHttp2LocalFlowController
- Parameters:
stream
- the stream for which window space should be freed.- Returns:
- the number of unconsumed bytes for the stream.
-
checkValidRatio
private static void checkValidRatio(float ratio) -
windowUpdateRatio
public void windowUpdateRatio(float ratio) The window update ratio is used to determine when a window update must be sent. If the ratio of bytes processed since the last update has meet or exceeded this ratio then a window update will be sent. This is the global window update ratio that will be used for new streams.- Parameters:
ratio
- the ratio to use when checking if aWINDOW_UPDATE
is determined necessary for new streams.- Throws:
IllegalArgumentException
- If the ratio is out of bounds (0, 1).
-
windowUpdateRatio
public float windowUpdateRatio()The window update ratio is used to determine when a window update must be sent. If the ratio of bytes processed since the last update has meet or exceeded this ratio then a window update will be sent. This is the global window update ratio that will be used for new streams. -
windowUpdateRatio
The window update ratio is used to determine when a window update must be sent. If the ratio of bytes processed since the last update has meet or exceeded this ratio then a window update will be sent. This window update ratio will only be applied tostreamId
.Note it is the responsibly of the caller to ensure that the initial
SETTINGS
frame is sent before this is called. It would be considered aHttp2Error.PROTOCOL_ERROR
if aWINDOW_UPDATE
was generated by this method before the initialSETTINGS
frame is sent.- Parameters:
stream
- the stream for whichratio
applies to.ratio
- the ratio to use when checking if aWINDOW_UPDATE
is determined necessary.- Throws:
Http2Exception
- If a protocol-error occurs while generatingWINDOW_UPDATE
frames
-
windowUpdateRatio
The window update ratio is used to determine when a window update must be sent. If the ratio of bytes processed since the last update has meet or exceeded this ratio then a window update will be sent. This window update ratio will only be applied tostreamId
.- Throws:
Http2Exception
- If no stream corresponding tostream
could be found.
-
receiveFlowControlledFrame
public void receiveFlowControlledFrame(Http2Stream stream, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception Description copied from interface:Http2LocalFlowController
Receives an inboundDATA
frame from the remote endpoint and applies flow control policies to it for both thestream
as well as the connection. If any flow control policies have been violated, an exception is raised immediately, otherwise the frame is considered to have "passed" flow control. Ifstream
isnull
or closed, flow control should only be applied to the connection window and the bytes are immediately consumed.- Specified by:
receiveFlowControlledFrame
in interfaceHttp2LocalFlowController
- Parameters:
stream
- the subject stream for the received frame. The connection stream object must not be used. Ifstream
isnull
or closed, flow control should only be applied to the connection window and the bytes are immediately consumed.data
- payload buffer for the frame.padding
- additional bytes that should be added to obscure the true content size. Must be between 0 and 256 (inclusive).endOfStream
- Indicates whether this is the last frame to be sent from the remote endpoint for this stream.- Throws:
Http2Exception
- if any flow control errors are encountered.
-
connectionState
-
state
-
isClosed
-