| 1 |
# nanonext - Dispatcher -------------------------------------------------------- |
| 2 |
|
| 3 |
#' Dispatcher |
| 4 |
#' |
| 5 |
#' Run the dispatcher event loop for mirai task distribution. |
| 6 |
#' |
| 7 |
#' @param sock REP socket for host communication. |
| 8 |
#' @param psock POLY socket for daemon communication. |
| 9 |
#' @param monitor Monitor object for pipe events (its CV is used for signaling). |
| 10 |
#' @param reset Pre-serialized connection reset error (raw vector). |
| 11 |
#' @param serial Serialization configuration (list or NULL). |
| 12 |
#' @param envir Environment containing RNG stream state. |
| 13 |
#' @param next_stream Function to get next RNG stream, called as next_stream(envir). |
| 14 |
#' |
| 15 |
#' @return Integer status code (0 = normal exit). |
| 16 |
#' |
| 17 |
#' @keywords internal |
| 18 |
#' @export |
| 19 |
#' |
| 20 |
.dispatcher <- function(sock, psock, monitor, reset, serial, envir, next_stream) { |
| 21 |
.Call(rnng_dispatcher_run, sock, psock, monitor, reset, serial, envir, next_stream) |
| 22 |
} |
| 23 |
|
| 24 |
#' Start In-Process Dispatcher |
| 25 |
#' |
| 26 |
#' @param url URL to listen at for daemon connections. |
| 27 |
#' @param disp_url inproc:// URL for host REQ socket. |
| 28 |
#' @param tls TLS configuration or NULL. |
| 29 |
#' @param serial Serialization configuration (list or NULL). |
| 30 |
#' @param stream RNG stream integer vector (.Random.seed). |
| 31 |
#' @param capacity Memory budget in MB (metric, 1 MB = 1,000,000 bytes) for |
| 32 |
#' queued task payloads. `NULL`, 0, non-finite, or negative values are |
| 33 |
#' treated as unlimited. |
| 34 |
#' @param cvar Shared condition variable for capacity signaling. |
| 35 |
#' |
| 36 |
#' @return External pointer to dispatcher handle. |
| 37 |
#' |
| 38 |
#' @keywords internal |
| 39 |
#' @export |
| 40 |
#' |
| 41 |
.dispatcher_start <- function(url, disp_url, tls, serial, stream, capacity, cvar) { |
| 42 |
.Call(rnng_dispatcher_start, url, disp_url, tls, serial, stream, capacity, cvar) |
| 43 |
} |
| 44 |
|
| 45 |
#' Stop In-Process Dispatcher |
| 46 |
#' |
| 47 |
#' @param disp External pointer to dispatcher handle. |
| 48 |
#' |
| 49 |
#' @return Invisible NULL. |
| 50 |
#' |
| 51 |
#' @keywords internal |
| 52 |
#' @export |
| 53 |
#' |
| 54 |
.dispatcher_stop <- function(disp) invisible(.Call(rnng_dispatcher_stop, disp)) |
| 55 |
|
| 56 |
#' Wait for N Daemon Connections |
| 57 |
#' |
| 58 |
#' @param disp External pointer to dispatcher handle. |
| 59 |
#' @param n Number of connections to wait for. |
| 60 |
#' |
| 61 |
#' @return Invisible NULL. |
| 62 |
#' |
| 63 |
#' @keywords internal |
| 64 |
#' @export |
| 65 |
#' |
| 66 |
.dispatcher_wait <- function(disp, n) invisible(.Call(rnng_dispatcher_wait, disp, n)) |
| 67 |
|
| 68 |
#' Dispatcher Info |
| 69 |
#' |
| 70 |
#' Read dispatcher statistics directly under lock. |
| 71 |
#' |
| 72 |
#' @param disp External pointer to dispatcher handle. |
| 73 |
#' |
| 74 |
#' @return Integer vector of length 5: connections, cumulative, awaiting, |
| 75 |
#' executing, completed. |
| 76 |
#' |
| 77 |
#' @keywords internal |
| 78 |
#' @export |
| 79 |
#' |
| 80 |
.dispatcher_info <- function(disp) .Call(rnng_dispatcher_info, disp) |
| 81 |
|
| 82 |
#' Dispatcher Capacity |
| 83 |
#' |
| 84 |
#' Read current and peak queued task payload usage at dispatcher, plus the |
| 85 |
#' configured capacity, in MB (metric, 1 MB = 1,000,000 bytes). |
| 86 |
#' |
| 87 |
#' @param disp External pointer to dispatcher handle. |
| 88 |
#' |
| 89 |
#' @return Named numeric vector of length 3: **used** (current) and **peak** |
| 90 |
#' (high-watermark) usage, and **capacity** (the `capacity` set on |
| 91 |
#' [.dispatcher_start()], `NA_real_` if unset/unbounded), all in MB. |
| 92 |
#' `NA_real_` in each slot if `disp` is invalid. |
| 93 |
#' |
| 94 |
#' @keywords internal |
| 95 |
#' @export |
| 96 |
#' |
| 97 |
.dispatcher_capacity <- function(disp) .Call(rnng_dispatcher_capacity, disp) |
| 98 |
|
| 99 |
#' Dispatcher Gate |
| 100 |
#' |
| 101 |
#' Block while queued bytes at dispatcher exceed the memory budget set on |
| 102 |
#' `.dispatcher_start()`. |
| 103 |
#' |
| 104 |
#' @param disp External pointer to dispatcher handle. |
| 105 |
#' |
| 106 |
#' @return Logical TRUE. |
| 107 |
#' |
| 108 |
#' @keywords internal |
| 109 |
#' @export |
| 110 |
#' |
| 111 |
.dispatcher_gate <- function(disp) .Call(rnng_dispatcher_gate, disp) |
| 112 |
|
| 113 |
#' Dispatcher Try Gate |
| 114 |
#' |
| 115 |
#' Snapshot read of dispatcher capacity. Returns immediately, never blocks. |
| 116 |
#' The non-blocking counterpart to [.dispatcher_gate()]. |
| 117 |
#' |
| 118 |
#' @param disp External pointer to dispatcher handle. |
| 119 |
#' |
| 120 |
#' @return Logical `TRUE` if submission would not block (queued bytes below |
| 121 |
#' the memory budget set on `.dispatcher_start()`, or unbounded), `FALSE` |
| 122 |
#' if at or over the budget. `NULL` if `disp` is invalid. |
| 123 |
#' |
| 124 |
#' @keywords internal |
| 125 |
#' @export |
| 126 |
#' |
| 127 |
.dispatcher_try_gate <- function(disp) .Call(rnng_dispatcher_try_gate, disp) |