Skip to content

Commit

Permalink
Merge branch 'elanfs-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed May 20, 2020
2 parents 73b62a5 + 161de20 commit ed7083b
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ application's environment:
`pool_size`
Specifies the size of the port program pool. Defaults to ``4``.

`nif_pool_size`
Specifies the size of the nif program pool. Defaults to ``4``.

`nif_pool_max_overflow`
Specifies the max workers to overflow of the nif program pool. Defaults to ``10``.

Run tests
---------
Expand Down
3 changes: 3 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
{post_hooks,
[{"(linux|darwin|solaris)", clean, "make -C c_src clean"},
{"(freebsd)", clean, "gmake -C c_src clean"}]}.
{deps, [
{poolboy, "1.5.2"}
]}.
7 changes: 6 additions & 1 deletion rebar.lock
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
[].
{"1.1.0",
[{<<"poolboy">>,{pkg,<<"poolboy">>,<<"1.5.2">>},0}]}.
[
{pkg_hash,[
{<<"poolboy">>, <<"392B007A1693A64540CEAD79830443ABF5762F5D30CF50BC95CB2C1AAAFA006B">>}]}
].
10 changes: 7 additions & 3 deletions src/bcrypt.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{application, bcrypt, [
{description, "An Erlang wrapper (NIF or port program) for the OpenBSD password scheme, bcrypt."},
{vsn, "git"},
{registered, [bcrypt_sup, bcrypt_nif_worker, bcrypt_port_sup, bcrypt_pool]},
{registered, [bcrypt_sup, bcrypt_port_sup, bcrypt_pool]},
{mod, {bcrypt_app, []}},
{applications, [kernel, stdlib, crypto]},
{applications, [kernel, stdlib, crypto, poolboy]},
{env, [
% Default number of 'rounds', defining the hashing complexity
{default_log_rounds, 12},
Expand All @@ -13,7 +13,11 @@
{mechanism, nif},

% Size of port program pool
{pool_size, 4}
{pool_size, 4},

{nif_pool_size, 4},
{nif_pool_max_overflow, 10}

]},
{maintainers, ["Hunter Morris", "Mrinal Wadhwa", "ErlangPack"]},
{licenses, ["MIT"]},
Expand Down
27 changes: 27 additions & 0 deletions src/bcrypt_nif_pool_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
%% Copyright (c) 2011 Hunter Morris
%% Distributed under the MIT license; see LICENSE for details.
-module(bcrypt_nif_pool_sup).

-behaviour(supervisor).

-export([start_link/0, start_child/0, init/1]).

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

init([]) ->
{ok, PoolSize} = application:get_env(bcrypt, nif_pool_size),
{ok, MaxOverFlow} = application:get_env(bcrypt, nif_pool_max_overflow),

PoolArgs = [
{name, {local, nif_pool}},
{nif_pool_size, PoolSize},
{nif_pool_max_overflow, MaxOverFlow},
{worker_module, bcrypt_nif_worker}
],

PoolSpecs = [
poolboy:child_spec(nif_pool, PoolArgs, [])
],

{ok, {{one_for_one, 10, 10}, PoolSpecs}}.
21 changes: 14 additions & 7 deletions src/bcrypt_nif_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

-behaviour(gen_server).

-export([start_link/0]).
-export([start_link/1]).
-export([gen_salt/0, gen_salt/1]).
-export([hashpw/2]).

Expand All @@ -18,19 +18,26 @@
context
}).

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

gen_salt() ->
gen_server:call(?MODULE, gen_salt, infinity).
gen_salt() ->
poolboy:transaction(nif_pool, fun(Worker) ->
gen_server:call(Worker, gen_salt, infinity)
end).

gen_salt(Rounds) ->
gen_server:call(?MODULE, {gen_salt, Rounds}, infinity).
poolboy:transaction(nif_pool, fun(Worker) ->
gen_server:call(Worker, {gen_salt, Rounds}, infinity)
end).


hashpw(Password, Salt) ->
gen_server:call(?MODULE, {hashpw, Password, Salt}, infinity).
poolboy:transaction(nif_pool, fun(Worker) ->
gen_server:call(Worker, {hashpw, Password, Salt}, infinity)
end).

init([]) ->
process_flag(trap_exit, true),
{ok, Default} = application:get_env(bcrypt, default_log_rounds),
Ctx = bcrypt_nif:create_ctx(),
{ok, #state{default_log_rounds = Default, context = Ctx}}.
Expand Down
6 changes: 3 additions & 3 deletions src/bcrypt_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ init([]) ->
{bcrypt_pool, {bcrypt_pool, start_link, []}, permanent,
16#ffffffff, worker, [bcrypt_pool]}],
NifChildren
= [{bcrypt_nif_worker, {bcrypt_nif_worker, start_link, []}, permanent,
16#ffffffff, worker, [bcrypt_nif_worker]}],
= [{bcrypt_nif_pool_sup, {bcrypt_nif_pool_sup, start_link, []}, permanent,
16#ffffffff, supervisor, [bcrypt_nif_pool_sup]}],
case application:get_env(bcrypt, mechanism) of
undefined -> {stop, no_mechanism_defined};
{ok, nif} -> {ok, {{one_for_all, 1, 1}, NifChildren}};
{ok, nif} -> {ok, {{one_for_all, 15, 60}, NifChildren}};
{ok, port} -> {ok, {{one_for_all, 15, 60}, PortChildren}}
end.
1 change: 1 addition & 0 deletions test/bcrypt_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@

start_with(Mechanism) when Mechanism =:= nif; Mechanism =:= port ->
application:start(crypto),
application:start(poolboy),
case application:load(bcrypt) of
{error, {already_loaded, bcrypt}} -> ok;
ok -> ok
Expand Down

0 comments on commit ed7083b

Please sign in to comment.