File tree Expand file tree Collapse file tree 3 files changed +22
-8
lines changed
main/java/io/opentelemetry/opamp/client/internal
test/java/io/opentelemetry/opamp/client/internal/connectivity/http Expand file tree Collapse file tree 3 files changed +22
-8
lines changed Original file line number Diff line number Diff line change 1010import java .time .ZonedDateTime ;
1111import java .time .format .DateTimeFormatter ;
1212import java .util .Locale ;
13+ import java .util .Optional ;
1314import java .util .regex .Pattern ;
1415
1516public final class RetryAfterParser {
@@ -29,13 +30,17 @@ public static RetryAfterParser getInstance() {
2930 this .systemTime = systemTime ;
3031 }
3132
32- public Duration parse (String value ) {
33+ public Optional <Duration > tryParse (String value ) {
34+ Duration duration = null ;
3335 if (SECONDS_PATTERN .matcher (value ).matches ()) {
34- return Duration .ofSeconds (Long .parseLong (value ));
36+ duration = Duration .ofSeconds (Long .parseLong (value ));
3537 } else if (DATE_PATTERN .matcher (value ).matches ()) {
36- return Duration .ofMillis (toMilliseconds (value ) - systemTime .getCurrentTimeMillis ());
38+ long difference = toMilliseconds (value ) - systemTime .getCurrentTimeMillis ();
39+ if (difference > 0 ) {
40+ duration = Duration .ofMillis (difference );
41+ }
3742 }
38- throw new IllegalArgumentException ( "Invalid Retry-After value: " + value );
43+ return Optional . ofNullable ( duration );
3944 }
4045
4146 private static long toMilliseconds (String value ) {
Original file line number Diff line number Diff line change 1717import java .io .OutputStream ;
1818import java .time .Duration ;
1919import java .util .Objects ;
20+ import java .util .Optional ;
2021import java .util .concurrent .ExecutionException ;
2122import java .util .concurrent .atomic .AtomicBoolean ;
2223import java .util .function .Consumer ;
@@ -164,7 +165,10 @@ private void handleHttpError(HttpSender.Response response) {
164165 String retryAfterHeader = response .getHeader ("Retry-After" );
165166 Duration retryAfter = null ;
166167 if (retryAfterHeader != null ) {
167- retryAfter = retryAfterParser .parse (retryAfterHeader );
168+ Optional <Duration > duration = retryAfterParser .tryParse (retryAfterHeader );
169+ if (duration .isPresent ()) {
170+ retryAfter = duration .get ();
171+ }
168172 }
169173 enableRetryMode (retryAfter );
170174 }
Original file line number Diff line number Diff line change @@ -18,12 +18,17 @@ class RetryAfterParserTest {
1818 @ Test
1919 void verifyParsing () {
2020 SystemTime systemTime = mock ();
21- long currentTimeMillis = 1577836800000L ;
21+ long currentTimeMillis = 1577836800000L ; // Wed, 01 Jan 2020 00:00:00 GMT
2222 when (systemTime .getCurrentTimeMillis ()).thenReturn (currentTimeMillis );
2323
2424 RetryAfterParser parser = new RetryAfterParser (systemTime );
2525
26- assertThat (parser .parse ("123" )).isEqualTo (Duration .ofSeconds (123 ));
27- assertThat (parser .parse ("Wed, 01 Jan 2020 01:00:00 GMT" )).isEqualTo (Duration .ofHours (1 ));
26+ assertThat (parser .tryParse ("123" )).get ().isEqualTo (Duration .ofSeconds (123 ));
27+ assertThat (parser .tryParse ("Wed, 01 Jan 2020 01:00:00 GMT" ))
28+ .get ()
29+ .isEqualTo (Duration .ofHours (1 ));
30+
31+ // Check when provided time is older than the current one
32+ assertThat (parser .tryParse ("Tue, 31 Dec 2019 23:00:00 GMT" )).isNotPresent ();
2833 }
2934}
You can’t perform that action at this time.
0 commit comments