Skip to content

Commit

Permalink
avoids legacy code path for daemon()
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Dec 5, 2024
1 parent 5fe0b60 commit a71fada
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
38 changes: 24 additions & 14 deletions R/daemon.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@
#'
#' @export
#'
daemon <- function(url, ..., dispatcher = FALSE, asyncdial = FALSE, autoexit = TRUE,
daemon <- function(url, dispatcher = FALSE, ..., asyncdial = FALSE, autoexit = TRUE,
cleanup = TRUE, output = FALSE, tls = NULL, rs = NULL) {

dispatcher || return(
missing(dispatcher) && return(
daemon_legacy(
url = url, asyncdial = asyncdial, autoexit = autoexit, cleanup = cleanup,
output = output, ..., tls = tls, rs = rs
)
)
cv <- cv()
sock <- socket(protocol = "poly")
sock <- socket(protocol = if (dispatcher) "poly" else "rep")
on.exit(reap(sock))
`[[<-`(., "sock", sock)
autoexit && pipe_notify(sock, cv = cv, remove = TRUE, flag = as.integer(autoexit))
Expand All @@ -117,17 +117,27 @@ daemon <- function(url, ..., dispatcher = FALSE, asyncdial = FALSE, autoexit = T
}
snapshot()

repeat {
aio <- recv_aio(sock, mode = 1L, cv = cv)
wait(cv) || break
m <- collect_aio(aio)
is.object(m) && next
cancel <- recv_aio(sock, mode = 8L, cv = NA)
data <- eval_mirai(m)
stop_aio(cancel)
send(sock, data, mode = 1L, block = TRUE)
if (cleanup) do_cleanup()
}
if (dispatcher)
repeat {
aio <- recv_aio(sock, mode = 1L, cv = cv)
wait(cv) || break
m <- collect_aio(aio)
is.object(m) && next
cancel <- recv_aio(sock, mode = 8L, cv = NA)
data <- eval_mirai(m)
stop_aio(cancel)
send(sock, data, mode = 1L, block = TRUE)
if (cleanup) do_cleanup()
} else
repeat {
ctx <- .context(sock)
aio <- recv_aio(ctx, mode = 1L, cv = cv)
wait(cv) || break
m <- collect_aio(aio)
data <- eval_mirai(m)
send(ctx, data, mode = 1L, block = TRUE)
if (cleanup) do_cleanup()
}

}

Expand Down
6 changes: 3 additions & 3 deletions man/daemon.Rd

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

4 changes: 2 additions & 2 deletions tests/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ connection && Sys.getenv("NOT_CRAN") == "true" && {
m <- mirai_map(1:10, function(x) { Sys.sleep(2); y <<- TRUE })
s <- stop_mirai(m)
test_equal(sum(unlist(m[])), 200L)
if (.Platform[["OS.type"]] != "windows") test_identical(s, !logical(10L))
if (.Platform[["OS.type"]] != "windows") test_class("errorValue", mirai(y)[])
if (Sys.info()[["sysname"]] == "Linux") test_identical(s, !logical(10L))
if (Sys.info()[["sysname"]] == "Linux") test_class("errorValue", mirai(y)[])
test_equal(status()$connections, 1L)
test_equal(length(nextget("urls")), 1L)
test_class("miraiLaunchCmd", launch_remote(1))
Expand Down

0 comments on commit a71fada

Please sign in to comment.