Skip to content

Commit 5bc04de

Browse files
rdwalliskdavisk6
authored andcommitted
Parse Retry-After header responses that include decimal points (OpenFeign#980)
rfc7231 section 7.1.3 states that the Retry-After header can return delay-seconds value that is a non-negative decimal integer, representing time in seconds. Some servers return the second delay with a decimal point. Eg instead of 2 they return 2.0 This patch handles this case where the server has included a decimal point in their response.
1 parent ad86f22 commit 5bc04de

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

core/src/main/java/feign/codec/ErrorDecoder.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@
1313
*/
1414
package feign.codec;
1515

16+
import static feign.FeignException.errorStatus;
17+
import static feign.Util.RETRY_AFTER;
18+
import static feign.Util.checkNotNull;
19+
import static java.util.Locale.US;
20+
import static java.util.concurrent.TimeUnit.SECONDS;
21+
22+
import feign.FeignException;
23+
import feign.Response;
24+
import feign.RetryableException;
25+
1626
import java.text.DateFormat;
1727
import java.text.ParseException;
1828
import java.text.SimpleDateFormat;
1929
import java.util.Collection;
2030
import java.util.Date;
2131
import java.util.Map;
22-
import feign.FeignException;
23-
import feign.Response;
24-
import feign.RetryableException;
25-
import static feign.FeignException.errorStatus;
26-
import static feign.Util.RETRY_AFTER;
27-
import static feign.Util.checkNotNull;
28-
import static java.util.Locale.US;
29-
import static java.util.concurrent.TimeUnit.NANOSECONDS;
30-
import static java.util.concurrent.TimeUnit.SECONDS;
3132

3233
/**
3334
* Allows you to massage an exception into a application-specific one. Converting out to a throttle
@@ -143,7 +144,8 @@ public Date apply(String retryAfter) {
143144
if (retryAfter == null) {
144145
return null;
145146
}
146-
if (retryAfter.matches("^[0-9]+$")) {
147+
if (retryAfter.matches("^[0-9]+\\.?0*$")) {
148+
retryAfter = retryAfter.replaceAll("\\.0*$", "");
147149
long deltaMillis = SECONDS.toMillis(Long.parseLong(retryAfter));
148150
return new Date(currentTimeMillis() + deltaMillis);
149151
}

core/src/test/java/feign/codec/RetryAfterDecoderTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
*/
1414
package feign.codec;
1515

16-
import org.junit.Test;
17-
import java.text.ParseException;
18-
import feign.codec.ErrorDecoder.RetryAfterDecoder;
1916
import static feign.codec.ErrorDecoder.RetryAfterDecoder.RFC822_FORMAT;
20-
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2117
import static org.junit.Assert.assertEquals;
2218
import static org.junit.Assert.assertFalse;
2319

20+
import feign.codec.ErrorDecoder.RetryAfterDecoder;
21+
22+
import java.text.ParseException;
23+
24+
import org.junit.Test;
25+
2426
public class RetryAfterDecoderTest {
2527

2628
private RetryAfterDecoder decoder = new RetryAfterDecoder(RFC822_FORMAT) {
@@ -48,4 +50,9 @@ public void rfc822Parses() throws ParseException {
4850
public void relativeSecondsParses() throws ParseException {
4951
assertEquals(RFC822_FORMAT.parse("Sun, 2 Jan 2000 00:00:00 GMT"), decoder.apply("86400"));
5052
}
53+
54+
@Test
55+
public void relativeSecondsParseDecimalIntegers() throws ParseException {
56+
assertEquals(RFC822_FORMAT.parse("Sun, 2 Jan 2000 00:00:00 GMT"), decoder.apply("86400.0"));
57+
}
5158
}

0 commit comments

Comments
 (0)