Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RETRY options to ignore statuses and use try #429

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions R/retry.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
#' \code{pause_cap} seconds.
#' @param quiet If \code{FALSE}, will print a message displaying how long
#' until the next request.
#' @return The last response. Note that if the request doesn't succeed after
#' @param use_try If \code{TRUE}, will wrap the request execution in a call to
#' \code{\link[base]{try}()} in order to catch lower level errors in the
#' request.
#' @param safe_statuses Integer vector of status codes that should be considered
#' safe and not cause a retry.
#' @return The last response or, if \code{use_try} was \code{TRUE}, an invisible
#' object of class \code{"try-error"}. Note that if the request doesn't succeed after
#' \code{times} times this will be a failed request, i.e. you still need
#' to use \code{\link{stop_for_status}()}.
#' @export
Expand All @@ -25,29 +31,50 @@
#' RETRY("GET", "http://httpbin.org/status/200")
#' # Never succeeds
#' RETRY("GET", "http://httpbin.org/status/500")
#' # Returns immediately with a 404 response
#' RETRY("GET", "http://httpbin.org/status/404", safe_statuses = 400L)
RETRY <- function(verb, url = NULL, config = list(), ...,
body = NULL, encode = c("multipart", "form", "json", "raw"),
times = 3, pause_base = 1, pause_cap = 60,
handle = NULL, quiet = FALSE) {
handle = NULL, quiet = FALSE, use_try = FALSE, safe_statuses = integer()) {
stopifnot(is.numeric(times), length(times) == 1L)
stopifnot(is.numeric(pause_base), length(pause_base) == 1L)
stopifnot(is.numeric(pause_cap), length(pause_cap) == 1L)
stopifnot(is.logical(use_try), length(use_try) == 1L, !is.na(use_try))
stopifnot(is.integer(safe_statuses))

hu <- handle_url(handle, url, ...)
req <- request_build(verb, hu$url, body_config(body, match.arg(encode)), config, ...)
resp <- request_perform(req, hu$handle$handle)
resp <- if (use_try) {
try(request_perform(req, hu$handle$handle))
} else {
request_perform(req, hu$handle$handle)
}

i <- 1
while (i < times && http_error(resp)) {
backoff_full_jitter(i, status_code(resp), pause_base, pause_cap, quiet = quiet)
while (i < times && should_retry(resp, safe_statuses)) {
status = if ("try-error" %in% class(resp)) {
attr(resp, "condition")$message
} else {
status_code(resp)
}
backoff_full_jitter(i, status, pause_base, pause_cap, quiet = quiet)

i <- i + 1
resp <- request_perform(req, hu$handle$handle)
resp <- if (use_try) {
try(request_perform(req, hu$handle$handle))
} else {
request_perform(req, hu$handle$handle)
}
}

resp
}

should_retry <- function(resp, safe_statuses) {
"try-error" %in% class(resp) || (!status_code(resp) %in% safe_statuses && http_error(resp))
}

backoff_full_jitter <- function(i, status, pause_base = 1, pause_cap = 60, quiet = FALSE) {
length <- ceiling(stats::runif(1, max = min(pause_cap, pause_base * (2 ^ i))))
if (!quiet) {
Expand Down
16 changes: 13 additions & 3 deletions man/RETRY.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.