Permalink
Browse files

tabs -> spaces

  • Loading branch information...
lukyanov committed Dec 18, 2012
1 parent a166a7f commit 868f7e456d8a0bd8e15e645ef25232ac93294de4
Showing with 63 additions and 61 deletions.
  1. +63 −61 src/portserver.erl
View
@@ -1,29 +1,31 @@
+%%% vim: set ts=4 sts=4 sw=4 et:
+
-module(portserver).
-export([
- start/1,
- start/2,
- start/3,
- start_link/1,
- start_link/2,
- start_link/3,
- stop/1
+ start/1,
+ start/2,
+ start/3,
+ start_link/1,
+ start_link/2,
+ start_link/3,
+ stop/1
]).
-export([
- code_change/3,
- handle_call/3,
- init/1,
- terminate/2,
- handle_info/2,
- handle_cast/2
+ code_change/3,
+ handle_call/3,
+ init/1,
+ terminate/2,
+ handle_info/2,
+ handle_cast/2
]).
-export([
- call/3,
- call/4,
- status/1,
- ping/1
+ call/3,
+ call/4,
+ status/1,
+ ping/1
]).
-behaviour(gen_server).
@@ -48,9 +50,9 @@ start_link(ServerName, PortCommand, Options) ->
stop(ServerRef) ->
case gen_server:call(ServerRef, stop) of
- ok -> ok;
- {error, port_closed} -> ok;
- {error, port_closing} = E -> E
+ ok -> ok;
+ {error, port_closed} -> ok;
+ {error, port_closing} = E -> E
end.
call(ServerRef, Command, Args) ->
@@ -67,46 +69,46 @@ ping(ServerRef) ->
-define(QUEUE_OVERLOAD_LENGTH, 1000).
-record(state, { status = running, port, q = queue:new(), qlen = 0, portcmd,
- overloads = jn_mavg:new_mavg(3600, [{history_length, 48}]) }).
+ overloads = jn_mavg:new_mavg(3600, [{history_length, 48}]) }).
init(PortCommand) ->
process_flag(trap_exit, true),
P = open_port({spawn, PortCommand}, [{packet, 4}, binary, exit_status]),
{Status, Port} = try port_command(P, term_to_binary(ping)) of
- true -> receive
- % The port started under ?MODULE must respond to 'ping' events
- % with a 'pong' answer at any time.
- {P, {data, <<131,100,0,4,"pong">>}} -> {running, P};
- {P, _} -> {start_failure, undefined};
- {'EXIT', P, _} -> {start_failure, undefined}
- after 5000 -> throw(start_timeout)
- end
- catch
- error:badarg -> {start_failure, undefined}
- end,
+ true -> receive
+ % The port started under ?MODULE must respond to 'ping' events
+ % with a 'pong' answer at any time.
+ {P, {data, <<131,100,0,4,"pong">>}} -> {running, P};
+ {P, _} -> {start_failure, undefined};
+ {'EXIT', P, _} -> {start_failure, undefined}
+ after 5000 -> throw(start_timeout)
+ end
+ catch
+ error:badarg -> {start_failure, undefined}
+ end,
process_flag(trap_exit, false),
case Status of
- start_failure -> error_logger:error_msg("Failed to start ~p~n", [PortCommand]);
- running -> ok
+ start_failure -> error_logger:error_msg("Failed to start ~p~n", [PortCommand]);
+ running -> ok
end,
{ok, #state{status = Status, port = Port, portcmd = PortCommand }}.
% First thing, check out that we are not overloaded.
handle_call(Query, From, #state{status = running, port = Port,
- qlen = QLen, portcmd = PortCmd, overloads = Mavg } = State)
- when is_tuple(Query), QLen >= ?QUEUE_OVERLOAD_LENGTH ->
+ qlen = QLen, portcmd = PortCmd, overloads = Mavg } = State)
+ when is_tuple(Query), QLen >= ?QUEUE_OVERLOAD_LENGTH ->
NewState = State#state{ overloads = jn_mavg:bump_mavg(Mavg, 1) },
if
QLen > 2 * ?QUEUE_OVERLOAD_LENGTH ->
- {reply, {error, {queue_overload, QLen}}, NewState};
+ {reply, {error, {queue_overload, QLen}}, NewState};
true ->
- port_command(Port, term_to_binary(Query)),
- case jn_mavg:history(Mavg) of
- {0, _, _} -> error_logger:warning_msg("Port ~p overloaded: ~p~n",
- [PortCmd, element(2, handle_call(status, From, NewState))]);
- _ -> ok % Do not print anything too frequently
- end,
- {noreply, enqueue_request(From, NewState)}
+ port_command(Port, term_to_binary(Query)),
+ case jn_mavg:history(Mavg) of
+ {0, _, _} -> error_logger:warning_msg("Port ~p overloaded: ~p~n",
+ [PortCmd, element(2, handle_call(status, From, NewState))]);
+ _ -> ok % Do not print anything too frequently
+ end,
+ {noreply, enqueue_request(From, NewState)}
end;
% Send the Query into the port.
@@ -134,15 +136,15 @@ handle_call(ping, From, #state{status = running, port = Port} = State) ->
%% Produce a set of status and health values.
handle_call(status, _From, #state{status = Status, qlen = QLen } = State) ->
Reply = [{status, Status}, {queue_length, QLen}]
- ++ case queue:peek(State#state.q) of
- {value, {T, _}} -> [{wait_time,
- now2ms(now()) - now2ms(T)}];
- empty -> []
- end
- ++ case jn_mavg:history(State#state.overloads) of
- {Current, _, Archived} when Current + Archived =:= 0 -> [];
- {C, H, A} -> [{overloads, [C|H] ++ (C+A)}] % Sic!
- end,
+ ++ case queue:peek(State#state.q) of
+ {value, {T, _}} -> [{wait_time,
+ now2ms(now()) - now2ms(T)}];
+ empty -> []
+ end
+ ++ case jn_mavg:history(State#state.overloads) of
+ {Current, _, Archived} when Current + Archived =:= 0 -> [];
+ {C, H, A} -> [{overloads, [C|H] ++ (C+A)}] % Sic!
+ end,
{reply, Reply, State};
handle_call(_Q, _From, #state{status = closing} = State) ->
@@ -155,10 +157,10 @@ handle_cast(_, State) ->
handle_info({Port, {data, Data}}, #state{port = Port} = State) ->
NewState = case queue:out(State#state.q) of
- {{value, {_T, From}}, Q} ->
- gen_server:reply(From, binary_to_term(Data)),
- State#state{q = Q, qlen = State#state.qlen - 1};
- {empty, _} -> State
+ {{value, {_T, From}}, Q} ->
+ gen_server:reply(From, binary_to_term(Data)),
+ State#state{q = Q, qlen = State#state.qlen - 1};
+ {empty, _} -> State
end,
{noreply, NewState};
handle_info({Port, _}, #state{status = closing, port = Port} = State) ->
@@ -171,7 +173,7 @@ handle_info(force_stop, #state{status = running} = State) ->
{stop, port_closed, respond_to_waiting(State, {error, port_closed})};
handle_info(_Info, State) ->
error_logger:warning_msg("Port ~p received unexpected message ~p~n",
- [State#state.portcmd, _Info]),
+ [State#state.portcmd, _Info]),
{noreply, State}.
code_change(_, State, _) ->
@@ -183,11 +185,11 @@ terminate(_, _) ->
%%% INTERNAL FUNCTIONS
enqueue_request(From, #state{q = Q, qlen = QLen } = State) ->
- State#state{ q = queue:in({now(), From}, Q), qlen = QLen + 1 }.
+ State#state{ q = queue:in({now(), From}, Q), qlen = QLen + 1 }.
respond_to_waiting(#state{q = Q} = State, WithMessage) ->
- [gen_server:reply(From, WithMessage) || {_, From} <- queue:to_list(Q)],
- State#state{ q = queue:new(), qlen = 0 }.
+ [gen_server:reply(From, WithMessage) || {_, From} <- queue:to_list(Q)],
+ State#state{ q = queue:new(), qlen = 0 }.
now2ms(Now) -> now2micro(Now) div 1000.

0 comments on commit 868f7e4

Please sign in to comment.