Skip to content

Commit ed7083b

Browse files
committed
Merge branch 'elanfs-master'
2 parents 73b62a5 + 161de20 commit ed7083b

File tree

8 files changed

+66
-14
lines changed

8 files changed

+66
-14
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ application's environment:
109109
`pool_size`
110110
Specifies the size of the port program pool. Defaults to ``4``.
111111

112+
`nif_pool_size`
113+
Specifies the size of the nif program pool. Defaults to ``4``.
114+
115+
`nif_pool_max_overflow`
116+
Specifies the max workers to overflow of the nif program pool. Defaults to ``10``.
112117

113118
Run tests
114119
---------

rebar.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
{post_hooks,
1313
[{"(linux|darwin|solaris)", clean, "make -C c_src clean"},
1414
{"(freebsd)", clean, "gmake -C c_src clean"}]}.
15+
{deps, [
16+
{poolboy, "1.5.2"}
17+
]}.

rebar.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
[].
1+
{"1.1.0",
2+
[{<<"poolboy">>,{pkg,<<"poolboy">>,<<"1.5.2">>},0}]}.
3+
[
4+
{pkg_hash,[
5+
{<<"poolboy">>, <<"392B007A1693A64540CEAD79830443ABF5762F5D30CF50BC95CB2C1AAAFA006B">>}]}
6+
].

src/bcrypt.app.src

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
{application, bcrypt, [
33
{description, "An Erlang wrapper (NIF or port program) for the OpenBSD password scheme, bcrypt."},
44
{vsn, "git"},
5-
{registered, [bcrypt_sup, bcrypt_nif_worker, bcrypt_port_sup, bcrypt_pool]},
5+
{registered, [bcrypt_sup, bcrypt_port_sup, bcrypt_pool]},
66
{mod, {bcrypt_app, []}},
7-
{applications, [kernel, stdlib, crypto]},
7+
{applications, [kernel, stdlib, crypto, poolboy]},
88
{env, [
99
% Default number of 'rounds', defining the hashing complexity
1010
{default_log_rounds, 12},
@@ -13,7 +13,11 @@
1313
{mechanism, nif},
1414

1515
% Size of port program pool
16-
{pool_size, 4}
16+
{pool_size, 4},
17+
18+
{nif_pool_size, 4},
19+
{nif_pool_max_overflow, 10}
20+
1721
]},
1822
{maintainers, ["Hunter Morris", "Mrinal Wadhwa", "ErlangPack"]},
1923
{licenses, ["MIT"]},

src/bcrypt_nif_pool_sup.erl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
%% Copyright (c) 2011 Hunter Morris
2+
%% Distributed under the MIT license; see LICENSE for details.
3+
-module(bcrypt_nif_pool_sup).
4+
5+
-behaviour(supervisor).
6+
7+
-export([start_link/0, start_child/0, init/1]).
8+
9+
start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).
10+
start_child() -> supervisor:start_child(?MODULE, []).
11+
12+
init([]) ->
13+
{ok, PoolSize} = application:get_env(bcrypt, nif_pool_size),
14+
{ok, MaxOverFlow} = application:get_env(bcrypt, nif_pool_max_overflow),
15+
16+
PoolArgs = [
17+
{name, {local, nif_pool}},
18+
{nif_pool_size, PoolSize},
19+
{nif_pool_max_overflow, MaxOverFlow},
20+
{worker_module, bcrypt_nif_worker}
21+
],
22+
23+
PoolSpecs = [
24+
poolboy:child_spec(nif_pool, PoolArgs, [])
25+
],
26+
27+
{ok, {{one_for_one, 10, 10}, PoolSpecs}}.

src/bcrypt_nif_worker.erl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
-behaviour(gen_server).
77

8-
-export([start_link/0]).
8+
-export([start_link/1]).
99
-export([gen_salt/0, gen_salt/1]).
1010
-export([hashpw/2]).
1111

@@ -18,19 +18,26 @@
1818
context
1919
}).
2020

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

24-
gen_salt() ->
25-
gen_server:call(?MODULE, gen_salt, infinity).
23+
gen_salt() ->
24+
poolboy:transaction(nif_pool, fun(Worker) ->
25+
gen_server:call(Worker, gen_salt, infinity)
26+
end).
2627

2728
gen_salt(Rounds) ->
28-
gen_server:call(?MODULE, {gen_salt, Rounds}, infinity).
29+
poolboy:transaction(nif_pool, fun(Worker) ->
30+
gen_server:call(Worker, {gen_salt, Rounds}, infinity)
31+
end).
32+
2933

3034
hashpw(Password, Salt) ->
31-
gen_server:call(?MODULE, {hashpw, Password, Salt}, infinity).
35+
poolboy:transaction(nif_pool, fun(Worker) ->
36+
gen_server:call(Worker, {hashpw, Password, Salt}, infinity)
37+
end).
3238

3339
init([]) ->
40+
process_flag(trap_exit, true),
3441
{ok, Default} = application:get_env(bcrypt, default_log_rounds),
3542
Ctx = bcrypt_nif:create_ctx(),
3643
{ok, #state{default_log_rounds = Default, context = Ctx}}.

src/bcrypt_sup.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ init([]) ->
1616
{bcrypt_pool, {bcrypt_pool, start_link, []}, permanent,
1717
16#ffffffff, worker, [bcrypt_pool]}],
1818
NifChildren
19-
= [{bcrypt_nif_worker, {bcrypt_nif_worker, start_link, []}, permanent,
20-
16#ffffffff, worker, [bcrypt_nif_worker]}],
19+
= [{bcrypt_nif_pool_sup, {bcrypt_nif_pool_sup, start_link, []}, permanent,
20+
16#ffffffff, supervisor, [bcrypt_nif_pool_sup]}],
2121
case application:get_env(bcrypt, mechanism) of
2222
undefined -> {stop, no_mechanism_defined};
23-
{ok, nif} -> {ok, {{one_for_all, 1, 1}, NifChildren}};
23+
{ok, nif} -> {ok, {{one_for_all, 15, 60}, NifChildren}};
2424
{ok, port} -> {ok, {{one_for_all, 15, 60}, PortChildren}}
2525
end.

test/bcrypt_tests.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484

8585
start_with(Mechanism) when Mechanism =:= nif; Mechanism =:= port ->
8686
application:start(crypto),
87+
application:start(poolboy),
8788
case application:load(bcrypt) of
8889
{error, {already_loaded, bcrypt}} -> ok;
8990
ok -> ok

0 commit comments

Comments
 (0)