resource-pooling
Library
Module
Module type
Parameter
Class
Class type
val create :
?validate:( 'a -> bool Lwt.t ) ->
?check:( exn -> 'a -> bool Lwt.t ) ->
?dispose:( 'a -> unit Lwt.t ) ->
int ->
( unit -> 'a Lwt.t ) ->
'a t
create n ?check ?validate ?dispose f
creates a new pool with at most n
elements. f
is used to create a new pool element. Elements are created on demand and re-used until disposed of. f
may raise the exception Resource_invalid
to signal a failed resource creation. In this case use
will re-attempt to create the resource (according to creation_attempts
).
val set_max : 'a t -> int -> unit
set the maximum size of the pool
exception to be thrown by the function supplied to use
when a resource is no longer valid and therefore to be disposed of; safe
determines whether it is safe to relaunch the function supplied to use
. Typically this refers to whether side-effects (that should not occur a second time) have been effectuated before the exception was raised.
use p f
requests one free element of the pool p
and gives it to the function f
. The element is put back into the pool after the promise created by f
completes.
In case the resource supplied to f
is no longer valid, f
can throw a Resource_invalid
exception in which case the resource is disposed of.
The parameter creation_attempts
(default: 1
) controls the number of resource creation attempts that are made in case the creation function raises the Resource_invalid
exception.
The parameter usage_attempts
(default: 1
) controls the number of attempts that are made in case f
raises the Resource_invalid
exception with safe
set to true. After each attempt the resource is disposed of.
In the case that p
is exhausted and the maximum number of elements is reached, use
will wait until one becomes free.
clear p
will clear all elements in p
, calling the dispose
function associated with p
on each of the cleared elements. Any elements from p
which are currently in use will be disposed of once they are released.
The next call to use p
after clear p
guarantees a freshly created pool element.
Disposals are performed sequentially in an undefined order.
val add : ?omit_max_check:bool -> 'a t -> 'a -> unit
By add p c
you can add an existing resource element c
to pool p
. This function may raise a Resource_limit_exceeded
exception. If omit_max_check
is true
(default: false
), then this exception will not be raised. Instead the maximum number of resources might be exceeded and more than p.max
elements will be available to the user.