AbstractEnginePool

abstract class AbstractEnginePool<T : AbstractEngine>(val size: Int) : Closeable

Abstract base class for a fixed-size pool of script engines, shared by AbstractScriptPool and AbstractExprEvaluatorPool.

Uses a coroutine Channel as a bounded buffer of instances. Subclasses create the instances with populate in their init block, borrow one with withInstance, and prepare a returned instance for its next borrower in reset. An instance handed to a borrower that is cancelled before it resumes goes back into the pool instead of being lost. Closing the pool closes its instances.

An instance the pool can no longer trust is closed and discarded rather than returned: one whose reset threw, and one whose timed evaluation did not finish in time (see withInstance with a timeout). Its place is not lost: the next borrower to take it gets a fresh instance from the factory given to populate. If creating that instance throws, that borrower gets the exception and the place stays empty for the next borrower to try again, so the pool keeps its size, and no borrower waits for an instance that is gone.

Parameters

size

the number of instances in the pool; must be positive

Type Parameters

T

the type of engine in the pool

Throws

if size is not positive

Inheritors

Constructors

Link copied to clipboard
constructor(size: Int)

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
val size: Int

Functions

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.