From 14ba322745838eb8d27a1223236ba243def8ff92 Mon Sep 17 00:00:00 2001 From: nielsoledam Date: Fri, 4 Aug 2017 19:56:48 +0200 Subject: [PATCH 1/3] Has re-added code for using value of "retry-after" header as waiting time if status_code = 429. Now also respects value of pause_min. --- R/retry.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/retry.R b/R/retry.R index d1cb29a6..c62d7015 100644 --- a/R/retry.R +++ b/R/retry.R @@ -91,6 +91,10 @@ backoff_full_jitter <- function(i, resp, pause_base = 1, pause_cap = 60, error_description <- "" status <- status_code(resp) } + if (status == 429) { + length_429 <- as.numeric(resp$headers[["retry-after"]]) + if (!is.null(length_429) && length_429 >= pause_min) length <- length_429 + } message(error_description, "Request failed [", status, "]. Retrying in ", round(length, 1), " seconds...") } Sys.sleep(length) From d4e869efc3f0a8d9de95028662efa50ff6f38973 Mon Sep 17 00:00:00 2001 From: nielsoledam Date: Fri, 4 Aug 2017 21:56:09 +0200 Subject: [PATCH 2/3] Fixed error in if logic and added documentation. --- NEWS.md | 4 ++++ R/retry.R | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index b2ff253d..b6468cc6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -71,6 +71,10 @@ * More forms of empty query are recognized as such. Eliminates a source of spurious trailing `?` and `?=` in URLs produced by `modify_url()`. (@jennybc, #452) + +* If the server returns HTTP status code 429 and specifies a `retry-after` + value, that value will now be used instead of exponential backoff with + jitter, unless it's smaller than `pause_min`. (@nielsoledam, #472) # httr 1.2.1 diff --git a/R/retry.R b/R/retry.R index c62d7015..328bdf9d 100644 --- a/R/retry.R +++ b/R/retry.R @@ -9,6 +9,8 @@ #' randomly waits up to twice as long. (Technically it uses exponential #' backoff with jitter, using the approach outlined in #' \url{https://www.awsarchitectureblog.com/2015/03/backoff.html}.) +#' If the server returns status code 429 and specifies a \code{retry-after} value, that +#' value will be used instead, unless it's smaller than \code{pause_min}. #' #' @inheritParams VERB #' @inheritParams GET @@ -92,8 +94,8 @@ backoff_full_jitter <- function(i, resp, pause_base = 1, pause_cap = 60, status <- status_code(resp) } if (status == 429) { - length_429 <- as.numeric(resp$headers[["retry-after"]]) - if (!is.null(length_429) && length_429 >= pause_min) length <- length_429 + retry_after <- resp$headers[["retry-after"]] + if (!is.null(retry_after)) length <- max(pause_min, as.numeric(retry_after)) } message(error_description, "Request failed [", status, "]. Retrying in ", round(length, 1), " seconds...") } From 33ad8bd9d2e60212b469901b40c7e451566718c3 Mon Sep 17 00:00:00 2001 From: nielsoledam Date: Fri, 4 Aug 2017 22:17:43 +0200 Subject: [PATCH 3/3] Added {} and indented condition code to match rest of code. --- R/retry.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/retry.R b/R/retry.R index 328bdf9d..766fab84 100644 --- a/R/retry.R +++ b/R/retry.R @@ -95,7 +95,9 @@ backoff_full_jitter <- function(i, resp, pause_base = 1, pause_cap = 60, } if (status == 429) { retry_after <- resp$headers[["retry-after"]] - if (!is.null(retry_after)) length <- max(pause_min, as.numeric(retry_after)) + if (!is.null(retry_after)) { + length <- max(pause_min, as.numeric(retry_after)) + } } message(error_description, "Request failed [", status, "]. Retrying in ", round(length, 1), " seconds...") }