Go to file
Zaiming Shi 597233a325
Merge pull request #25 from ayo-dele/fix-dialyzier-warnings
fix(ecpool.erl): Dialyzer warnings
2020-11-25 13:50:03 +01:00
include refactor(with_client): change with_client/2,3 to pick_and_do/3 (#24) 2020-11-24 09:22:46 +08:00
src modify appup version config. 2020-11-25 13:47:53 +01:00
test refactor(with_client): change with_client/2,3 to pick_and_do/3 (#24) 2020-11-24 09:22:46 +08:00
.gitignore Remove the on_reconnect from pool opts 2020-03-25 10:49:17 +08:00
.travis.yml Not compatible with 20 because ecpool has used logger from otp 21.0 2019-08-26 16:38:34 +08:00
LICENSE Update README and LICENSE 2018-12-18 13:32:38 +08:00
Makefile Add make cover in makefile 2019-08-26 16:32:47 +08:00
README.md 0.3.1 - Improve the ecpool library for rule engine 2019-03-15 16:59:35 +08:00
rebar.config Merge branch 'master' into develop 2019-08-26 14:38:32 +08:00
rebar.lock Fix problems on develop branch 2019-08-26 16:28:53 +08:00

README.md

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 database going down.

Overview

A pool worker to manage/monitor the connection to server or database:

PoolWorker -> Conn -> 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: round_robin | random | hash
          {pool_type, round_robin},
          %% 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@emqx.io

License

The Apache License Version 2.0