-
Notifications
You must be signed in to change notification settings - Fork 2
/
README
83 lines (61 loc) · 2.62 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
NOMENCLATURE
------------
Endpoint: A database system, or any other connection-oriented service.
INTRODUCTION
------------
connection_pool is a generic connection pool manager for Erlang applications.
Goals:
- Allow pools to be started by OTP supervisors.
- Assign symbolic names to pools (e.g. 'redis.master', 'redis.slave:0', 'redis.slave:1').
- Return 'dead_enpoint' if all connections to an endpoint go away. This allows the caller to call another endpoint or return acceptable empty results.
- Try to re-establish dead connections.
- Make it easy to support new types of endpoints.
SUPPORTED ENDPOINT TYPES
------------------------
Currently, the following types of endpoints are supported:
PostgreSQL with epgsql driver (https://github.com/wg/epgsql)
MySQL with http://code.google.com/p/erlang-mysql-driver/ and mysql_conn2.erl (in this package)
NOTE: Don't use MySQL if you have a choice. Use PostgreSQL.
Redis with Redo driver (https://github.com/JacobVorreuter/redo)
NOTE: If you are using Erldis, we highly recommend using Redo instead.
DEFINING ENDPOINTS
------------------
Example of a set of master-slaves endpoint definitions for Redis:
-define(REDIS_NODES, [
{'redis:0', [{max_conns, 4}, {'conn_opts', {'192.168.0.10', 6379}}]},
{'redis:1', [{max_conns, 4}, {'conn_opts', {'192.168.0.11', 6379}}]},
{'redis:2', [{max_conns, 4}, {'conn_opts', {'192.168.0.12', 6379}}]},
{'redis:3', [{max_conns, 4}, {'conn_opts', {'192.168.0.13', 6379}}]}
]).
GENERATING CHILD SPECS
----------------------
redo_pool_specs() ->
[redo_pool:spec(Details) || Details <- ?REDIS_NODES].
% supervisor
init([]) ->
{ok, {{one_for_one, 3, 40}, redo_pool_specs()}}.
BASIC USAGE
-----------
redis_cmd(PoolName, F) ->
connection_pool:run({redo_pool, PoolName}, F).
get_foo_bar() ->
[Foo, Bar] = redis_cmd('default:0', fun(C) ->
redo:cmd(C, [
[<<"get">>, <<"foo">>],
[<<"get">>, <<"bar">>]
])
end).
POSTGRESQL POOL STARTED BY SUPERVISOR
-------------------------------------
psql_pool_specs() ->
PsqlNodes = [{'db:0', [{max_conns, 2}, {conn_opts, ["127.0.0.1", "user", "pass", [{database, "db0"}]]}]}],
[psql_pool:spec(Details) || Details <- PsqlNodes].
init([]) ->
{ok, {{one_for_one, 3, 40}, psql_pool_specs()}}.
POSTGRESQL POOL STARTED DIRECTLY (WITHOUT SUPERVISOR)
-----------------------------------------------------
foo() ->
[{PoolName, EndpointDetails}] = [{'db:0', [{max_conns, 1}, {conn_opts, ["127.0.0.1", "user", "pass", [{database, "db0"}]]}]}],
{ok, Pool} = connection_pool:start_link({psql_pool, Name}, Details),
% do stuff
connection_pool:close(Pool).