val create : int -> ?validate:('a -> bool Lwt.t) -> ?check:('a -> (bool -> unit) -> unit) -> ?dispose:('a -> unit Lwt.t) -> (unit -> 'a Lwt.t) -> 'a tcreate 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.
- parameter validate
is called each time a pool element is accessed by
use, before the element is provided touse's callback. Ifvalidate elementresolves totruethe element is considered valid and is passed to the callback for use as-is. Ifvalidate elementresolves tofalsethe tested pool element is passed todisposethen dropped, with a new one is created to takeelement's place in the pool.validateis available since Lwt 3.2.0.
- parameter check
is called after the resolution of
use's callback when the resolution is a failed promise.check element is_okmust callis_okexactly once withtrueifelementis still valid andfalseotherwise. Ifcheckcallsis_ok falsethendisposewill be run onelementand the element will not be returned to the pool.
- parameter dispose
is used as described above and by
clearto dispose of all elements in a pool.disposeis not guaranteed to be called on the elements in a pool when the pool is garbage collected.clearshould be used if the elements of the pool need to be explicitly disposed of.
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 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.
- since
- 3.2.0