Usage
This page provides detailed instructions on how to use the Pools.jl package, including explanations of key concepts, API documentation for all exported functions and types, and examples of common usage patterns.
Core Concepts
Resource Pools
A resource pool is a collection of resources that are managed and reused to improve performance. Instead of creating new resources every time they are needed, a pool maintains a set of available resources and distributes them as requested. When a resource is no longer needed, it is returned to the pool for later reuse, avoiding the overhead of creating and destroying resources repeatedly.
Resource Lifecycle
Resources managed by Pools.jl go through a specific lifecycle:
- Creation: New resources are created using the
create(::Type{T})function. - Acquisition: Resources are acquired from the pool using the
acquire!(pool::Pool{T})function. - Validation: Before a resource is given to a user, it is validated using the
check(resource::T)function to ensure it is still valid. - Usage: The resource is used by the application.
- Update: Before a resource is returned to the pool, its state can be updated using the
change!(resource::T)function. - Release: Resources are returned to the pool using the
release!(pool::Pool{T}, resource::Resource{T})function. - Finalization: Resources are finalized (cleaned up) using the
clean!(resource::T)function when they are no longer needed by the pool (e.g., during pool draining or when they fail validation).
Using the Pool
Creating a Pool
To create a resource pool, you need to define your resource type T and implement the create, check, change!, and clean! functions for that type. Then, you can create a pool using the Pool{T}(limit::Int) constructor.
using ConnectionPools
import ConnectionPools: create, check, change!, clean!
# Implement the required functions
create(::Type{T}) = T()
check(::T) = println("Resource validated") # How to validate
change!(::T) = println("Resource updated") # How to update
clean!(::T) = println("Resource finalized") # How to finalize
pool = ConnectionPool{T}(5) # Create a pool with a maximum of 5 resourcesAcquiring a Resource
To acquire a resource from the pool, use the acquire!(pool::Pool{T}) function. This function will either return a free resource from the pool, create a new resource (if below the limit), or block until a resource becomes available.
resource = acquire!(pool)
# ... use the resource ...Releasing a Resource
To release a resource back to the pool, use the release!(pool::Pool{T}, resource::Resource{T}) function.
release!(pool, resource)Using withresource (Recommended)
The recommended way to work with pooled resources is to use the withresource(f::Function, pool::Pool{T}) function. This function automatically acquires a resource, passes it to your function f, and ensures that the resource is released back to the pool, even if errors occur.
withresource(pool) do resource
# ... use the resource ...
end # Resource is automatically released hereDraining the Pool
To release and finalize all resources in the pool, use the drain!(pool::Pool{T}) function. This is typically done when you want to shut down the pool.
drain!(pool)API @autodoc(Pools)