1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
%% @copyright 2011 Hunter Morris
%% @doc Implementation of `gen_server' behaviour.
%% @end
%% Distributed under the MIT license; see LICENSE for details.
-module(bcrypt_pool).
-author('Hunter Morris <huntermorris@gmail.com>').
-behaviour(gen_server).
-export([start_link/0, available/1]).
-export([gen_salt/0, gen_salt/1]).
-export([hashpw/2]).
%% gen_server
-export([init/1, code_change/3, terminate/2,
handle_call/3, handle_cast/2, handle_info/2]).
-record(state, {
size = 0,
busy = 0,
requests = queue:new(),
ports = queue:new()
}).
-record(req, {mon :: reference(), from :: {pid(), atom()}}).
-type state() :: #state{size :: 0, busy :: 0, requests :: queue:queue(), ports :: queue:queue()}.
%% @doc Creates a `gen_server' process as part of a supervision tree.
-spec start_link() -> Result when
Result :: {ok,Pid} | ignore | {error,Error},
Pid :: pid(),
Error :: {already_started,Pid} | term().
start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
%% @doc Asynchronosly check if `Pid' in `#state:requests' queue or not.
-spec available(Pid) -> Result when
Pid :: pid(),
Result :: ok.
available(Pid) -> gen_server:cast(?MODULE, {available, Pid}).
%% @doc Generate a random text salt.
-spec gen_salt() -> Result when
Result :: {ok, Salt},
Salt :: [byte()].
gen_salt() -> do_call(fun bcrypt_port:gen_salt/1, []).
%% @doc Generate a random text salt. Rounds defines the complexity of
%% the hashing, increasing the cost as 2^log_rounds.
-spec gen_salt(Rounds) -> Result when
Rounds :: bcrypt:rounds(),
Result :: {ok, Salt},
Salt :: [byte()].
gen_salt(Rounds) -> do_call(fun bcrypt_port:gen_salt/2, [Rounds]).
%% @doc Hash the specified password and the salt.
hashpw(Password, Salt) -> do_call(fun bcrypt_port:hashpw/3, [Password, Salt]).
%% @private
-spec init([]) -> Result when
Result :: {ok, state()}.
init([]) ->
{ok, Size} = application:get_env(bcrypt, pool_size),
{ok, #state{size = Size}}.
%% @private
terminate(shutdown, _) -> ok.
%% @private
-spec handle_call(Request, From, State) -> Result when
Request :: request,
From :: {RPid, atom()},
RPid :: pid(),
State :: state(),
Result :: {noreply, state()} | {reply, {ok, pid()}, state()}.
handle_call(request, {RPid, _} = From, #state{ports = P} = State) ->
case queue:out(P) of
{empty, P} ->
#state{size = Size, busy = B, requests = R} = State,
B1 =
if Size > B ->
{ok, _} = bcrypt_port_sup:start_child(),
B + 1;
true ->
B
end,
RRef = erlang:monitor(process, RPid),
R1 = queue:in(#req{mon = RRef, from = From}, R),
{noreply, State#state{requests = R1,
busy = B1}};
{{value, PPid}, P1} ->
#state{busy = B} = State,
{reply, {ok, PPid}, State#state{busy = B + 1, ports = P1}}
end;
handle_call(Msg, _, _) -> exit({unknown_call, Msg}).
%% @private
-spec handle_cast({available, Pid}, state()) -> Result when
Pid :: pid(),
Result :: {noreply, state()}.
handle_cast(
{available, Pid},
#state{requests = R, ports = P, busy = B} = S) ->
case queue:out(R) of
{empty, R} ->
{noreply, S#state{ports = queue:in(Pid, P), busy = B - 1}};
{{value, #req{mon = Mon, from = F}}, R1} ->
true = erlang:demonitor(Mon, [flush]),
gen_server:reply(F, {ok, Pid}),
{noreply, S#state{requests = R1}}
end;
handle_cast(Msg, _) -> exit({unknown_cast, Msg}).
%% @private
handle_info({'DOWN', Ref, process, _Pid, _Reason}, #state{requests = R} = State) ->
R1 = queue:from_list(lists:keydelete(Ref, #req.mon, queue:to_list(R))),
{noreply, State#state{requests = R1}};
%% @private
handle_info(Msg, _) -> exit({unknown_info, Msg}).
%% @private
code_change(_OldVsn, State, _Extra) -> {ok, State}.
do_call(F, Args0) ->
{ok, Pid} = gen_server:call(?MODULE, request, infinity),
Args = [Pid|Args0],
apply(F, Args).
|