public class ReadThrottleFilter extends IoFilterAdapter
IoFilter
that throttles incoming traffic to
prevent a unwanted OutOfMemoryError
under heavy load.
This filter will automatically disable reads on an IoSession
once
the amount of the read data batched for that session in the ExecutorFilter
reaches a defined threshold. It accomplishes this by adding one filter before the
ExecutorFilter
.
The size of the received data is calculated by MessageSizeEstimator
.
If you are using a transport whose envelope is not an IoBuffer
,
you could write your own MessageSizeEstimator
for better traffic
calculation. However, the DefaultMessageSizeEstimator
will suffice
in most cases.
It is recommended to add this filter at the end of your filter chain
configuration because it is possible to subvert the behavior of the added
filters by adding a filter immediately before/after the ExecutorFilter
after inserting this builder, consequently leading to a unexpected behavior.
IoFilter.NextFilter
Constructor and Description |
---|
ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor)
Creates a new instance with 64KB maxSessionBufferSize,
128MB maxGlobalBufferSize and a new
DefaultMessageSizeEstimator . |
ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor,
int maxSessionBufferSize,
int maxServiceBufferSize,
int maxGlobalBufferSize)
Creates a new instance with the specified maxSessionBufferSize,
maxGlobalBufferSize and a new
DefaultMessageSizeEstimator . |
ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor,
ReadThrottlePolicy policy) |
ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor,
ReadThrottlePolicy policy,
int maxSessionBufferSize,
int maxServiceBufferSize,
int maxGlobalBufferSize) |
ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor,
ReadThrottlePolicy policy,
MessageSizeEstimator messageSizeEstimator) |
ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor,
ReadThrottlePolicy policy,
MessageSizeEstimator messageSizeEstimator,
int maxSessionBufferSize,
int maxServiceBufferSize,
int maxGlobalBufferSize)
Creates a new instance with the specified maxSessionBufferSize,
maxGlobalBufferSize and
MessageSizeEstimator . |
Modifier and Type | Method and Description |
---|---|
void |
filterSetTrafficMask(IoFilter.NextFilter nextFilter,
IoSession session,
TrafficMask trafficMask)
Filters
IoSession.setTrafficMask(TrafficMask) method invocation. |
static int |
getGlobalBufferSize()
Returns the current amount of data in the buffer of the
ExecutorFilter
for all IoSession whose IoFilterChain has been configured by
this builder. |
int |
getMaxGlobalBufferSize()
Returns the maximum amount of data in the buffer of the
ExecutorFilter
for all IoSession whose IoFilterChain has been configured by
this builder. |
int |
getMaxServiceBufferSize() |
int |
getMaxSessionBufferSize()
Returns the maximum amount of data in the buffer of the
ExecutorFilter
per IoSession . |
MessageSizeEstimator |
getMessageSizeEstimator()
Returns the size estimator currently in use.
|
ReadThrottlePolicy |
getPolicy() |
static int |
getServiceBufferSize(IoService service) |
int |
getSessionBufferSize(IoSession session)
Returns the current amount of data in the buffer of the
ExecutorFilter
for the specified IoSession . |
void |
messageReceived(IoFilter.NextFilter nextFilter,
IoSession session,
java.lang.Object message)
Filters
IoHandler.messageReceived(IoSession,Object)
event. |
void |
onPostAdd(IoFilterChain parent,
java.lang.String name,
IoFilter.NextFilter nextFilter)
Invoked after this filter is added to the specified parent.
|
void |
onPostRemove(IoFilterChain parent,
java.lang.String name,
IoFilter.NextFilter nextFilter)
Invoked after this filter is removed from the specified parent.
|
void |
onPreAdd(IoFilterChain parent,
java.lang.String name,
IoFilter.NextFilter nextFilter)
Invoked before this filter is added to the specified parent.
|
void |
setMaxGlobalBufferSize(int maxGlobalBufferSize)
Sets the maximum amount of data in the buffer of the
ExecutorFilter
for all IoSession whose IoFilterChain has been configured by
this builder. |
void |
setMaxServiceBufferSize(int maxServiceBufferSize) |
void |
setMaxSessionBufferSize(int maxSessionBufferSize)
Sets the maximum amount of data in the buffer of the
ExecutorFilter
per IoSession . |
void |
setPolicy(ReadThrottlePolicy policy) |
java.lang.String |
toString() |
destroy, exceptionCaught, filterClose, filterWrite, init, messageSent, onPreRemove, sessionClosed, sessionCreated, sessionIdle, sessionOpened
public ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor)
DefaultMessageSizeEstimator
.public ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor, ReadThrottlePolicy policy)
public ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor, ReadThrottlePolicy policy, MessageSizeEstimator messageSizeEstimator)
public ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor, int maxSessionBufferSize, int maxServiceBufferSize, int maxGlobalBufferSize)
DefaultMessageSizeEstimator
.public ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor, ReadThrottlePolicy policy, int maxSessionBufferSize, int maxServiceBufferSize, int maxGlobalBufferSize)
public ReadThrottleFilter(java.util.concurrent.ScheduledExecutorService executor, ReadThrottlePolicy policy, MessageSizeEstimator messageSizeEstimator, int maxSessionBufferSize, int maxServiceBufferSize, int maxGlobalBufferSize)
MessageSizeEstimator
.maxSessionBufferSize
- the maximum amount of data in the buffer of
the ExecutorFilter
per IoSession
.
Specify 0
or a smaller value to disable.maxGlobalBufferSize
- the maximum amount of data in the buffer of
the ExecutorFilter
for all IoSession
whose IoFilterChain
has been configured by
this builder.
Specify 0
or a smaller value to disable.messageSizeEstimator
- the message size estimator. If null
,
a new DefaultMessageSizeEstimator
is created.public static int getGlobalBufferSize()
ExecutorFilter
for all IoSession
whose IoFilterChain
has been configured by
this builder.public static int getServiceBufferSize(IoService service)
public ReadThrottlePolicy getPolicy()
public void setPolicy(ReadThrottlePolicy policy)
public int getMaxSessionBufferSize()
ExecutorFilter
per IoSession
. 0
means 'disabled'.public int getMaxServiceBufferSize()
public int getMaxGlobalBufferSize()
ExecutorFilter
for all IoSession
whose IoFilterChain
has been configured by
this builder. 0
means 'disabled'.public void setMaxSessionBufferSize(int maxSessionBufferSize)
ExecutorFilter
per IoSession
. Specify 0
or a smaller value to disable.public void setMaxServiceBufferSize(int maxServiceBufferSize)
public void setMaxGlobalBufferSize(int maxGlobalBufferSize)
ExecutorFilter
for all IoSession
whose IoFilterChain
has been configured by
this builder. Specify 0
or a smaller value to disable.public MessageSizeEstimator getMessageSizeEstimator()
public int getSessionBufferSize(IoSession session)
ExecutorFilter
for the specified IoSession
.public void onPreAdd(IoFilterChain parent, java.lang.String name, IoFilter.NextFilter nextFilter) throws java.lang.Exception
IoFilter
IoFilter.init()
is invoked.onPreAdd
in interface IoFilter
onPreAdd
in class IoFilterAdapter
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.java.lang.Exception
public void onPostAdd(IoFilterChain parent, java.lang.String name, IoFilter.NextFilter nextFilter) throws java.lang.Exception
IoFilter
IoFilter.init()
is invoked.onPostAdd
in interface IoFilter
onPostAdd
in class IoFilterAdapter
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.java.lang.Exception
public void onPostRemove(IoFilterChain parent, java.lang.String name, IoFilter.NextFilter nextFilter) throws java.lang.Exception
IoFilter
IoFilter.destroy()
is invoked.onPostRemove
in interface IoFilter
onPostRemove
in class IoFilterAdapter
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.java.lang.Exception
public void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Object message) throws java.lang.Exception
IoFilter
IoHandler.messageReceived(IoSession,Object)
event.messageReceived
in interface IoFilter
messageReceived
in class IoFilterAdapter
java.lang.Exception
public void filterSetTrafficMask(IoFilter.NextFilter nextFilter, IoSession session, TrafficMask trafficMask) throws java.lang.Exception
IoFilter
IoSession.setTrafficMask(TrafficMask)
method invocation.filterSetTrafficMask
in interface IoFilter
filterSetTrafficMask
in class IoFilterAdapter
java.lang.Exception
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2004-2019 Apache MINA Project. All Rights Reserved.