A quality of service Handler that conditionally
limits the number of concurrent requests, to provide more predictable
end-user experience in case descendant Handlers have limited
capacity.
This Handler limits the number of concurrent requests
to the number configured via setMaxRequestCount(int).
If more requests are received, they are suspended (that is, not
forwarded to the child Handler) and stored in a priority
queue.
Priorities are determined via getPriority(Request),
that should return values between 0 (the lowest priority)
and positive numbers, typically in the range 0-10.
When a request that is being processed completes, the suspended request that current has the highest priority is resumed.
This Handler is ideal to avoid contending on slow/limited
resources such as a JDBC connection pool, avoiding the situation
where all server threads blocked contending on the limited
resource, therefore leaving threads free to process other
requests that do not require access to the limited resource.
Requests are resumed in priority order, so that when the server is under load, and there are many requests suspended to be processed, high priority request are processed first. For example, load balancer "ping" requests may have the highest priority, followed by requests performed by admin users, etc. so that regardless of the load, "ping" and "admin" requests will always be able to access the web application.
| Modifier and Type | Class and Description |
|---|---|
| private class | |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| private static final String | |
| private static final Logger | |
| private int | |
| private Duration | |
| private final Set | |
| private final Map | |
| private final AtomicInteger | |
| private CyclicTimeouts |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected void | doStart()
Overrides org. |
| protected void | doStop()
Stops the managed lifecycle beans in the reverse order they were added.
|
| protected void | failSuspended(Request
the request to fail request, Response the response to fail response, Callback the callback to complete callback, int the failure status code status, Throwable the failure failure)Fails the given suspended request/response with the given error code and failure. |
| public int | |
| public Duration | Returns: the max duration of time a request may stay suspendedGet the max duration of time a request may stay suspended. |
| protected int | Returns: the priority of the given suspended request, a value>= 0the suspended request to compute the priority for request)Returns the priority of the given suspended request,
a value greater than or equal to |
| public long | |
| private boolean | |
| private static void | |
| public boolean | onConditionsMet(Request
The request to handle request, Response The response to generate response, Callback The callback for completion callback)Implements abstract org. |
| protected boolean | onConditionsNotMet(Request
The request to handle request, Response The response to generate response, Callback The callback for completion callback)Implements abstract org. |
| private void | |
| private boolean | |
| public void | setMaxRequestCount(int
the max number of concurrent requests maxRequests)Sets the max number of concurrent requests. |
| public void | setMaxSuspend(Duration
the max duration of time a request may stay suspended maxSuspend)Sets the max duration of time a request may stay suspended. |
| private void |
| EXPIRED_ATTRIBUTE_NAME | back to summary |
|---|---|
| private static final String EXPIRED_ATTRIBUTE_NAME | |
| LOG | back to summary |
|---|---|
| private static final Logger LOG Hides org. | |
| maxRequests | back to summary |
|---|---|
| private int maxRequests | |
| maxSuspend | back to summary |
|---|---|
| private Duration maxSuspend | |
| priorities | back to summary |
|---|---|
| private final Set<Integer> priorities | |
| queues | back to summary |
|---|---|
| private final Map<Integer, Queue<QoSHandler. | |
| state | back to summary |
|---|---|
| private final AtomicInteger state | |
| timeouts | back to summary |
|---|---|
| private CyclicTimeouts<QoSHandler. | |
| QoSHandler | back to summary |
|---|---|
| public QoSHandler() | |
| QoSHandler | back to summary |
|---|---|
| public QoSHandler(Handler handler) | |
| doStart | back to summary |
|---|---|
| protected void doStart() throws Exception Overrides org. Doc from org. Starts the managed lifecycle beans in the order they were added.
| |
| doStop | back to summary |
|---|---|
| protected void doStop() throws Exception Overrides org. Doc from org. Stops the managed lifecycle beans in the reverse order they were added.
| |
| failSuspended | back to summary |
|---|---|
| protected void failSuspended(Request request, Response response, Callback callback, int status, Throwable failure) Fails the given suspended request/response with the given error code and failure. This method is called only for suspended requests, in case of timeout while suspended, or in case of failure when trying to handle a resumed request. | |
| getMaxRequestCount | back to summary |
|---|---|
public int getMaxRequestCount()
| |
| getMaxSuspend | back to summary |
|---|---|
| public Duration getMaxSuspend() Get the max duration of time a request may stay suspended.
| |
| getPriority | back to summary |
|---|---|
| protected int getPriority(Request request) Returns the priority of the given suspended request,
a value greater than or equal to Priority The set of returned priorities should be stable over
time, typically constrained in the range
| |
| getSuspendedRequestCount | back to summary |
|---|---|
public long getSuspendedRequestCount()
| |
| handleWithPermit | back to summary |
|---|---|
| private boolean handleWithPermit(Request request, Response response, Callback callback) throws Exception | |
| notAvailable | back to summary |
|---|---|
| private static void notAvailable(Response response, Callback callback) | |
| onConditionsMet | back to summary |
|---|---|
| public boolean onConditionsMet(Request request, Response response, Callback callback) throws Exception Implements abstract org. Doc from org. Handle a request that has met the conditions.
Typically, the implementation will provide optional handling and then call the
| |
| onConditionsNotMet | back to summary |
|---|---|
| protected boolean onConditionsNotMet(Request request, Response response, Callback callback) throws Exception Implements abstract org. Doc from org. This method is called when the request has not met the conditions and is not to be handled by this handler. Implementations may return false; send an error response; or handle the request differently. | |
| resume | back to summary |
|---|---|
| private void resume(Throwable x) | |
| resumeSuspended | back to summary |
|---|---|
| private boolean resumeSuspended() | |
| setMaxRequestCount | back to summary |
|---|---|
| public void setMaxRequestCount(int maxRequests) Sets the max number of concurrent requests. A negative or zero value indicates to calculate a value based on heuristics, drawn from the number the size of the server thread pool and/or the number of CPU cores.
| |
| setMaxSuspend | back to summary |
|---|---|
| public void setMaxSuspend(Duration maxSuspend) Sets the max duration of time a request may stay suspended. Once the duration expires, the request is failed with an HTTP
status of
| |
| suspend | back to summary |
|---|---|
| private void suspend(Request request, Response response, Callback callback) | |
| Modifier and Type | Field and Description |
|---|---|
| private final Callback | |
| private final long | |
| private final int | |
| private final Request | |
| private final Response |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public long | getExpireNanoTime()
Implements org. Returns the expiration time in nanoseconds. |
| public void |
| callback | back to summary |
|---|---|
| private final Callback callback | |
| expireNanoTime | back to summary |
|---|---|
| private final long expireNanoTime | |
| priority | back to summary |
|---|---|
| private final int priority | |
| request | back to summary |
|---|---|
| private final Request request | |
| response | back to summary |
|---|---|
| private final Response response | |
| Entry | back to summary |
|---|---|
| private Entry(Request request, Response response, Callback callback, int priority) | |
| expire | back to summary |
|---|---|
| private void expire() | |
| getExpireNanoTime | back to summary |
|---|---|
| public long getExpireNanoTime() Implements org. Doc from org. Returns the expiration time in nanoseconds. The value to return must be calculated taking into account the current nanoTime, for example: expireNanoTime = NanoTime.now() + timeoutNanos
Returning
| |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation.
| |
| Modifier and Type | Method and Description |
|---|---|
| protected Iterator | |
| protected boolean | onExpired(QoSHandler.
the entity that is expired entryImplements abstract org. Invoked during the iteration when the given entity is expired. |
| Timeouts | back to summary |
|---|---|
| private Timeouts(Scheduler scheduler) | |
| iterator | back to summary |
|---|---|
| protected Iterator Implements abstract org.
| |
| onExpired | back to summary |
|---|---|
| protected boolean onExpired(QoSHandler. Implements abstract org. Doc from org. Invoked during the iteration when the given entity is expired. This method may be invoked multiple times, and even concurrently, for the same expirable entity and therefore the expiration of the entity, if any, should be an idempotent action. When
| |