Skip to content

Commit e0c71f7

Browse files
authored
Add interrupts escape hatch (#746)
I can't see any way to test this, but I experimented interactively with this code: ```R library(httr2) request_base <- request(example_url()) reqs <- rep(list(request_base |> req_url_path("/delay/0.1")), 100) repeat(req_perform_parallel(reqs)) ``` Fixes #720
1 parent 44ec779 commit e0c71f7

File tree

6 files changed

+18
-0
lines changed

6 files changed

+18
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# httr2 (development version)
22

3+
* Functions that capture interrutps (like `req_perform_parallel()` and friends) are now easier to escape if they're called inside a loop: you can press Ctrl + C twice to guarantee an exit (#1810).
34
* New `last_request_json()` and `last_response_json()` to conveniently see JSON bodies (#734).
45
* `req_body_json_modify()` can now be used on a request with an empty body.
56
* `resp_timing()` exposes timing information about the request measured by libcurl (@arcresu, #725).

R/httr2-package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ the$token_cache <- new_environment()
1717
the$last_response <- NULL
1818
the$last_request <- NULL
1919
the$pool_pollers <- new_environment()
20+
the$last_interrupt <- 0

R/req-perform-iterative.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ req_perform_iterative <- function(
181181
}
182182
},
183183
interrupt = function(cnd) {
184+
check_repeated_interrupt()
185+
184186
# interrupt might occur after i was incremented
185187
if (is.null(resps[[i]])) {
186188
i <<- i - 1

R/req-perform-parallel.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ req_perform_parallel <- function(
9191
tryCatch(
9292
queue$process(),
9393
interrupt = function(cnd) {
94+
check_repeated_interrupt()
95+
9496
queue$queue_status <- "errored"
9597
queue$process()
9698

R/req-perform-sequential.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ req_perform_sequential <- function(
9292
}
9393
},
9494
interrupt = function(cnd) {
95+
check_repeated_interrupt()
96+
9597
resps <- resps[seq_len(i)]
9698
cli::cli_alert_warning(
9799
"Terminating iteration; returning {i} response{?s}."

R/utils.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,13 @@ log_stream <- function(..., prefix = "<< ") {
347347
paste_c <- function(..., collapse = "") {
348348
paste0(c(...), collapse = collapse)
349349
}
350+
351+
# Give user the get-out-of-jail-free card if interrupt-capturing function
352+
# is wrapped inside a loop
353+
check_repeated_interrupt <- function() {
354+
if (as.double(Sys.time()) - the$last_interrupt < 1) {
355+
cli::cli_alert_warning("Interrupting")
356+
interrupt()
357+
}
358+
the$last_interrupt <- as.double(Sys.time())
359+
}

0 commit comments

Comments
 (0)