Skip to content

Commit

Permalink
updated to simplify architecture and allow pooly_sup to be supervised by
Browse files Browse the repository at this point in the history
other supervisors
  • Loading branch information
aberman committed Nov 24, 2011
1 parent 7635215 commit a871eae
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 114 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Recent changes

#### 11/23/2011
* Simplified architecture and now allow pooly_sup to be passed the config file to allow other OTP applications to supervise pooly. To do this, supervise pooly_sup and pass it the file name for the pooly configuration.
* Changed application to look for an environment variable to tell it where the pooly.conf file is located. This should improve portability when included in other applications.

#### 8/14/2011
Expand All @@ -17,7 +18,6 @@
3. Create an app config file for your application which includes:

```
[{pooly, [{config_file, LocationOfFile}]}].
e.g. [{pooly, [{config_file, "priv/pooly.conf"}]}].
Expand Down
2 changes: 1 addition & 1 deletion src/pooly.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
kernel,
stdlib
]},
{mod, { pooly_app, []}},
{mod, { pooly_sup, []}},
{env, []}
]}.
16 changes: 10 additions & 6 deletions src/pooly.erl
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,18 @@ remove_pid(Pid, State) ->
update_state(#state{} = State) ->
Config = State#state.config,
try
State#state.q_len < Config#config.min_pool_size orelse throw(ready),

MaxPoolSize = Config#config.max_pool_size,
State#state.q_len < Config#config.min_pool_size orelse throw(ready),
Total = State#state.total,
Diff = MaxPoolSize - Total,
Diff + State#state.q_len > 0 orelse throw(exhausted),

Increment = erlang:min(Config#config.acquire_increment, Diff),
Increment = case Config#config.max_pool_size of
infinity ->
Config#config.acquire_increment;
MaxPoolSize ->
Diff = MaxPoolSize - Total,
Diff + State#state.q_len > 0 orelse throw(exhausted),
erlang:min(Config#config.acquire_increment, Diff)
end,

{next_state, ready, State#state{q = queue:join(State#state.q,
queue:from_list(new_process(State, Increment))),
q_len = State#state.q_len + Increment,
Expand Down
80 changes: 0 additions & 80 deletions src/pooly_app.erl

This file was deleted.

12 changes: 10 additions & 2 deletions src/pooly_member_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ start_link(Name) ->
false -> Name
end,

supervisor:start_link({local, list_to_atom(NameStr ++ "_sup")}, ?MODULE, []).
supervisor:start_link({local, list_to_exsiting_atom_internal(NameStr ++ "_sup")}, ?MODULE, []).

init([]) ->
Restart = {simple_one_for_one, 1, 1},
Child = {pooly_member, {pooly_member, start_link, []},
temporary, brutal_kill, worker, [pooly_member]},
{ok, {Restart, [Child]}}.
{ok, {Restart, [Child]}}.

list_to_exsiting_atom_internal(List) when is_list(List) ->
try
list_to_existing_atom(List)
catch
error:_ ->
list_to_atom(List)
end.
93 changes: 69 additions & 24 deletions src/pooly_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,73 @@
%%% -------------------------------------------------------------------
-module(pooly_sup).

-behaviour(application).
-behaviour(supervisor).
%% --------------------------------------------------------------------
%% External exports
%% --------------------------------------------------------------------
-export([
start_link/0
]).

%% --------------------------------------------------------------------
%% Internal exports
%% --------------------------------------------------------------------
-export([
init/1
]).

-define(SERVER, ?MODULE).

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
Restart = {simple_one_for_one, 1, 1},
Child = {pooly_pool_sup, {pooly_pool_sup, start_link, []},
permanent, 5000, supervisor, [pooly_pool_sup]},
{ok, {Restart, [Child]}}.

-define(DEFAULT_ACQUIRE_INCREMENT, 3).
-define(DEFAULT_INITIAL_POOL_SIZE, 5).
-define(DEFAULT_MAX_POOL_SIZE, infinity).
-define(DEFAULT_MIN_POOL_SIZE, 3).
-define(DEFAULT_IDLE_TIMEOUT, 2 * 60 * 60 * 1000).
-define(DEFAULT_MAX_AGE, infinity).

-define(CONFIG_KEYS, [acquire_increment, initial_pool_size, max_pool_size, min_pool_size, idle_timeout, max_age]).
-define(POOL_WORKER_KEYS, [module, args]).

-include("record.hrl").


%% Application callbacks
-export([start/2, stop/1]).

%% Supervisor callbacks
-export([start_link/1, init/1]).

%% ===================================================================
%% Application callbacks
%% ===================================================================
start(_StartType, _StartArgs) ->
ConfigFile = case application:get_env(config_file) of
undefined -> exit({config_not_set, "Set the config_file application parameter"});
{ok, Val} -> Val
end,
pooly_sup:start_link(ConfigFile).

stop(_State) ->
ok.

start_link(ConfigFile) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, [ConfigFile]).

init([ConfigFile]) ->
{ok, Conf} = file:consult(ConfigFile),
PoolProps = lists:filter(fun({Key, _}) -> not lists:member(Key, ?CONFIG_KEYS) end, Conf),
% Get Any Default Settings

DefaultConfig = #config{
acquire_increment = proplists:get_value(acquire_increment, Conf, ?DEFAULT_ACQUIRE_INCREMENT),
initial_pool_size = proplists:get_value(initial_pool_size, Conf, ?DEFAULT_INITIAL_POOL_SIZE),
max_pool_size = proplists:get_value(max_pool_size, Conf, ?DEFAULT_MAX_POOL_SIZE),
min_pool_size = proplists:get_value(min_pool_size, Conf, ?DEFAULT_MIN_POOL_SIZE),
idle_timeout = proplists:get_value(idle_timeout, Conf, ?DEFAULT_IDLE_TIMEOUT),
max_age = proplists:get_value(max_age, Conf, ?DEFAULT_MAX_AGE)
},
Pools = lists:map(
fun({Name, Props}) ->
Mod = proplists:get_value(module, Props),
Mod =/= undefined orelse exit({error, bad_pooly_config}),
{Name, {pooly_pool_sup, start_link, [Name, DefaultConfig#config{
module = Mod,
args = proplists:get_value(args, Props, []),
acquire_increment = proplists:get_value(acquire_increment, Props, DefaultConfig#config.acquire_increment),
initial_pool_size = proplists:get_value(initial_pool_size, Props, DefaultConfig#config.initial_pool_size),
max_pool_size = proplists:get_value(max_pool_size, Props, DefaultConfig#config.max_pool_size),
min_pool_size = proplists:get_value(min_pool_size, Props, DefaultConfig#config.min_pool_size),
idle_timeout = proplists:get_value(idle_timeout, Props, DefaultConfig#config.idle_timeout),
max_age = proplists:get_value(max_age, Props, DefaultConfig#config.max_age)
}]}, permanent, 5000, supervisor, [pooly_pool_sup]}
end
, PoolProps),

Restart = {one_for_one, 10, 10},
{ok, {Restart, Pools}}.

0 comments on commit a871eae

Please sign in to comment.