public abstract class AbstractConcurrencyLimitingClient<I extends Request,O extends Response> extends SimpleDecoratingClient<I,O>
Client decorator that limits the concurrent number of active requests.
numActiveRequests() increases when Client.execute(ClientRequestContext, Request) is
invoked and decreases when the Response returned by the
Client.execute(ClientRequestContext, Request) is closed. When numActiveRequests() reaches
at the configured maxConcurrency the Requests are deferred until the currently active
Requests are completed.
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractConcurrencyLimitingClient(Client<I,O> delegate,
int maxConcurrency)
Creates a new instance that decorates the specified
delegate to limit the concurrent number of
active requests to maxConcurrency, with the default timeout of
milliseconds. |
protected |
AbstractConcurrencyLimitingClient(Client<I,O> delegate,
int maxConcurrency,
long timeout,
TimeUnit unit)
Creates a new instance that decorates the specified
delegate to limit the concurrent number of
active requests to maxConcurrency. |
| Modifier and Type | Method and Description |
|---|---|
O |
execute(ClientRequestContext ctx,
I req)
|
protected abstract O |
newDeferredResponse(ClientRequestContext ctx,
CompletionStage<O> resFuture)
Implement this method to return a new
Response which delegates to the Response
the specified CompletionStage is completed with. |
int |
numActiveRequests()
Returns the number of the
Requests that are being executed. |
as, delegate, toStringprotected AbstractConcurrencyLimitingClient(Client<I,O> delegate, int maxConcurrency)
delegate to limit the concurrent number of
active requests to maxConcurrency, with the default timeout of
milliseconds.delegate - the delegate ClientmaxConcurrency - the maximum number of concurrent active requests. 0 to disable the limit.protected AbstractConcurrencyLimitingClient(Client<I,O> delegate, int maxConcurrency, long timeout, TimeUnit unit)
delegate to limit the concurrent number of
active requests to maxConcurrency.delegate - the delegate ClientmaxConcurrency - the maximum number of concurrent active requests. 0 to disable the limit.timeout - the amount of time until this decorator fails the request if the request was not
delegated to the delegate before thenpublic int numActiveRequests()
Requests that are being executed.public O execute(ClientRequestContext ctx, I req) throws Exception
Clientprotected abstract O newDeferredResponse(ClientRequestContext ctx, CompletionStage<O> resFuture) throws Exception
Response which delegates to the Response
the specified CompletionStage is completed with. For example, you could use
HttpResponse.from(CompletionStage, EventExecutor):
protected HttpResponse newDeferredResponse(
ClientRequestContext ctx, CompletionStage<HttpResponse> resFuture) {
return HttpResponse.from(resFuture, ctx.eventLoop());
}
ExceptionCopyright © 2020 LeanCloud. All rights reserved.