AbstractExprEvaluatorPool
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
the number of evaluator instances in the pool; must be positive
how many times an evaluator is returned between resets of its context; 1 resets on every return
Type Parameters
the concrete type of AbstractExprEvaluator managed by this pool
Throws
if size or resetEvery is not positive
Properties
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.
Functions
Evaluates expr by borrowing an evaluator from the pool, blocking the current thread until one is available and recycling it afterwards.
blockingEval with a timeout given as a java.time.Duration, for Java callers: the overload taking a Kotlin Duration cannot be called from Java.
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.
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.
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.
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.