-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from g-andrade/feature/synchronous-worker-init…
…ialization Do not allow workers to be used before they're initialized
- Loading branch information
Showing
7 changed files
with
155 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,7 +191,7 @@ code_change(_OldVsn, State, _Extra) -> | |
- `worker_module`: the module that represents the workers | ||
- `size`: maximum pool size | ||
|
||
## Authors | ||
## Original Author | ||
|
||
- Luis Rascão (lrascao) <[email protected]> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
%% @private | ||
%% Poolgirl - A sexy Erlang worker pool factory | ||
|
||
-module(poolgirl_internal_directory). | ||
|
||
-include_lib("stdlib/include/ms_transform.hrl"). | ||
|
||
-export([new/0, | ||
register/2, | ||
find/2]). | ||
|
||
%%% | ||
%%% Types | ||
%%% | ||
|
||
-opaque t() :: ets:tab(). | ||
-export_type([t/0]). | ||
|
||
%%% | ||
%%% Exported functions | ||
%%% | ||
|
||
-spec new() -> t(). | ||
new() -> | ||
ets:new('poolgirl.internal_directory', [public]). | ||
|
||
-spec register(t(), atom()) -> boolean(). | ||
register(Directory, Name) -> | ||
case find(Directory, Name) of | ||
{ok, ExistingPid} -> | ||
maybe_over_register(Directory, Name, ExistingPid); | ||
error -> | ||
ets:insert_new(Directory, {Name, self()}) | ||
end. | ||
|
||
-spec find(t(), atom()) -> {ok, pid()} | error. | ||
find(Directory, Name) -> | ||
case ets:lookup(Directory, Name) of | ||
[{Name, Pid}] -> | ||
{ok, Pid}; | ||
[] -> | ||
error | ||
end. | ||
|
||
%%% | ||
%%% Local functions | ||
%%% | ||
|
||
maybe_over_register(Directory, Name, ExistingPid) -> | ||
(ExistingPid =:= self() orelse not is_process_alive(ExistingPid)) | ||
andalso over_register(Directory, Name, ExistingPid). | ||
|
||
over_register(Directory, Name, ExistingPid) -> | ||
MatchSpec = over_registration_match_spec(Name, ExistingPid), | ||
case _AmountReplaced = ets:select_replace(Directory, MatchSpec) of | ||
1 -> true; | ||
0 -> false | ||
end. | ||
|
||
over_registration_match_spec(Name, ExistingPid) -> | ||
% Atomic replacement of the entry we've just looked up | ||
ets:fun2ms( | ||
fun ({EntryName, EntryPid}) when EntryName =:= Name, | ||
EntryPid =:= ExistingPid -> | ||
{EntryName, self()} | ||
end). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters