Class FixedLimit
- All Implemented Interfaces:
RuntimeType.Api<FixedLimitConfig>, Limit, LimitAlgorithm, NamedService
- See Also:
-
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 limit, meaning unlimited execution.static final intDefault length of the queue.static final StringTimeout of a request that is enqueued. -
Method Summary
Modifier and TypeMethodDescriptionstatic FixedLimitConfig.Builderbuilder()Create a new fluent API builder to constructFixedLimitinstance.<T> LimitAlgorithm.Result<T> Invoke a callable within the limits of this limiter.protected AtomicIntegerReturns theAtomicIntegerinstance tracking the current number of concurrent requests.copy()Create a copy of this limit with the same configuration.static FixedLimitcreate()Create a new instance with all defaults (no limit).static FixedLimitcreate(FixedLimitConfig config) Create a new instance from configuration.static FixedLimitCreate a new instance from configuration.static FixedLimitCreate an instance from the provided semaphore.static FixedLimitcreate(Consumer<FixedLimitConfig.Builder> consumer) Create a new instance customizing its configuration.protected LimitAlgorithm.OutcomedoTryAcquireOutcome(boolean wait) voidinit(Limit.InitializationContext context) Initialization method for this limit.voidDeprecated.protected intReturns the initial number of permits set for this semaphore-based limit.name()Name of this implementation, as provided inConfiguredProvider.create(Config, String).The prototype as it was received when creating this runtime object instance.protected AtomicIntegerReturns theAtomicIntegerinstance tracking the number of rejected requests.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_LIMIT
public static final int DEFAULT_LIMITDefault limit, meaning unlimited execution.- See Also:
-
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 constructFixedLimitinstance.- Returns:
- fluent API builder
-
create
Create a new instance with all defaults (no limit).- Returns:
- a new limit instance
-
create
Create an instance from the provided semaphore.- Parameters:
semaphore- semaphore to use- Returns:
- a new fixed limit backed by the provided semaphore
-
create
Create a new instance from configuration.- Parameters:
config- configuration of the fixed limit- Returns:
- a new limit instance configured from
config
-
create
Create a new instance from configuration.- Parameters:
config- configuration of the fixed 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
-
prototype
Description copied from interface:RuntimeType.ApiThe prototype as it was received when creating this runtime object instance.- Specified by:
prototypein interfaceRuntimeType.Api<FixedLimitConfig>- Returns:
- prototype object used to create this instance
-
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
-
copy
-
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
-
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
-
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. -
initialPermits
protected int initialPermits()Returns the initial number of permits set for this semaphore-based limit.The initial number of permits is used to initialize the underlying semaphore.
- Returns:
- the initial number of permits
-
concurrentRequests
Returns theAtomicIntegerinstance tracking the current number of concurrent requests.The returned
AtomicIntegeris used to maintain a count of the concurrent requests being processed.- Returns:
- the
AtomicIntegerinstance tracking concurrent requests
-
rejectedRequests
Returns theAtomicIntegerinstance tracking the number of rejected requests.- Returns:
- rejected requests counter
-
doTryAcquireOutcome
-