Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply rebar3 formatting #4

Merged
merged 3 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ os:
- linux

otp_release:
- 20.3
- 21.3

notifications:
Expand Down
9 changes: 6 additions & 3 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
debug_info
]}.

{minimum_otp_vsn, "20"}.
{minimum_otp_vsn, "21"}.

{cover_enabled, true}.

Expand All @@ -38,9 +38,12 @@
deprecated_functions
]}.

{alias, [{test, [xref, dialyzer, lint, ct, cover]}]}.
{alias, [{test, [format, lint, xref, dialyzer, ct, cover]}]}.

{plugins, [
{rebar3_lint, "0.1.10"},
rebar3_format,
rebar3_lint,
rebar3_hex
]}.

{format, [{files, ["src/*.erl", "test/*.erl"]}]}.
12 changes: 4 additions & 8 deletions src/spillway.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@
%%-------------------------------------------------------------------
-module(spillway).

-export([enter/2,
enter/3,
leave/1,
leave/2,
cur/1,
state/0]).
-export([enter/2, enter/3, leave/1, leave/2, cur/1, state/0]).

%%%===================================================================
%%% External functions
Expand All @@ -40,7 +35,8 @@
enter(Name, Limit) ->
enter(Name, 1, Limit).

-spec enter(term(), non_neg_integer(), non_neg_integer()) -> false | {true, non_neg_integer()}.
-spec enter(term(), non_neg_integer(), non_neg_integer()) -> false |
{true, non_neg_integer()}.
enter(Name, Size, Limit) when Size > 0 ->
spillway_srv:enter(Name, Size, Limit).

Expand All @@ -54,7 +50,6 @@ leave(Name) ->
leave(Name, Size) ->
spillway_srv:leave(Name, Size).


%% @doc Return the current counter value. It will return 0 if it does not exist.
-spec cur(term()) -> non_neg_integer().
cur(Name) ->
Expand All @@ -63,3 +58,4 @@ cur(Name) ->
%% @doc For debug purposes. Returns the state of all counters.
state() ->
spillway_srv:state().

1 change: 1 addition & 0 deletions src/spillway_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ start(_StartType, _StartArgs) ->
stop(_State) ->
ok.


%%====================================================================
%% Internal functions
%%====================================================================
98 changes: 48 additions & 50 deletions src/spillway_srv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,81 +26,76 @@
-behaviour(gen_server).

%% API
-export([
start_link/0,
enter/3,
leave/2,
cur/1,
state/0]).

-export([start_link/0, enter/3, leave/2, cur/1, state/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).

-define(SERVER, ?MODULE).
-define(TID, spillway).

-record(counter, {name,
value = 0 :: non_neg_integer()}).
-record(counter, {name, value = 0 :: non_neg_integer()}).

%%%===================================================================
%%% External functions
%%%===================================================================

%% Attempt to increment the named counter, respecting the given limit. If the counter was
%% successfully incremented, return {true, NewValue}. Otherwise, return false.
-spec enter(term(), non_neg_integer(), non_neg_integer()) -> false | {true, non_neg_integer()}.
-spec enter(term(), non_neg_integer(), non_neg_integer()) -> false |
{true, non_neg_integer()}.
enter(Name, Size, Limit) when Size > 0 ->
case cur(Name) of
Value when Value + Size > Limit ->
false;
_ ->
%% note: update_counter accepts a list of operations. we need to know whether
%% we were the process to successfully increment a limit-reaching value, so we
%% use an initial non-incrementing operation to read the existing value. if
%% the result is [X, X+Size], we successfully incremented the counter. if we
%% failed, the result will be [X, X].

[OldValue, NewValue] = ets:update_counter(?TID, Name,
[{#counter.value, 0},
{#counter.value, Size, Limit, Limit}],
#counter{name = Name}),

Expected = OldValue + Size,
case NewValue of
OldValue ->
%% We did not increment
false;
Expected ->
%% We incremented
{true, Expected};
Limit ->
%% We incremented over the limit so limit is set
{true, Limit}
end
Value when Value + Size > Limit ->
false;
_ ->
%% note: update_counter accepts a list of operations. we need to know whether
%% we were the process to successfully increment a limit-reaching value, so we
%% use an initial non-incrementing operation to read the existing value. if
%% the result is [X, X+Size], we successfully incremented the counter. if we
%% failed, the result will be [X, X].
[OldValue, NewValue] = ets:update_counter(?TID,
Name,
[{#counter.value, 0},
{#counter.value, Size, Limit, Limit}],
#counter{name = Name}),
Expected = OldValue + Size,
case NewValue of
OldValue ->
%% We did not increment
false;
Expected ->
%% We incremented
{true, Expected};
Limit ->
%% We incremented over the limit so limit is set
{true, Limit}
end
end.


%% Attempt to decrement the named counter, with a lower limit of 0. Return the new value.
%% The counter must already exist.
-spec leave(term(), non_neg_integer()) -> non_neg_integer().
leave(Name, Size) ->
ets:update_counter(?TID, Name, {#counter.value, -Size, 0, 0}).


%% Return the current counter value.
-spec cur(term()) -> non_neg_integer().
cur(Name) ->
try
ets:lookup_element(?TID, Name, #counter.value)
ets:lookup_element(?TID, Name, #counter.value)
catch
error:badarg ->
0
error:badarg ->
0
end.

%% For debug purposes, return the state of all counters
-spec state() -> list(term()).
state()->
-spec state() -> [term()].
state() ->
ets:tab2list(?TID).

%%%===================================================================
Expand Down Expand Up @@ -131,10 +126,13 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.


%%%% Internal Functions
create_ets() ->
ets:new(?TID, [set, named_table, public,
{keypos, #counter.name},
{read_concurrency, true},
{write_concurrency, true}]).
ets:new(?TID,
[set,
named_table,
public,
{keypos, #counter.name},
{read_concurrency, true},
{write_concurrency, true}]).

6 changes: 3 additions & 3 deletions src/spillway_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

%% API
-export([start_link/0]).

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

Expand All @@ -46,11 +45,12 @@ start_link() ->

init([]) ->
Procs = [child(spillway_srv, worker, [])],
{ok, { {one_for_all, 0, 1}, Procs}}.
{ok, {{one_for_all, 0, 1}, Procs}}.

%%====================================================================
%% Internal functions
%%====================================================================

child(I, Type, Args) ->
{I, {I, start_link, Args}, permanent, 5000, Type, [I]}.
{I, {I, start_link, Args}, permanent, 5000, Type, [I]}.

99 changes: 45 additions & 54 deletions test/spillway_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,22 @@
-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").


-export([all/0,
init_per_suite/1,
end_per_suite/1,
suite/0,
init_per_testcase/2,
end_per_testcase/2]).

-export([
complex/1,
simple/1
]).
init_per_suite/1,
end_per_suite/1,
suite/0,
init_per_testcase/2,
end_per_testcase/2]).
-export([complex/1, simple/1]).

-define(TABLE, test_counter).

%%%=============================================================================
%%% common_test callbacks
%%%=============================================================================

all() -> [
simple,
complex
].
all() ->
[simple, complex].

suite() ->
[{timetrap, {seconds, 15}}].
Expand Down Expand Up @@ -81,36 +74,34 @@ simple(_) ->
?assertEqual(0, spillway:leave(?TABLE)),
ok.

spawn_proc(ProcN, Limit, SignalGo, SignalStop, Parent) ->
spawn_monitor(fun () ->
monitor(process, SignalGo),
monitor(process, SignalStop),
receive
{'DOWN', _, process, SignalGo, _} ->
case spillway:enter(?TABLE, ProcN, Limit) of
{true, N} ->
send_parent(Parent, {entered, self(), N}),
receive
{'DOWN', _, process, SignalStop, _} ->
spillway:leave(?TABLE, ProcN)
end;
false ->
send_parent(Parent, {not_entered, self()}),
ok
end
end
end).

complex(_) ->
NProcs = 2000,
Limit = 140000,
Parent = self(),
SignalGo = signal(),
SignalStop = signal(),
Processes = [
begin
{Process, _Ref} =
spawn_monitor(fun() ->
monitor(process, SignalGo),
monitor(process, SignalStop),
receive
{'DOWN', _, process, SignalGo, _} ->
case spillway:enter(?TABLE, Proc, Limit) of
{true, N} ->
send_parent(Parent, {entered, self(), N}),
receive
{'DOWN', _, process, SignalStop, _} ->
spillway:leave(?TABLE, Proc)
end;
false ->
send_parent(Parent, {not_entered, self()}),
ok
end
end
end),
Process
end
|| Proc <- lists:seq(1, NProcs)],
Processes = [element(1, spawn_proc(ProcN, Limit, SignalGo, SignalStop, Parent))
|| ProcN <- lists:seq(1, NProcs)],
SignalGo ! go,

%% Collect enter msg
Expand All @@ -132,27 +123,27 @@ collect_values([], Max) ->
Max;
collect_values(ProcessesSignal, Cur) ->
receive
{not_entered, Pid} ->
collect_values(ProcessesSignal -- [Pid], Cur);
{entered, Pid, M} when M > Cur ->
collect_values(ProcessesSignal -- [Pid], M);
{entered, Pid, _} ->
collect_values(ProcessesSignal -- [Pid], Cur)
{not_entered, Pid} ->
collect_values(ProcessesSignal -- [Pid], Cur);
{entered, Pid, M} when M > Cur ->
collect_values(ProcessesSignal -- [Pid], M);
{entered, Pid, _} ->
collect_values(ProcessesSignal -- [Pid], Cur)
end.

wait_for_down([]) ->
ok;
wait_for_down(ProcessesExited) ->
receive
{'DOWN', _, process, Pid, _} ->
wait_for_down(ProcessesExited -- [Pid])
{'DOWN', _, process, Pid, _} ->
wait_for_down(ProcessesExited -- [Pid])
end.


signal() ->
spawn(
fun() ->
receive
go -> ok
end
end).
spawn(fun () ->
receive
go ->
ok
end
end).