Class RetryAfterParser
java.lang.Object
com.maybeitssquid.retry.RetryAfterParser
public class RetryAfterParser
extends Object
implements Function<jakarta.servlet.http.HttpServletResponse, Optional<Duration>>
Parses an HTTP Retry-After header to determine how long to wait before retrying. Parsing of the
header is based on a superset of RFC 7231. Anything permitted by that RFC should parse correctly.
Minor variations may be allowed, such as fractional seconds, optional day name, single-digit
hour/minute/second, optional seconds, extra whitespace before the hour.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Function<String, Optional<ZonedDateTime>> Forgiving parer for a superset of ASCTIME dates.Accept extendedRetry-Afterheader that allows decimal seconds.static final Function<String, Optional<ZonedDateTime>> Forgiving parser for a superset of IMF-fixdate using the builtinDateTimeFormatter.RFC_1123_DATE_TIMEstatic final Function<String, Optional<ZonedDateTime>> Parser for ISO-8601 dates.static final org.slf4j.LoggerLogger for errors during parsing, particularly to diagnose a misbehaving server.static final DurationThe longest delay this parser will report.static final StringTheRetry-Afterheader name.static final Function<String, Optional<ZonedDateTime>> Forgiving parer for a superset of RFC-850 dates. -
Method Summary
Modifier and TypeMethodDescriptionapply(jakarta.servlet.http.HttpServletResponse response) Parser to recognize the Retry-After formats defined in section 5.6.6 of RFC-9110 and convert the value to a duration.static RetryAfterParserParser that acceptsRetry-Afterheaders with a potentially decimal number of seconds to wait.static RetryAfterParserextended()Parser that acceptsRetry-Afterheaders that meet a superset of RFC-9110, including headers with a decimal number of seconds delay.static RetryAfterParserextended(InstantSource clock) Parser that acceptsRetry-Afterheaders that meet a superset of RFC-9110, including headers with a decimal number of seconds delay and ISO-8601 instants.static RetryAfterParserParser that accepts onlyRetry-Afterheaders with an integer number of seconds to wait.static RetryAfterParserstrict()Parser that accepts onlyRetry-Afterheaders that meet a reasonably strict interpretation of RFC-9110.static RetryAfterParserstrict(InstantSource clock) Parser that accepts onlyRetry-Afterheaders that meet a reasonably strict interpretation of RFC-9110.
-
Field Details
-
LOGGER
public static final org.slf4j.Logger LOGGERLogger for errors during parsing, particularly to diagnose a misbehaving server. -
RETRY_AFTER_HEADER
-
MAX_DELAY
The longest delay this parser will report. A server that asks for more than can be expressed in milliseconds is clamped to this rather than having the value wrap or be discarded, so the delay still exceeds any limit a caller has configured and the retry is refused rather than allowed. -
STRICT_SECONDS
AcceptRetry-Afterheader that matches only strict RFC 7231delay-seconds.A delay too large to express in milliseconds is clamped to
MAX_DELAYrather than wrapped or discarded, so everyDurationthis parser returns is safe to pass toDuration.toMillis()and still exceeds any sane limit. -
DECIMAL_SECONDS
-
IMF_FIXDATE
Forgiving parser for a superset of IMF-fixdate using the builtinDateTimeFormatter.RFC_1123_DATE_TIMEExample: "Thu, 02 Jan 2003 01:23:45 GMT"
-
RFC_850
Forgiving parer for a superset of RFC-850 dates.Example: "Thursday, 02-Jan-03 01:23:45 GMT"
-
ASCTIME
Forgiving parer for a superset of ASCTIME dates.Example: "Thu Jan 2 01:23:45 2003"
-
ISO
Parser for ISO-8601 dates. Any number of fractional second digits from 1 to 9 is accepted, not only whole groups of three.Example: "2011-12-03T10:15:30Z" Example: "2011-12-03T10:15:30.1Z" Example: "2011-12-03T10:15:30.123Z" Example: "2011-12-03T10:15:30.123456789Z"
-
-
Method Details
-
secondsOnly
Parser that accepts onlyRetry-Afterheaders with an integer number of seconds to wait. If the header contains some other value, such as a date, it is ignored.- Returns:
- Parser that accepts only
delay-secondsin theRetry-Afterheader.
-
decimalSeconds
Parser that acceptsRetry-Afterheaders with a potentially decimal number of seconds to wait. If the header contains some other value, such as a date, it is ignored.- Returns:
- Parser that accepts only seconds in the
Retry-Afterheader.
-
strict
Parser that accepts onlyRetry-Afterheaders that meet a reasonably strict interpretation of RFC-9110.- Parameters:
clock- the clock to use to compute offsets when the header is a date. Useful for testing.- Returns:
- Parser for RFC-9110
Retry-Afterheaders.
-
strict
Parser that accepts onlyRetry-Afterheaders that meet a reasonably strict interpretation of RFC-9110. Uses system clock to compute offsets when the header is a date.- Returns:
- Parser for RFC-9110
Retry-Afterheaders.
-
extended
Parser that acceptsRetry-Afterheaders that meet a superset of RFC-9110, including headers with a decimal number of seconds delay and ISO-8601 instants.- Parameters:
clock- the clock to use to compute offsets when the header is a date. Useful for testing.- Returns:
- Parser for extended
Retry-Afterheaders.
-
extended
Parser that acceptsRetry-Afterheaders that meet a superset of RFC-9110, including headers with a decimal number of seconds delay. Uses system clock to compute offsets when the header is a date.- Returns:
- Parser for extended
Retry-Afterheaders.
-
apply
Parser to recognize the Retry-After formats defined in section 5.6.6 of RFC-9110 and convert the value to a duration. If the header specified a date, the duration is relative to the current time.The source of current time defaults to the system clock, but can be overridden by supplying an
InstantSourceto the constructor.
-