Skip to content
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
12 changes: 6 additions & 6 deletions .github/workflows/erlang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ jobs:
name: OTP:${{matrix.otp}}
strategy:
matrix:
otp: ['28', '27']
rebar3: ['3.26']
otp: ['28', '29']
rebar3: ['3.27']
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v7
- uses: erlef/setup-beam@v1
id: setup-beam
with:
otp-version: ${{matrix.otp}}
rebar3-version: ${{matrix.rebar3}}
- name: Restore _build
uses: actions/cache@v5
uses: actions/cache@v6
with:
path: _build
key: _build-cache-for-os-${{runner.os}}-otp-${{steps.setup-beam.outputs.otp-version}}-rebar3-${{steps.setup-beam.outputs.rebar3-version}}-hash-${{hashFiles('rebar.lock')}}
- name: Restore rebar3's cache
uses: actions/cache@v5
uses: actions/cache@v6
with:
path: ~/.cache/rebar3
key: rebar3-cache-for-os-${{runner.os}}-otp-${{steps.setup-beam.outputs.otp-version}}-rebar3-${{steps.setup-beam.outputs.rebar3-version}}-hash-${{hashFiles('rebar.lock')}}
Expand All @@ -41,7 +41,7 @@ jobs:
- name: Run tests and verifications
run: rebar3 test
- name: Upload code coverage
uses: codecov/codecov-action@v5
uses: codecov/codecov-action@v7
with:
files: ./_build/test/covertool/worker_pool.covertool.xml
token: ${{ secrets.CODECOV_TOKEN }}
Expand Down
17 changes: 11 additions & 6 deletions elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@
{config, [
#{
files => ["src/**/*.erl"],
ruleset => erl_files,
ruleset => erl_files_strict,
rules =>
[
{elvis_style, no_macros, disable},
{elvis_style, no_invalid_dynamic_calls, #{
ignore => [wpool_process, wpool_time_checker]
}},
{elvis_style, no_god_modules, #{limit => 30}},
{elvis_style, state_record_and_type, disable},
{elvis_style, dont_repeat_yourself, #{
ignore => [wpool_SUITE], min_complexity => 13
}}
{elvis_style, dont_repeat_yourself, #{min_complexity => 13}},
{elvis_style, max_function_length, disable},
{elvis_style, max_function_clause_length, #{max_length => 75}}
]
},
#{
files => ["test/*.erl"],
ruleset => erl_files,
ruleset => erl_files_strict,
rules =>
[
{elvis_style, no_debug_call, disable},
{elvis_style, state_record_and_type, disable},
{elvis_style, dont_repeat_yourself, #{min_complexity => 13}}
{elvis_style, dont_repeat_yourself, #{min_complexity => 13}},
{elvis_style, max_module_length, disable},
{elvis_style, max_function_length, disable},
{elvis_style, max_function_clause_length, disable},
{elvis_style, abc_size, disable}
]
}
]}
Expand Down
14 changes: 7 additions & 7 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
{cover_export_enabled, true},
{cover_opts, [verbose, {min_coverage, 92}]},
{ct_opts, [{verbose, true}]},
{deps, [{katana, "1.0.0"}, {mixer, "1.2.0", {pkg, inaka_mixer}}, {meck, "~> 1.1"}]},
{deps, [{mixer, "2.0.0", {pkg, inaka_mixer}}, {meck, "~> 1.2"}]},
{dialyzer, [
{warnings, [no_return, unmatched_returns, error_handling, underspecs, unknown]},
{plt_extra_apps, [common_test, katana, meck]}
{plt_extra_apps, [common_test, meck]}
]}
]}
]}.
Expand All @@ -40,11 +40,11 @@
%% == Dependencies and plugins ==

{project_plugins, [
{rebar3_hank, "~> 1.6"},
{rebar3_depup, "~> 0.4.0"},
{rebar3_hex, "~> 7.1"},
{rebar3_ex_doc, "~> 0.2"},
{rebar3_lint, "~> 5.0"},
{rebar3_hank, "~> 2.0.0"},
{rebar3_depup, "~> 1.0.0"},
{rebar3_hex, "~> 7.2"},
{rebar3_ex_doc, "~> 0.3"},
{rebar3_lint, "~> 6.0"},
{erlfmt, "~> 1.8"},
{covertool, "~> 2.0"}
]}.
Expand Down
4 changes: 2 additions & 2 deletions src/wpool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,12 @@ stop() ->
%% BEHAVIOUR CALLBACKS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @private
-spec start(any(), any()) -> supervisor:startlink_ret().
-spec start(term(), term()) -> supervisor:startlink_ret().
start(_StartType, _StartArgs) ->
wpool_sup:start_link().

%% @private
-spec stop(any()) -> ok.
-spec stop(term()) -> ok.
stop(_State) ->
ok.

Expand Down
37 changes: 29 additions & 8 deletions src/wpool_pool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@
%%% `t:wpool:pool_sup_intensity()' options respectively.
-module(wpool_pool).

%% We use <- to filter and we use list_to_atom/1 to build atoms from other atoms.
-elvis(
[
{elvis_style, prefer_strict_generators, disable},
{elvis_style, no_common_caveats_call, #{
caveat_functions => [
{timer, send_after, 2},
{timer, send_after, 3},
{timer, send_interval, 2},
{timer, send_interval, 3},
{erlang, size, 1},
{gen_statem, call, 2},
{gen_server, call, 2},
{gen_event, call, 3},
{erlang, binary_to_atom, 1},
{erlang, binary_to_atom, 2}
]
}}
]
).

-include_lib("kernel/include/logger.hrl").

-behaviour(supervisor).
Expand Down Expand Up @@ -145,7 +166,7 @@ run_with_available_worker(Name, Run, Timeout) ->
%% The timeout provided includes the time it takes to get a worker
%% and for it to process the call.
%% @throws no_workers | timeout
-spec call_available_worker(wpool:name(), any(), timeout()) -> any().
-spec call_available_worker(wpool:name(), term(), timeout()) -> term().
call_available_worker(Name, Call, Timeout) ->
case find_wpool(Name) of
undefined ->
Expand All @@ -163,7 +184,7 @@ call_available_worker(Name, Call, Timeout) ->

%% @doc Picks the first available worker and sends the request to it.
%% The timeout provided considers only the time it takes to get a worker
-spec send_request_available_worker(wpool:name(), any(), timeout()) ->
-spec send_request_available_worker(wpool:name(), term(), timeout()) ->
noproc | timeout | gen_server:request_id().
send_request_available_worker(Name, Call, Timeout) ->
wpool_queue_manager:send_request_available_worker(
Expand Down Expand Up @@ -233,7 +254,7 @@ all() ->
Name
|| {{?MODULE, Name}, _} <- persistent_term:get(),
is_atom(Name),
find_wpool(Name) /= undefined
find_wpool(Name) =/= undefined
].

%% @doc Retrieves the list of worker registered names.
Expand All @@ -245,7 +266,7 @@ get_workers(Name) ->
%% @doc Retrieves the pool stats for all pools
-spec stats() -> [wpool:stats()].
stats() ->
[stats(Name) || Name <- all()].
lists:map(fun stats/1, all()).

%% @doc Retrieves a snapshot of the pool stats
%% @throws no_workers
Expand Down Expand Up @@ -350,10 +371,10 @@ remove_callback_module(Pool, Module) ->
%% @doc Get values from the worker pool record. Useful when using a custom
%% strategy function.
-spec wpool_get
(atom(), wpool()) -> any();
([atom()], wpool()) -> any().
(atom(), wpool()) -> term();
([atom()], wpool()) -> term().
wpool_get(List, Wpool) when is_list(List) ->
[g(Atom, Wpool) || Atom <- List];
[g(Atom, Wpool) || Atom <:- List];
wpool_get(Atom, Wpool) when is_atom(Atom) ->
g(Atom, Wpool).

Expand Down Expand Up @@ -556,7 +577,7 @@ all_workers(Name) ->
store_wpool(Name, Size, Options) ->
Atomic = atomics:new(1, [{signed, false}]),
atomics:put(Atomic, 1, 1),
WorkerNames = list_to_tuple([worker_name(Name, I) || I <- lists:seq(1, Size)]),
WorkerNames = list_to_tuple([worker_name(Name, I) || I <:- lists:seq(1, Size)]),
Wpool =
#wpool{
name = Name,
Expand Down
6 changes: 3 additions & 3 deletions src/wpool_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ terminate(Reason, State) ->
end.

%% @private
-spec code_change(string() | {down, string()}, state(), any()) ->
-spec code_change(string() | {down, string()}, state(), term()) ->
{ok, state()} | {error, term()}.
code_change(OldVsn, #state{mod = #callback_cache{module = Mod}} = State, Extra) ->
case erlang:function_exported(Mod, code_change, 3) of
Expand All @@ -207,7 +207,7 @@ code_change(OldVsn, #state{mod = #callback_cache{module = Mod}} = State, Extra)
end.

%% @private
-spec handle_info(any(), state()) ->
-spec handle_info(term(), state()) ->
{noreply, state()} | {noreply, state(), next_step()} | {stop, term(), state()}.
handle_info(Info, #state{mod = CbCache} = State) ->
#callback_cache{module = Mod, handle_info = HandleInfo} = CbCache,
Expand Down Expand Up @@ -235,7 +235,7 @@ handle_info(Info, #state{mod = CbCache} = State) ->
end.

%% @private
-spec handle_continue(any(), state()) ->
-spec handle_continue(term(), state()) ->
{noreply, state()}
| {noreply, state(), next_step()}
| {stop, term(), state()}.
Expand Down
14 changes: 7 additions & 7 deletions src/wpool_process_callbacks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

-export_type([event/0]).

-callback handle_init_start(wpool:name()) -> any().
-callback handle_worker_creation(wpool:name()) -> any().
-callback handle_worker_death(wpool:name(), term()) -> any().
-callback handle_init_start(wpool:name()) -> term().
-callback handle_worker_creation(wpool:name()) -> term().
-callback handle_worker_death(wpool:name(), term()) -> term().

-optional_callbacks([
handle_init_start/1,
Expand All @@ -34,7 +34,7 @@ init(Module) ->
{ok, Module}.

%% @private
-spec handle_event({event(), [any()]}, state()) -> {ok, state()}.
-spec handle_event({event(), [term()]}, state()) -> {ok, state()}.
handle_event({Event, Args}, Module) ->
call(Module, Event, Args),
{ok, Module}.
Expand All @@ -45,14 +45,14 @@ handle_call(Msg, State) ->
{ok, {error, {unexpected_call, Msg}}, State}.

%% @doc Sends a notification to all registered callback modules.
-spec notify(event(), undefined | atom(), [any()]) -> ok.
-spec notify(event(), undefined | atom(), [term()]) -> ok.
notify(_, undefined, _) ->
ok;
notify(Event, EventMgr, Args) ->
gen_event:notify(EventMgr, {Event, Args}).

%% @doc Adds a callback module.
-spec add_callback_module(wpool:name(), module()) -> ok | {error, any()}.
-spec add_callback_module(wpool:name(), module()) -> ok | {error, term()}.
add_callback_module(EventManager, Module) ->
case ensure_loaded(Module) of
ok ->
Expand All @@ -62,7 +62,7 @@ add_callback_module(EventManager, Module) ->
end.

%% @doc Removes a callback module.
-spec remove_callback_module(wpool:name(), module()) -> ok | {error, any()}.
-spec remove_callback_module(wpool:name(), module()) -> ok | {error, term()}.
remove_callback_module(EventManager, Module) ->
gen_event:delete_handler(EventManager, {wpool_process_callbacks, Module}, Module).

Expand Down
2 changes: 1 addition & 1 deletion src/wpool_process_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ init({Name, Options}) ->
type => worker,
modules => [Worker]
}
|| I <- lists:seq(1, Workers)
|| I <:- lists:seq(1, Workers)
],
{ok, {Strategy, WorkerSpecs}}.

Expand Down
10 changes: 5 additions & 5 deletions src/wpool_queue_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ run_with_available_worker(QueueManager, Call, Timeout) ->
end.

%% @doc returns the first available worker in the pool
-spec call_available_worker(queue_mgr(), any(), timeout()) -> noproc | timeout | any().
-spec call_available_worker(queue_mgr(), term(), timeout()) -> noproc | timeout | term().
call_available_worker(QueueManager, Call, Timeout) ->
case get_available_worker(QueueManager, Call, Timeout) of
{ok, Worker, TimeLeft} when TimeLeft > 0 ->
Expand All @@ -112,7 +112,7 @@ cast_to_available_worker(QueueManager, Cast) ->
gen_server:cast(QueueManager, {cast_to_available_worker, Cast}).

%% @doc returns the first available worker in the pool
-spec send_request_available_worker(queue_mgr(), any(), timeout()) ->
-spec send_request_available_worker(queue_mgr(), term(), timeout()) ->
noproc | timeout | gen_server:request_id().
send_request_available_worker(QueueManager, Call, Timeout) ->
case get_available_worker(QueueManager, Call, Timeout) of
Expand Down Expand Up @@ -147,7 +147,7 @@ worker_dead(QueueManager, Worker) ->
-spec pending_task_count(queue_mgr()) -> non_neg_integer().
pending_task_count(QueueManager) ->
try
gen_server:call(QueueManager, pending_task_count)
gen_server:call(QueueManager, pending_task_count, 5000)
catch
_:{noproc, _} ->
0
Expand Down Expand Up @@ -246,7 +246,7 @@ handle_call({available_worker, ExpiresAt}, {ClientPid, _Ref} = Client, State) ->
handle_call(pending_task_count, _From, State) ->
{reply, get(pending_tasks), State}.

-spec handle_info(any(), state()) -> {noreply, state()}.
-spec handle_info(term(), state()) -> {noreply, state()}.
handle_info({'DOWN', Ref, Type, {Worker, _Node}, Exit}, State) ->
handle_info({'DOWN', Ref, Type, Worker, Exit}, State);
handle_info({'DOWN', _, _, Worker, Exit}, #state{monitors = Mons} = State) ->
Expand All @@ -263,7 +263,7 @@ handle_info(_Info, State) ->
%%%===================================================================
%%% private
%%%===================================================================
-spec get_available_worker(queue_mgr(), any(), timeout()) ->
-spec get_available_worker(queue_mgr(), term(), timeout()) ->
noproc | timeout | {ok, atom(), timeout()}.
get_available_worker(QueueManager, Call, Timeout) ->
ExpiresAt = expires(Timeout),
Expand Down
6 changes: 3 additions & 3 deletions src/wpool_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
%% @doc Starts the supervisor
-spec start_link() -> supervisor:startlink_ret().
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
supervisor:start_link({local, ?MODULE}, ?MODULE, #{}).
Comment thread
elbrujohalcon marked this conversation as resolved.

%% @doc Starts a new pool
-spec start_pool(wpool:name(), wpool:options()) -> supervisor:startchild_ret().
Expand Down Expand Up @@ -55,8 +55,8 @@ stop_pool(Name) ->
%%----------------------------------------------------------------------
%% Supervisor behaviour callbacks
%%----------------------------------------------------------------------
-spec init([]) -> {ok, {{simple_one_for_one, 5, 60}, [supervisor:child_spec()]}}.
init([]) ->
-spec init(#{}) -> {ok, {{simple_one_for_one, 5, 60}, [supervisor:child_spec()]}}.
init(#{}) ->
{ok,
{{simple_one_for_one, 5, 60}, [
{wpool_pool, {wpool_pool, start_link, []}, permanent, 2000, supervisor, dynamic}
Expand Down
8 changes: 4 additions & 4 deletions src/wpool_time_checker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ start_link(WPool, Name, Handler) when is_tuple(Handler) ->
%% @private
-spec add_handler(atom(), handler()) -> ok.
add_handler(Name, Handler) ->
gen_server:call(Name, {add_handler, Handler}).
gen_server:call(Name, {add_handler, Handler}, 5000).

%%%===================================================================
%%% simple callbacks
Expand All @@ -71,7 +71,7 @@ handle_call({add_handler, Handler}, _, #state{handlers = Handlers} = State) ->
%%% real (i.e. interesting) callbacks
%%%===================================================================
%% @private
-spec handle_info(any(), state()) -> {noreply, state()}.
-spec handle_info(term(), state()) -> {noreply, state()}.
handle_info({check, Pid, TaskId, Runtime, WarningsLeft}, State) ->
case erlang:process_info(Pid, dictionary) of
{dictionary, Values} ->
Expand Down Expand Up @@ -124,7 +124,7 @@ decrease_warnings(N) ->
N - 1.

-spec send_reports(
[{atom(), atom()}],
[{module(), atom()}],
atom(),
atom(),
pid(),
Expand All @@ -134,7 +134,7 @@ decrease_warnings(N) ->
ok.
send_reports(Handlers, Alert, Pool, Pid, Task, Runtime) ->
Args = [{alert, Alert}, {pool, Pool}, {worker, Pid}, {task, Task}, {runtime, Runtime}],
_ = [safe_apply(Mod, Fun, Args) || {Mod, Fun} <- Handlers],
_ = [safe_apply(Mod, Fun, Args) || {Mod, Fun} <:- Handlers],
ok.

safe_apply(Mod, Fun, Args) ->
Expand Down
Loading