Skip to content
Newer
Older
100644 167 lines (134 sloc) 4.11 KB
cd1174c Initial import
Devin Torres authored
1 Poolboy - A hunky Erlang worker pool factory
2 ============================================
3
9ebddfc @devinus Add .travis.yml for Travis CI and build status badge
authored
4 [![Build Status](https://secure.travis-ci.org/devinus/poolboy.png?branch=master)](http://travis-ci.org/devinus/poolboy)
5
4f19aad Use nicer Markdown header format in README
Devin Torres authored
6 Usage
7 -----
cd1174c Initial import
Devin Torres authored
8
4987c10 @devinus Several additions and improvements
authored
9 ```erl-sh
10 1> Worker = poolboy:checkout(PoolName).
11 <0.9001.0>
12 2> gen_server:call(Worker, Request).
13 ok
14 3> poolboy:checkin(PoolName, Worker).
15 ok
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
16 ```
cd1174c Initial import
Devin Torres authored
17
9aa1a6a @devinus Add poolboy_worker behaviour
authored
18 Example
19 -------
20
21 This is an example application showcasing database connection pools using
22 Poolboy and Will Glozer's [epgsql](https://github.com/wg/epgsql).
cd1174c Initial import
Devin Torres authored
23
24 ### example.app
25
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
26 ```erlang
27 {application, example, [
28 {description, "An example application"},
29 {vsn, "0.1"},
30 {applications, [kernel, stdlib, sasl, crypto, ssl]},
31 {modules, [example, example_worker]},
32 {registered, [example]},
33 {mod, {example, []}},
34 {env, [
35 {pools, [
36 {pool1, [
d35583a 1. Fixed pool spec in README file (it should be 'size' variable, not …
Nikolay A. Panov authored
37 {size, 10},
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
38 {max_overflow, 20},
39 {hostname, "127.0.0.1"},
40 {database, "db1"},
41 {username, "db1"},
42 {password, "abc123"}
43 ]},
44 {pool2, [
d35583a 1. Fixed pool spec in README file (it should be 'size' variable, not …
Nikolay A. Panov authored
45 {size, 5},
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
46 {max_overflow, 10},
47 {hostname, "127.0.0.1"},
48 {database, "db2"},
49 {username, "db2"},
50 {password, "abc123"}
cd1174c Initial import
Devin Torres authored
51 ]}
52 ]}
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
53 ]}
54 ]}.
55 ```
cd1174c Initial import
Devin Torres authored
56
57 ### example.erl
58
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
59 ```erlang
60 -module(example).
61 -behaviour(application).
62 -behaviour(supervisor).
63
f2bfc2b @devinus 'query' is a historical reserved word in Erlang
authored
64 -export([start/0, stop/0, squery/2, equery/3]).
9aa1a6a @devinus Add poolboy_worker behaviour
authored
65 -export([start/2, stop/1]).
66 -export([init/1]).
67
68 start() ->
69 application:start(?MODULE).
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
70
9aa1a6a @devinus Add poolboy_worker behaviour
authored
71 stop() ->
72 application:stop(?MODULE).
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
73
74 start(_Type, _Args) ->
75 supervisor:start_link({local, example_sup}, ?MODULE, []).
9aa1a6a @devinus Add poolboy_worker behaviour
authored
76
77 stop(_State) ->
78 ok.
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
79
80 init([]) ->
81 {ok, Pools} = application:get_env(example, pools),
82 PoolSpecs = lists:map(fun({PoolName, PoolConfig}) ->
4f19aad Use nicer Markdown header format in README
Devin Torres authored
83 Args = [{name, {local, PoolName}},
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
84 {worker_module, example_worker}]
85 ++ PoolConfig,
4987c10 @devinus Several additions and improvements
authored
86 poolboy:child_spec(PoolName, Args)
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
87 end, Pools),
9c385e0 Change example supervisor strategy to one_for_one
Devin Torres authored
88 {ok, {{one_for_one, 10, 10}, PoolSpecs}}.
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
89
f2bfc2b @devinus 'query' is a historical reserved word in Erlang
authored
90 squery(PoolName, Sql) ->
4987c10 @devinus Several additions and improvements
authored
91 poolboy:transaction(PoolName, fun(Worker) ->
92 gen_server:call(Worker, {squery, Sql})
93 end).
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
94
f2bfc2b @devinus 'query' is a historical reserved word in Erlang
authored
95 equery(PoolName, Stmt, Params) ->
4987c10 @devinus Several additions and improvements
authored
96 poolboy:transaction(PoolName, fun(Worker) ->
97 gen_server:call(Worker, {equery, Stmt, Params})
98 end).
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
99 ```
cd1174c Initial import
Devin Torres authored
100
0c5a6ff Fix documentation typo
Devin Torres authored
101 ### example_worker.erl
cd1174c Initial import
Devin Torres authored
102
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
103 ```erlang
104 -module(example_worker).
105 -behaviour(gen_server).
9aa1a6a @devinus Add poolboy_worker behaviour
authored
106 -behaviour(poolboy_worker).
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
107
9aa1a6a @devinus Add poolboy_worker behaviour
authored
108 -export([start_link/1]).
109 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
110 code_change/3]).
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
111
112 -record(state, {conn}).
113
9aa1a6a @devinus Add poolboy_worker behaviour
authored
114 start_link(Args) ->
115 gen_server:start_link(?MODULE, Args, []).
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
116
117 init(Args) ->
118 Hostname = proplists:get_value(hostname, Args),
119 Database = proplists:get_value(database, Args),
120 Username = proplists:get_value(username, Args),
121 Password = proplists:get_value(password, Args),
122 {ok, Conn} = pgsql:connect(Hostname, Username, Password, [
123 {database, Database}
124 ]),
125 {ok, #state{conn=Conn}}.
126
f2bfc2b @devinus 'query' is a historical reserved word in Erlang
authored
127 handle_call({squery, Sql}, _From, #state{conn=Conn}=State) ->
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
128 {reply, pgsql:squery(Conn, Sql), State};
f2bfc2b @devinus 'query' is a historical reserved word in Erlang
authored
129 handle_call({equery, Stmt, Params}, _From, #state{conn=Conn}=State) ->
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
130 {reply, pgsql:equery(Conn, Stmt, Params), State};
131 handle_call(_Request, _From, State) ->
132 {reply, ok, State}.
133
134 handle_cast(_Msg, State) ->
135 {noreply, State}.
136
137 handle_info(_Info, State) ->
138 {noreply, State}.
139
140 terminate(_Reason, #state{conn=Conn}) ->
4987c10 @devinus Several additions and improvements
authored
141 ok = pgsql:close(Conn),
2803bad Add syntax highlighting to Erlang examples in README
Devin Torres authored
142 ok.
143
144 code_change(_OldVsn, State, _Extra) ->
145 {ok, State}.
146 ```
cd1174c Initial import
Devin Torres authored
147
4f19aad Use nicer Markdown header format in README
Devin Torres authored
148 Options
149 -------
cd1174c Initial import
Devin Torres authored
150
151 - `name`: the pool name
152 - `worker_module`: the module that represents the workers
153 - `size`: maximum pool size
154 - `max_overflow`: maximum number of workers created if pool is empty
1d8c3ea Add Authors section to README
Devin Torres authored
155
156 Authors
157 -------
2b37986 Meh, that didn't work
Devin Torres authored
158 - Devin Torres (devinus) <devin@devintorres.com>
159 - Andrew Thompson (Vagabond) <andrew@hijacked.us>
db36398 Sorry Kurt
Devin Torres authored
160 - Kurt Williams (onkel-dirtus) <kurt.r.williams@gmail.com>
51d875e Make Poolboy optionally available under the Apache License
Devin Torres authored
161
162 License
163 -------
164 Poolboy is available in the public domain (see `UNLICENSE`).
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
165 Poolboy is also optionally available under the Apache License (see `LICENSE`),
166 meant especially for jurisdictions that do not recognize public domain works.
Something went wrong with that request. Please try again.