Resource pool (rsrc_pool_ex)project is written in Elixir as a tiny library. The goal of the tool is reduce the overhead of creating new resources by reusing of the same resources among multiple processes. Achieving result is better performance and throughput. The resource pool was inspired by Java Apache's commons pool and adopts API and main principals from this project. Database connection is most popular example for pooling resource.
Resource pool project was inspired by Apache Commons Pool library and API was borrowed from there. But internal implementation is completely different, written in Elixir and it is using Erlang OTP design principles and Erlang concurrent model. Resource Pool is Elixir application library.
ResourcePool.GenServer
(resource_pool_srv.ex) is a main module of Resource Pool. It is generic server and implements almost all Pool functionality.ResourcePool
(resource_pool.ex) is a facade for GenServer and exposes all API functions.ResourceFactory
(resource_factory.ex) definesResourceFactory
behaviour.
- Create instance of Resource Pool
where:
{:ok, pid} = ResourcePool.new(:test_pool, ResourceFactory, [])
:test_pool
- registered name of the new pool;ResourceFactory
- name of a module that implementsResourceFactory
behaviour. New resource pool is usually shared between few processes.
- Borrow resource from pool
The process can use the borrowed resource and has to return to pool after finish.
resource = ResourcePool.borrow(:test_pool)
- Return resource to pool
The process cannot use the
:ok = ResourcePool.return(:test_pool, resource)
resource
anymore. - Pool can be created with options:
See ResourcePool for more details about options.
options = [max_active: 10, when_exhausted_action: fail] {:ok, pid} = ResourcePool.new(:test_pool, ResourceFactory, options)
See Resource Pool article for details and http://erlpool.sourceforge.net/.
If available in Hex, the package can be installed
by adding rsrc_pool_ex
to your list of dependencies in mix.exs
:
def deps do
[
{:rsrc_pool_ex, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/rsrc_pool_ex.