Class AimdLimit
- All Implemented Interfaces:
RuntimeType.Api<AimdLimitConfig>, Limit, LimitAlgorithm, NamedService
The additive-increase/multiplicative-decrease (AIMD) algorithm is a feedback control algorithm best known for its use in TCP congestion control. AIMD combines linear growth of the congestion window when there is no congestion with an exponential reduction when congestion is detected.
-
Nested Class Summary
Nested classes/interfaces inherited from interface Limit
Limit.InitializationContextModifier and TypeInterfaceDescriptionstatic interfaceRuntime context used when initializing aLimit.Nested classes/interfaces inherited from interface LimitAlgorithm
LimitAlgorithm.Outcome, LimitAlgorithm.Result<T>, LimitAlgorithm.TokenModifier and TypeInterfaceDescriptionstatic interfaceRepresents the outcome of a limit algorithm decision.static interfaceCarrier for both the result of aCallablesubjected to concurrency limits and the correspondingLimitAlgorithm.Outcome.static interfaceWhen a token is retrieved fromLimitAlgorithm.tryAcquireOutcome(), one of its methods must be called when the task is over, to release the token back to the pool (such as a permit returned to aSemaphore). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault length of the queue.static final StringTimeout of a request that is enqueued. -
Method Summary
Modifier and TypeMethodDescriptionstatic AimdLimitConfig.Builderbuilder()Create a new fluent API builder to constructAimdLimitinstance.<T> LimitAlgorithm.Result<T> Invoke a callable within the limits of this limiter.copy()Create a copy of this limit with the same configuration.static AimdLimitcreate()Create a new instance with all defaults.static AimdLimitcreate(AimdLimitConfig config) Create a new instance from configuration.static AimdLimitCreate a new instance from configuration.static AimdLimitcreate(Consumer<AimdLimitConfig.Builder> consumer) Create a new instance customizing its configuration.voidinit(Limit.InitializationContext context) Initialization method for this limit.voidDeprecated.name()Name of this implementation, as provided inConfiguredProvider.create(Config, String).The prototype as it was received when creating this runtime object instance.Invoke a runnable within the limits of this limiter.tryAcquireOutcome(boolean wait) Tries to acquire a token, waiting for available permits for the configured amount of time ifwaitis enabled and the implementation supports queueing, returning immediately otherwise.type()Type of this implementation, to distinguish instances of same type, with differentNamedService.name().Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface LimitAlgorithm
tryAcquireOutcomeModifier and TypeMethodDescriptiondefault LimitAlgorithm.OutcomeTry to acquire a token, waiting for available permits for the configured amount of time, if queuing is enabled.
-
Field Details
-
DEFAULT_QUEUE_LENGTH
public static final int DEFAULT_QUEUE_LENGTHDefault length of the queue.- See Also:
-
DEFAULT_QUEUE_TIMEOUT_DURATION
Timeout of a request that is enqueued.- See Also:
-
-
Method Details
-
builder
Create a new fluent API builder to constructAimdLimitinstance.- Returns:
- fluent API builder
-
create
Create a new instance with all defaults.- Returns:
- a new limit instance
-
create
-
create
Create a new instance from configuration.- Parameters:
config- configuration of the AIMD limit- Returns:
- a new limit instance configured from
config
-
create
Create a new instance customizing its configuration.- Parameters:
consumer- consumer of configuration builder- Returns:
- a new limit instance configured from the builder
-
call
Description copied from interface:LimitAlgorithmInvoke a callable within the limits of this limiter.Custom implementations that override this method should:
- Obtain the outcome for the submitted work from
LimitAlgorithm.tryAcquireOutcome(boolean). - Invoke
LimitAlgorithm.Token.success(),LimitAlgorithm.Token.ignore(), orLimitAlgorithm.Token.dropped()on an accepted token to reflect the final state of the work item. - Throw
LimitExceptionif no token is available.
If the callable throws
IgnoreTaskException, callIgnoreTaskException.handle()after marking the token as ignored so the return value or wrapped exception is preserved.- Specified by:
callin interfaceLimitAlgorithm- Type Parameters:
T- the callable return type- Parameters:
callable- callable to execute within the limit- Returns:
- result of the callable with the outcome
- Throws:
Exception- in case the task failed with an exception
- Obtain the outcome for the submitted work from
-
run
Description copied from interface:LimitAlgorithmInvoke a runnable within the limits of this limiter.Custom implementations that override this method should:
- Obtain the outcome for the submitted work from
LimitAlgorithm.tryAcquireOutcome(boolean). - Invoke
LimitAlgorithm.Token.success(),LimitAlgorithm.Token.ignore(), orLimitAlgorithm.Token.dropped()on an accepted token to reflect the final state of the work item. - Throw
LimitExceptionif no token is available.
If the runnable throws
IgnoreTaskException, callIgnoreTaskException.handle()after marking the token as ignored so the wrapped exception is preserved when present.- Specified by:
runin interfaceLimitAlgorithm- Parameters:
runnable- runnable to execute within the limit- Returns:
Outcomefrom the limit algorithm- Throws:
Exception- in case the task failed with an exception
- Obtain the outcome for the submitted work from
-
tryAcquireOutcome
Description copied from interface:LimitAlgorithmTries to acquire a token, waiting for available permits for the configured amount of time ifwaitis enabled and the implementation supports queueing, returning immediately otherwise.If acquired, the caller must call one of the
LimitAlgorithm.Tokenoperations to release the token. If the response is rejected, the limit does not have an available token.- Specified by:
tryAcquireOutcomein interfaceLimitAlgorithm- Parameters:
wait- whether to wait in the queue (if one is configured/available in the limit), or to return immediately- Returns:
- outcome of the acquisition attempt
-
name
Description copied from interface:NamedServiceName of this implementation, as provided inConfiguredProvider.create(Config, String).- Specified by:
namein interfaceNamedService- Returns:
- name of this service
-
type
Description copied from interface:NamedServiceType of this implementation, to distinguish instances of same type, with differentNamedService.name(). Use for exampleConfiguredProvider.configKey()to define the type.- Specified by:
typein interfaceNamedService- Returns:
- type of this service
-
prototype
Description copied from interface:RuntimeType.ApiThe prototype as it was received when creating this runtime object instance.- Specified by:
prototypein interfaceRuntimeType.Api<AimdLimitConfig>- Returns:
- prototype object used to create this instance
-
copy
-
init
Description copied from interface:LimitInitialization method for this limit. This method can be used for any task, including metrics initialization. -
init
Deprecated.Description copied from interface:LimitInitialization method for this limit. This method can be used for any task, including metrics initialization.
-