18
18
import java .util .List ;
19
19
import java .util .Map ;
20
20
import java .util .TimeZone ;
21
+ import java .util .concurrent .CompletableFuture ;
22
+ import java .util .concurrent .ExecutionException ;
23
+ import java .util .concurrent .TimeUnit ;
24
+ import java .util .concurrent .TimeoutException ;
21
25
22
26
import org .slf4j .Logger ;
23
27
import org .slf4j .LoggerFactory ;
@@ -57,12 +61,14 @@ public class SatnogsClient {
57
61
private final HttpClient httpclient ;
58
62
private final String hostname ;
59
63
private final Duration timeout ;
64
+ private final long timeoutMillis ;
60
65
private final Clock clock ;
61
66
62
67
public SatnogsClient (Configuration config , Clock clock ) {
63
68
this .hostname = config .getProperty ("satnogs.hostname" );
64
69
this .clock = clock ;
65
- this .timeout = Duration .ofMillis (config .getInteger ("satnogs.connectionTimeout" ));
70
+ this .timeoutMillis = config .getInteger ("satnogs.connectionTimeout" );
71
+ this .timeout = Duration .ofMillis (timeoutMillis );
66
72
this .httpclient = HttpClient .newBuilder ().version (Version .HTTP_2 ).followRedirects (Redirect .NORMAL ).connectTimeout (timeout ).build ();
67
73
}
68
74
@@ -491,7 +497,8 @@ private HttpResponse<String> sendWithRetry(HttpRequest request, HttpResponse.Bod
491
497
int currentRetry = 0 ;
492
498
while (true ) {
493
499
try {
494
- HttpResponse <String > result = httpclient .send (request , responseBodyHandler );
500
+ CompletableFuture <HttpResponse <String >> response = httpclient .sendAsync (request , responseBodyHandler );
501
+ HttpResponse <String > result = response .get (timeoutMillis , TimeUnit .MILLISECONDS );
495
502
// retry for any server-side errors
496
503
// can happen during server upgrade
497
504
if (result .statusCode () < 500 ) {
@@ -502,10 +509,10 @@ private HttpResponse<String> sendWithRetry(HttpRequest request, HttpResponse.Bod
502
509
return result ;
503
510
}
504
511
LOG .info ("unable to send. status code: {}. retry {}" , result .statusCode (), currentRetry );
505
- } catch (IOException e ) {
512
+ } catch (ExecutionException | TimeoutException e ) {
506
513
currentRetry ++;
507
514
if (currentRetry > MAX_RETRIES ) {
508
- throw e ;
515
+ throw new IOException ( e ) ;
509
516
}
510
517
Util .logIOException (LOG , false , "unable to send. retry " + currentRetry , e );
511
518
}
0 commit comments