@@ -33,55 +33,48 @@ public class ExternalUrlKeyReader {
33
33
private static final Duration FIXED_DELAY = Duration .ofSeconds (5 );
34
34
private static final int MAX_RETRIES = 59 ;
35
35
36
- private final String url ;
36
+ private final URL url ;
37
37
private final ObjectMapper mapper ;
38
38
private final AsyncRunner asyncRunner ;
39
39
40
40
public ExternalUrlKeyReader (
41
- final String url , final ObjectMapper mapper , final AsyncRunner asyncRunner ) {
42
- this .url = url ;
41
+ final String urlString , final ObjectMapper mapper , final AsyncRunner asyncRunner ) {
42
+ this .url = getUrl ( urlString ) ;
43
43
this .mapper = mapper ;
44
44
this .asyncRunner = asyncRunner ;
45
45
}
46
46
47
47
public Stream <BLSPublicKey > readKeys () {
48
- final String [] keys =
49
- readUrl ()
50
- .exceptionallyCompose (
51
- ex -> {
52
- if (ex instanceof MalformedURLException ) {
53
- return SafeFuture .failedFuture (
54
- new InvalidConfigurationException (
55
- "Failed to load public keys from invalid URL: " + url , ex ));
56
- }
57
- return retry ();
58
- })
59
- .join ();
48
+ final String [] keys = getKeysWithRetry ().join ();
60
49
return Arrays .stream (keys ).map (key -> BLSPublicKey .fromSSZBytes (Bytes .fromHexString (key )));
61
50
}
62
51
63
- private SafeFuture <String []> readUrl () {
64
- try {
65
- return SafeFuture .completedFuture (mapper .readValue (new URL (url ), String [].class ));
66
- } catch (IOException e ) {
67
- return SafeFuture .failedFuture (e );
68
- }
69
- }
70
-
71
52
@ VisibleForTesting
72
- SafeFuture <String []> retry () {
53
+ SafeFuture <String []> getKeysWithRetry () {
73
54
return asyncRunner
74
- .runWithRetry (
75
- () -> {
76
- STATUS_LOG .failedToLoadPublicKeysFromUrl (url );
77
- return readUrl ();
78
- },
79
- FIXED_DELAY ,
80
- MAX_RETRIES )
55
+ .runWithRetry (this ::readUrl , FIXED_DELAY , MAX_RETRIES )
81
56
.exceptionallyCompose (
82
57
ex ->
83
58
SafeFuture .failedFuture (
84
59
new InvalidConfigurationException (
85
60
"Failed to load public keys from URL: " + url , ex )));
86
61
}
62
+
63
+ private SafeFuture <String []> readUrl () {
64
+ try {
65
+ return SafeFuture .completedFuture (mapper .readValue (url , String [].class ));
66
+ } catch (IOException e ) {
67
+ STATUS_LOG .failedToLoadPublicKeysFromUrl (url .toString ());
68
+ return SafeFuture .failedFuture (e );
69
+ }
70
+ }
71
+
72
+ private URL getUrl (final String url ) {
73
+ try {
74
+ return new URL (url );
75
+ } catch (MalformedURLException e ) {
76
+ throw new InvalidConfigurationException (
77
+ "Failed to load public keys from invalid URL: " + url , e );
78
+ }
79
+ }
87
80
}
0 commit comments