R6 class for a pool of pre-warmed SecureSession instances.
Creates multiple sessions at initialization time so that $execute() calls
can run immediately on an idle session without waiting for process startup.
Sessions are returned to the pool after each execution completes (or errors).
Thread Safety
SecureSessionPool is NOT thread-safe. The acquire/release
mechanism uses no locking and assumes single-threaded access. If you need
to use pools from multiple processes (e.g., via parallel::mclapply
or future), each process should create its own pool instance.
Sharing a single pool across threads or forked processes will lead to
race conditions in session acquisition.
Methods
Method new()
Create a new SecureSessionPool
Usage
SecureSessionPool$new(
size = 4L,
tools = list(),
sandbox = TRUE,
limits = NULL,
verbose = FALSE,
reset_between_uses = FALSE
)Arguments
sizeInteger, number of sessions to pre-warm (default 4, minimum 1).
toolsA list of
securer_tool()objects passed to each session.sandboxLogical, whether to enable OS-level sandboxing.
limitsOptional named list of resource limits.
verboseLogical, whether to emit diagnostic messages.
reset_between_usesLogical, whether to restart each session after an execution before returning it to the pool (default
FALSE). WhenTRUE, callssession$restart()after every$execute()to prevent state leaking between executions (e.g., variables, loaded packages, options set by prior code).
Method execute()
Execute R code on an available pooled session
Arguments
codeCharacter string of R code to execute.
timeoutTimeout in seconds, or
NULLfor no timeout.acquire_timeoutOptional timeout in seconds to wait for a session to become available. If
NULL(default), fails immediately when all sessions are busy. If provided, retries acquisition with a short sleep (0.1s) between retries until the timeout expires.
Method format()
Format method for display
Method print()
Print method
Method close()
Close all sessions and shut down the pool