@@ -31,12 +31,13 @@ public Response performRequestRetry(String endpointId, @Nullable Node node, Requ
3131 try {
3232 return delegate .performRequest (endpointId , node , request , options );
3333 } catch (ResponseException e ) {
34- if (e .getResponse ().getStatusLine ().getStatusCode () == 503 ) { // TODO list of statuses
34+ if (e .getResponse ().getStatusLine ().getStatusCode () == 503 ) { // TODO list of statuses, configurable or hardcoded?
3535 // synchronous retry
3636 if (backoffIter .hasNext ()) {
3737 try {
38- Thread .sleep (backoffIter .next ()); // TODO ... no?
38+ Thread .sleep (backoffIter .next ());
3939 } catch (InterruptedException ie ) {
40+ throw e ; // TODO okay with masking IE and just returning original exception?
4041 }
4142 System .out .println ("Retrying" );
4243 return performRequestRetry (endpointId , node , request , options , backoffIter );
@@ -50,33 +51,41 @@ public Response performRequestRetry(String endpointId, @Nullable Node node, Requ
5051 @ Override
5152 public CompletableFuture <Response > performRequestAsync (String endpointId , @ Nullable Node node ,
5253 Request request , TransportOptions options ) {
53- return performRequestAsyncRetry (endpointId , node , request , options , backoffPolicy .iterator ());
54+ RequestFuture <Response > futureResult = new RequestFuture <>();
55+ return performRequestAsyncRetry (endpointId , node , request , options , backoffPolicy .iterator (),
56+ futureResult );
5457 }
5558
5659 public CompletableFuture <Response > performRequestAsyncRetry (String endpointId , @ Nullable Node node ,
5760 Request request ,
5861 TransportOptions options ,
59- Iterator <Long > backoffIter ) {
60- CompletableFuture <Response > fut = delegate .performRequestAsync (endpointId , node , request , options );
61- try {
62- fut .get (); // TODO is this problematic?
63- return fut ;
64- } catch (Exception e ) {
65- if (e .getCause () instanceof ResponseException ) {
66- if (((ResponseException ) e .getCause ()).getResponse ().getStatusLine ().getStatusCode () == 503 ) { // TODO list of statuses
67- if (backoffIter .hasNext ()) {
68- try {
69- Thread .sleep (backoffIter .next ()); // TODO ... no?
70- } catch (InterruptedException ie ) {
71- fut .completeExceptionally (e ); // TODO masking internal errors and just returning original error okay?
62+ Iterator <Long > backoffIter ,
63+ CompletableFuture <Response > futureResult ) {
64+ CompletableFuture <Response > res = delegate .performRequestAsync (endpointId , node , request , options );
65+
66+ res .whenComplete ((resp , e ) -> {
67+ if (e != null ) {
68+ if (e instanceof ResponseException ) {
69+ if (((ResponseException ) e ).getResponse ().getStatusLine ().getStatusCode () == 503 ) { // TODO list of statuses, configurable or hardcoded?
70+ if (backoffIter .hasNext ()) {
71+ try {
72+ Thread .sleep (backoffIter .next ());
73+ } catch (InterruptedException ie ) {
74+ // TODO okay with masking IE and just returning original exception?
75+ futureResult .completeExceptionally (e );
76+ }
77+ System .out .println ("Retrying" );
78+ performRequestAsyncRetry (endpointId , node , request , options , backoffIter ,futureResult );
7279 }
73- System .out .println ("Retrying" );
74- return performRequestAsyncRetry (endpointId , node , request , options , backoffIter );
7580 }
7681 }
7782 }
78- return fut ;
79- }
83+ else {
84+ futureResult .complete (resp );
85+ }
86+ });
87+
88+ return futureResult ;
8089 }
8190
8291 @ Override
0 commit comments