Skip to content

Commit 614a26d

Browse files
committed
initial release
0 parents  commit 614a26d

16 files changed

+827
-0
lines changed

.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
ebin
2+
*.swp
3+
*.dump
4+
edoc-info
5+
.DS_Store
6+
deps/
7+
doc/*.html
8+
doc/*.css
9+
doc/erlang.png
10+
doc/edoc-info
11+
t/*.beam
12+
examples/*.beam
13+
.eunit/*
14+
log
15+
16+
examples/proxy_openbsd/log
17+
examples/proxy_openbsd/deps
18+
examples/proxy_openbsd/ebim

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2013 (c) Benoît Chesneau <benoitc@e-engura.org>
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

NOTICE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
barrel
2+
------
3+
4+
2013 (c) Benoît Chesneau <benoitc@e-engura.org>
5+
6+
barrel is released under the MIT license. See the LICENSE
7+
file for the complete license.
8+
9+
10+
Third parties
11+
-------------
12+
13+
*) barrel_tcp, barrel_ssl under ISC license:
14+
Copyright (c) 2011-2012, Loïc Hoguin <essen@ninenines.eu>

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# barrel
2+
3+
barrel is a generic TCP acceptor pool with low latency in Erlang.

rebar

138 KB
Binary file not shown.

rebar.config

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
2+
%% ex: ft=erlang ts=4 sw=4 et
3+
4+
{erl_opts, [warnings_as_errors, {parse_transform, lager_transform}]}.
5+
{xref_checks, [undefined_function_calls]}.
6+
7+
{cover_enabled, true}.
8+
{cover_print_enabled, true}.
9+
{eunit_opts, [verbose]}.
10+
11+
{require_otp_vsn, "R15|R16"}.
12+
{clean_files, ["*~","*/*~","*/*.xfm","test/*.beam"]}.
13+
14+
{deps, [
15+
{lager, ".*", {git, "git://github.com/basho/lager.git",
16+
{branch, "master"}}}
17+
]}.
18+

src/barrel.app.src

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
2+
%% ex: ft=erlang ts=4 sw=4 et
3+
4+
{application, barrel,
5+
[
6+
{description, ""},
7+
{vsn, "1.0"},
8+
{registered, []},
9+
{applications, [
10+
kernel,
11+
stdlib
12+
]},
13+
{mod, { barrel_app, []}},
14+
{env, []}
15+
]}.

src/barrel.erl

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
%%% -*- erlang -*-
2+
%%%
3+
%%% This file is part of barrel released under the MIT license.
4+
%%% See the NOTICE for more information.
5+
6+
-module(barrel).
7+
8+
-export([start/0, stop/0]).
9+
-export([start_listener/6, start_listener/7,
10+
stop_listener/1]).
11+
12+
13+
% --- Application ---
14+
15+
%% @doc Start the barrel application. Useful when testing using the shell.
16+
start() ->
17+
barrel_deps:ensure(),
18+
application:load(barrel),
19+
barrel_app:ensure_deps_started(),
20+
application:start(barrel).
21+
22+
%% @doc Start the coffer application. Useful when testing using the shell.
23+
stop() ->
24+
application:stop(barrel).
25+
26+
27+
% --- barrel API ---
28+
29+
30+
start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol,
31+
ProtocolOpts) ->
32+
start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol,
33+
ProtocolOpts, []).
34+
35+
start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol,
36+
ProtoOpts, ListenerOpts) ->
37+
barrel_listener:start_listener(Ref, NbAcceptors, Transport, TransOpts,
38+
Protocol, ProtoOpts, ListenerOpts).
39+
40+
stop_listener(Ref) ->
41+
barrel_listener:stop_listener(Ref).

src/barrel_acceptor.erl

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
%%% -*- erlang -*-
2+
%%%
3+
%%% This file is part of barrel released under the MIT license.
4+
%%% See the NOTICE for more information.
5+
6+
-module(barrel_acceptor).
7+
8+
-export([start_link/5]).
9+
10+
-export([accept/5]).
11+
12+
13+
start_link(Listener, Transport, ListenSocket, Opts, Protocol) ->
14+
spawn_link(?MODULE, accept, [Listener, Transport, ListenSocket, Opts,
15+
Protocol]).
16+
17+
%% accept on the socket until a client connect
18+
accept(Listener, Transport, ListenSocket, Opts,
19+
{ProtocolHandler, ProtoOpts}=Protocol) ->
20+
21+
AcceptTimeout = proplists:get_value(accept_timeout, Opts, 10000),
22+
case catch Transport:accept(ListenSocket, AcceptTimeout) of
23+
{ok, Socket} ->
24+
gen_server:cast(Listener, accepted),
25+
ProtocolHandler:init(Transport, Socket, ProtoOpts);
26+
{error, timeout} ->
27+
?MODULE:accept(Listener, Transport, ListenSocket, Opts,
28+
Protocol);
29+
{error, econnaborted} ->
30+
?MODULE:accept(Listener, Transport, ListenSocket, Opts,
31+
Protocol);
32+
{error, esslaccept} ->
33+
exit(normal);
34+
{error, Other} ->
35+
exit({error, Other})
36+
end.

src/barrel_app.erl

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
%%% -*- erlang -*-
2+
%%%
3+
%%% This file is part of barrel released under the MIT license.
4+
%%% See the NOTICE for more information.
5+
6+
-module(barrel_app).
7+
8+
-behaviour(application).
9+
10+
%% Application callbacks
11+
-export([start/2, stop/1,
12+
ensure_deps_started/0]).
13+
14+
%% ===================================================================
15+
%% Application callbacks
16+
%% ===================================================================
17+
18+
start(_StartType, _StartArgs) ->
19+
barrel_deps:ensure(),
20+
ensure_deps_started(),
21+
barrel_sup:start_link().
22+
23+
stop(_State) ->
24+
ok.
25+
26+
ensure_deps_started() ->
27+
{ok, Deps} = application:get_key(barrel, applications),
28+
true = lists:all(fun ensure_started/1, Deps).
29+
30+
ensure_started(App) ->
31+
case application:start(App) of
32+
ok ->
33+
true;
34+
{error, {already_started, App}} ->
35+
true;
36+
Else ->
37+
error_logger:error_msg("Couldn't start ~p: ~p", [App, Else]),
38+
Else
39+
end.
40+

src/barrel_deps.erl

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
%%% -*- erlang -*-
2+
%%%
3+
%%% This file is part of barrel released under the MIT license.
4+
%%% See the NOTICE for more information.
5+
6+
-module(barrel_deps).
7+
-author('Justin Sheehy <justin@basho.com>').
8+
-author('Andy Gross <andy@basho.com>').
9+
10+
-export([ensure/0, ensure/1]).
11+
-export([get_base_dir/0, get_base_dir/1]).
12+
-export([local_path/1, local_path/2]).
13+
-export([deps_on_path/0, new_siblings/1]).
14+
15+
%% @spec deps_on_path() -> [ProjNameAndVers]
16+
%% @doc List of project dependencies on the path.
17+
deps_on_path() ->
18+
ordsets:from_list([filename:basename(filename:dirname(X)) || X <- code:get_path()]).
19+
20+
%% @spec new_siblings(Module) -> [Dir]
21+
%% @doc Find new siblings paths relative to Module that aren't already on the
22+
%% code path.
23+
new_siblings(Module) ->
24+
Existing = deps_on_path(),
25+
SiblingEbin = [ X || X <- filelib:wildcard(local_path(["deps", "*", "ebin"], Module)),
26+
filename:basename(filename:dirname(X)) /= %% don't include self
27+
filename:basename(filename:dirname(
28+
filename:dirname(
29+
filename:dirname(X)))) ],
30+
Siblings = [filename:dirname(X) || X <- SiblingEbin,
31+
ordsets:is_element(
32+
filename:basename(filename:dirname(X)),
33+
Existing) =:= false],
34+
lists:filter(fun filelib:is_dir/1,
35+
lists:append([[filename:join([X, "ebin"]),
36+
filename:join([X, "include"])] ||
37+
X <- Siblings])).
38+
39+
40+
%% @spec ensure(Module) -> ok
41+
%% @doc Ensure that all ebin and include paths for dependencies
42+
%% of the application for Module are on the code path.
43+
ensure(Module) ->
44+
code:add_paths(new_siblings(Module)),
45+
%% code:clash is annoying when you load couchbeam in a script.
46+
%% code:clash(),
47+
ok.
48+
49+
%% @spec ensure() -> ok
50+
%% @doc Ensure that the ebin and include paths for dependencies of
51+
%% this application are on the code path. Equivalent to
52+
%% ensure(?Module).
53+
ensure() ->
54+
ensure(?MODULE).
55+
56+
%% @spec get_base_dir(Module) -> string()
57+
%% @doc Return the application directory for Module. It assumes Module is in
58+
%% a standard OTP layout application in the ebin or src directory.
59+
get_base_dir(Module) ->
60+
{file, Here} = code:is_loaded(Module),
61+
filename:dirname(filename:dirname(Here)).
62+
63+
%% @spec get_base_dir() -> string()
64+
%% @doc Return the application directory for this application. Equivalent to
65+
%% get_base_dir(?MODULE).
66+
get_base_dir() ->
67+
get_base_dir(?MODULE).
68+
69+
%% @spec local_path([string()], Module) -> string()
70+
%% @doc Return an application-relative directory from Module's application.
71+
local_path(Components, Module) ->
72+
filename:join([get_base_dir(Module) | Components]).
73+
74+
%% @spec local_path(Components) -> string()
75+
%% @doc Return an application-relative directory for this application.
76+
%% Equivalent to local_path(Components, ?MODULE).
77+
local_path(Components) ->
78+
local_path(Components, ?MODULE).

0 commit comments

Comments
 (0)