AbstractExprEvaluatorPool

abstract class AbstractExprEvaluatorPool<T : AbstractExprEvaluator>(size: Int, val resetEvery: Int) : AbstractEnginePool<T>

Abstract base class for a pool of AbstractExprEvaluator instances.

Each evaluator's context is reset once it has been returned resetEvery times, so engine state such as Kotlin's REPL history cannot grow without bound. Resetting is not free: a fresh Kotlin REPL costs several times an evaluation, so resetting on every return would make the pool slower than a lone evaluator. Between resets, a top-level declaration an expression makes (such as val y = 1; y > 0) stays visible to the evaluator's later borrowers. A reset clears variables and declarations, not the interpreter's own state or the JVM's: a module attribute an expression sets in Jython, or a static field it sets anywhere, is still there for the next borrower. Subclasses populate the pool with populate in their init block. AbstractEnginePool describes borrowing and closing.

eval and blockingEval run the expression on the caller's thread and cannot be stopped once it starts: a timeout or cancellation around them bounds only the wait for an evaluator. Their overloads that take a timeout run it on a thread of their own and interrupt it when the timeout expires.

Parameters

size

the number of evaluator instances in the pool; must be positive

resetEvery

how many times an evaluator is returned between resets of its context; 1 resets on every return

Type Parameters

T

the concrete type of AbstractExprEvaluator managed by this pool

Throws

if size or resetEvery is not positive

Constructors

Link copied to clipboard
constructor(size: Int, resetEvery: Int)
constructor(size: Int)

Creates a pool that resets each evaluator's context every time it is returned.

Properties

Link copied to clipboard

Returns an approximate, point-in-time indication of whether every place in the pool is currently borrowed, so that a borrower would have to wait. Delegates to Channel.isEmpty, which is racy under concurrent borrow/recycle and may return a stale result, so do not rely on it for correctness.

Link copied to clipboard
Link copied to clipboard
val size: Int

Functions

Link copied to clipboard

Evaluates expr by borrowing an evaluator from the pool, blocking the current thread until one is available and recycling it afterwards.

fun blockingEval(expr: String, timeout: Duration): Boolean

blockingEval with a timeout given as a java.time.Duration, for Java callers: the overload taking a Kotlin Duration cannot be called from Java.

fun blockingEval(expr: String, timeout: Duration): Boolean

Like blockingEval, but runs the expression on a thread of its own and fails with a ScriptTimeoutException if it does not finish within timeout. See eval with a timeout; the wait for an evaluator does not count toward timeout.

Link copied to clipboard
open override fun close()

Closes the pool and the instances it holds. An instance still borrowed is closed when it is returned, and later borrows throw kotlinx.coroutines.channels.ClosedReceiveChannelException. Every instance the pool holds is closed even when closing one throws; the first failure then propagates, with the later ones attached as suppressed. An instance whose timed evaluation was abandoned is closed when that evaluation returns, if it ever does.

Link copied to clipboard
suspend fun eval(expr: String): Boolean

Suspends until an evaluator can be borrowed from the pool, evaluates expr, and recycles the evaluator afterwards. Suspends (rather than blocking) when all size evaluators are in use.

suspend fun eval(expr: String, timeout: Duration): Boolean

Like eval, but runs the expression on a thread of its own and waits at most timeout for it; the wait for an evaluator does not count toward timeout. If the expression does not finish in time, its thread is interrupted, the evaluator is taken out of the pool for good and a fresh one takes its place, and a ScriptTimeoutException is thrown. Cancelling the caller while the expression runs stops it the same way.