alekras / ex.rsrc_pool

Resource pool is implemented 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. Database connection is most popular example for pooling resource.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Resource Pool - Getting started

Summary

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.

Introduction

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.

Structure

  • 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) defines ResourceFactory behaviour.

Getting started

  1. Create instance of Resource Pool
    {:ok, pid} = ResourcePool.new(:test_pool, ResourceFactory, [])
    
    where:
    • :test_pool - registered name of the new pool;
    • ResourceFactory - name of a module that implements ResourceFactory behaviour. New resource pool is usually shared between few processes.
  2. Borrow resource from pool
    resource = ResourcePool.borrow(:test_pool)
    
    The process can use the borrowed resource and has to return to pool after finish.
  3. Return resource to pool
    :ok = ResourcePool.return(:test_pool, resource)
    
    The process cannot use the resource anymore.
  4. Pool can be created with options:
    options = [max_active: 10, when_exhausted_action: fail]
    {:ok, pid} = ResourcePool.new(:test_pool, ResourceFactory, options)
    
    See ResourcePool for more details about options.

See Resource Pool article for details and http://erlpool.sourceforge.net/.

Installation

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.

About

Resource pool is implemented 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. Database connection is most popular example for pooling resource.

License:Apache License 2.0


Languages

Language:Elixir 100.0%