Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use an asset pool to manage multiple handlers
- Loading branch information
Showing
6 changed files
with
132 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
-module(asset_pool). | ||
-behaviour(gen_server). | ||
|
||
%% api | ||
-export([start_link/1, start/1, lease_asset/0, return_asset/1]). | ||
|
||
%% gen_server callbacks | ||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, | ||
terminate/2, code_change/3]). | ||
|
||
-record(state, {assets = undefined, | ||
handler = undefined}). | ||
|
||
%%==================================================================== | ||
%% API | ||
%%==================================================================== | ||
|
||
start_link(Args) -> | ||
gen_server:start_link({global, ?MODULE}, ?MODULE, Args, []). | ||
|
||
start(Args) -> | ||
gen_server:start({global, ?MODULE}, ?MODULE, Args, []). | ||
|
||
lease_asset() -> | ||
gen_server:call({global, ?MODULE}, {lease_asset}). | ||
|
||
return_asset(Asset) -> | ||
gen_server:call({global, ?MODULE}, {return_asset, Asset}). | ||
|
||
%%==================================================================== | ||
%% gen_server callbacks | ||
%%==================================================================== | ||
|
||
%%-------------------------------------------------------------------- | ||
%% Function: init(Args) -> {ok, State} | | ||
%% {ok, State, Timeout} | | ||
%% ignore | | ||
%% {stop, Reason} | ||
%% Description: Initiates the server | ||
%%-------------------------------------------------------------------- | ||
init([Count, Handler]) -> | ||
process_flag(trap_exit, true), | ||
error_logger:info_msg("~p starting~n", [?MODULE]), | ||
Assets = start_handlers(Count, Handler), | ||
{ok, #state{assets = Assets, handler = Handler}}. | ||
|
||
%%-------------------------------------------------------------------- | ||
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} | | ||
%% {reply, Reply, State, Timeout} | | ||
%% {noreply, State} | | ||
%% {noreply, State, Timeout} | | ||
%% {stop, Reason, Reply, State} | | ||
%% {stop, Reason, State} | ||
%% Description: Handling call messages | ||
%%-------------------------------------------------------------------- | ||
handle_call({lease_asset}, _From, State) -> | ||
{{value, Asset}, Assets2} = queue:out(State#state.assets), | ||
{reply, Asset, State#state{assets = Assets2}}; | ||
handle_call({return_asset, Asset}, _From, State) -> | ||
Assets2 = queue:in(Asset, State#state.assets), | ||
{reply, ok, State#state{assets = Assets2}}; | ||
handle_call(_Request, _From, State) -> | ||
{reply, ok, State}. | ||
|
||
%%-------------------------------------------------------------------- | ||
%% Function: handle_cast(Msg, State) -> {noreply, State} | | ||
%% {noreply, State, Timeout} | | ||
%% {stop, Reason, State} | ||
%% Description: Handling cast messages | ||
%%-------------------------------------------------------------------- | ||
handle_cast(_Msg, State) -> {noreply, State}. | ||
|
||
handle_info({'EXIT', _Pid, _Error}, State) -> | ||
error_logger:error_msg("Port closed, restarting port...~n", []), | ||
Handler = State#state.handler, | ||
Asset = port_wrapper:wrap_link("ruby " ++ Handler), | ||
Assets = queue:in(Asset, State#state.assets), | ||
{noreply, State#state{assets = Assets}}; | ||
handle_info(Msg, State) -> | ||
error_logger:error_msg("Unexpected message: ~p~n", [Msg]), | ||
{noreply, State}. | ||
|
||
terminate(_Reason, _State) -> ok. | ||
code_change(_OldVersion, State, _Extra) -> {ok, State}. | ||
|
||
%%==================================================================== | ||
%% Internal | ||
%%==================================================================== | ||
|
||
start_handlers(Count, Handler) -> | ||
start_handlers(queue:new(), Count, Handler). | ||
|
||
start_handlers(Assets, 0, _Handler) -> | ||
Assets; | ||
start_handlers(Assets, Count, Handler) -> | ||
Asset = port_wrapper:wrap_link("ruby " ++ Handler), | ||
Assets2 = queue:in(Asset, Assets), | ||
start_handlers(Assets2, Count - 1, Handler). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-module(asset_pool_sup). | ||
-behaviour(supervisor). | ||
-export([start_link/0, init/1]). | ||
|
||
start_link() -> | ||
supervisor:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
init([]) -> | ||
{ok, Handler} = application:get_env(ernie_server_app, handler), | ||
io:format("Using handler ~p~n", [Handler]), | ||
{ok, {{one_for_one, 1, 60}, | ||
[{asset_pool, {asset_pool, start_link, [[2, Handler]]}, | ||
permanent, brutal_kill, worker, [asset_pool]}]}}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) | ||
require 'ernie' | ||
|
||
mod(:calc) do | ||
fun(:add) do |a, b| | ||
a + b | ||
end | ||
end |