riemann.pool

A generic thread-safe resource pool.

fixed-pool

(fixed-pool open)(fixed-pool open opts)(fixed-pool open close opts)

A fixed pool of thingys. (open) is called to generate a thingy. (close thingy) is called when a thingy is invalidated. When thingys are invalidated, the pool will immediately try to open a new one; if open throws or returns nil, the pool will sleep for regenerate-interval seconds before retrying (open).

:regenerate-interval How long to wait between retrying (open). :size Number of thingys in the pool. :block-start Should (fixed-pool) wait until the pool is full before returning?

Note that fixed-pool is correct only if every successful (claim) is followed by exactly one of either (invalidate) or (release). If calls are unbalanced; e.g. resources are not released, doubly released, or released and invalidated, starvation or unbounded blocking could occur. (with-pool) provides this guarantee.

Pool

protocol

members

claim

(claim pool)(claim pool timeout)

Take a thingy from the pool. Timeout in seconds; if unspecified, 0. Returns nil if no thingy available.

grow

(grow pool)

Adds an element to the pool.

invalidate

(invalidate pool thingy)

Tell the pool a thingy is no longer valid.

release

(release pool thingy)

Returns a thingy to the pool.

with-pool

macro

(with-pool [thingy pool timeout] & body)

Evaluates body in a try expression with a symbol ’thingy claimed from the given pool, with specified claim timeout. Releases thingy at the end of the body, or if an exception is thrown, invalidates them and rethrows. Example:

; With client, taken from connection-pool, waiting 5 seconds to claim, send ; client a message. (with-pool [client connection-pool 5] (send client a-message))