mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-04-24 23:27:25 -04:00
Add support for cancelling async requests in low-level REST client (#45379)
The low-level REST client exposes a `performRequestAsync` method that allows to send async requests, but today it does not expose the ability to cancel such requests. That is something that the underlying apache async http client supports, and it makes sense for us to expose. This commit adds a return value to the `performRequestAsync` method, which is backwards compatible. A `Cancellable` object gets returned, which exposes a `cancel` public method. When calling `cancel`, the on-going request associated with the returned `Cancellable` instance will be cancelled by calling its `abort` method. This works throughout multiple retries, though some special care was needed for the case where `cancel` is called between different attempts (when one attempt has failed and the consecutive one has not been sent yet). Note that cancelling a request on the client side does not automatically translate to cancelling the server side execution of it. That needs to be specifically implemented, which is on the work for the search API (see #43332). Relates to #44802
This commit is contained in:
parent
e0c9234ba9
commit
4baab594aa
8 changed files with 366 additions and 86 deletions
|
@ -224,7 +224,7 @@ Once the `RestClient` has been created, requests can be sent by calling either
|
|||
will block the calling thread and return the `Response` when the request is
|
||||
successful or throw an exception if it fails. `performRequestAsync` is
|
||||
asynchronous and accepts a `ResponseListener` argument that it calls with a
|
||||
`Response` when the request is successful or with an `Exception` if it4 fails.
|
||||
`Response` when the request is successful or with an `Exception` if it fails.
|
||||
|
||||
This is synchronous:
|
||||
|
||||
|
@ -329,6 +329,22 @@ include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-async-examp
|
|||
<2> Handle the returned exception, due to communication error or a response
|
||||
with status code that indicates an error
|
||||
|
||||
==== Cancelling asynchronous requests
|
||||
|
||||
The `performRequestAsync` method returns a `Cancellable` that exposes a single
|
||||
public method called `cancel`. Such method can be called to cancel the on-going
|
||||
request. Cancelling a request will result in aborting the http request through
|
||||
the underlying http client. On the server side, this does not automatically
|
||||
translate to the execution of that request being cancelled, which needs to be
|
||||
specifically implemented in the API itself.
|
||||
|
||||
["source","java",subs="attributes,callouts,macros"]
|
||||
--------------------------------------------------
|
||||
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-async-cancel]
|
||||
--------------------------------------------------
|
||||
<1> Process the returned response, in case it was ready before the request got cancelled
|
||||
<2> Handle the returned exception, which will most likely be a `CancellationException` as the request got cancelled
|
||||
|
||||
[[java-rest-low-usage-responses]]
|
||||
=== Reading responses
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue