This commit is contained in:
Feng 2016-01-02 14:46:20 +08:00
parent a8c8908851
commit deeaa491ca
3 changed files with 118 additions and 3 deletions

11
CHANGES Normal file
View File

@ -0,0 +1,11 @@
0.2-beta (2016/01/02)
---------------------
Eunit tests and first release.
0.1-beta (2015/12/30)
---------------------
First commit.

101
README.md
View File

@ -1,2 +1,99 @@
# ecpool
Erlang Connection/Client Pool Library
# Erlang Connection/Client Pool
ecpool is different with worker-pool libraries in that it is designed to pool connection/clients to server or database.
ecpool tries to avoid the erlang application crash when the server or dabased going down.
## Overview
A pool worker to manage and monitor the client to server or database:
```
PoolWorker -> Client -> DB
```
Use client:
```
ecpool:with_client(Pool, fun(Client) -> call(Client) end).
```
## Usage
### Start the pool
```
ecpool:start_pool(Pool, Mod, Opts).
```
For example:
```
PgOpts = [%% Pool Size
{pool_size, 10},
%% Pool Type: random | hash | round_robin
{pool_type, random},
%% Auto reconnect
{auto_reconnect, 3},
%% epgsql opts
{host, "localhost"},
{port, 5432},
{username, "feng"},
{password, ""},
{database, "mqtt"},
{encoding, utf8}],
ecpool:start_pool(epgsql_pool, epgsql_pool_client, PgOpts)
```
### The Callback Module
Pool Worker Callback:
```
-callback connect(ConnOpts :: list()) -> {ok, pid()} | {error, any()}.
```
For example, epgsql_pool_client module:
```
-module(epgsql_pool_client).
-behavihour(ecpool_worker).
connect(Opts) ->
Host = proplists:get_value(host, Opts),
Username = proplists:get_value(username, Opts),
Password = proplists:get_value(password, Opts),
epgsql:connect(Host, Username, Password, conn_opts(Opts)).
squery(Pool, Sql) ->
ecpool:with_client(Pool, fun(Client) -> epgsql:squery(Client, Sql) end).
```
## Design
The ecpool supervisor tree:
```
pool_sup[one_for_all supervisor]
pool[gen-server]
worker_sup[one_for_one supervisor]
worker1 -> connection1
worker2 -> connection1
....
```
## Author
Feng Lee <feng@emqtt.io>
## License
The MIT License (MIT)

View File

@ -29,8 +29,15 @@
-export([start_pool/3, start_sup_pool/3, stop_sup_pool/1,
with_client/2, with_client/3, name/1, workers/1]).
-type pool_type() :: random | hash | round_robin.
-type option() :: {pool_size, pos_integer()}
| {pool_type, pool_type()}
| {auto_reconnect, false | pos_integer()}
| tuple().
%% @doc Start the pool
-spec start_pool(atom(), atom(), list()) -> {ok, pid()} | {error, any()}.
-spec start_pool(atom(), atom(), [option()]) -> {ok, pid()} | {error, any()}.
start_pool(Pool, Mod, Opts) when is_atom(Pool) ->
ecpool_pool_sup:start_link(Pool, Mod, Opts).