Permalink
Browse files

Merge pull request #48 from lpgauth/upkeep/metal

Use Metal behaviour
  • Loading branch information...
lpgauth committed Dec 17, 2017
2 parents 428870f + e394e08 commit c6515dc7da9ecb825c846bf48655afa4e3c6ccf6
Showing with 66 additions and 34 deletions.
  1. +1 −0 .travis.yml
  2. +24 −4 doc/statsderl_server.md
  3. +1 −0 rebar.config
  4. +1 −1 rebar.lock
  5. +1 −1 src/statsderl.app.src
  6. +38 −28 src/statsderl_server.erl
View
@@ -17,6 +17,7 @@ language: erlang
notifications:
email: false
otp_release:
- 20.1
- 19.1
- 18.3
script: "make travis"
View
@@ -4,24 +4,35 @@
* [Function Index](#index)
* [Function Details](#functions)
__Behaviours:__ [`metal`](metal.md).
<a name="index"></a>
## Function Index ##
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#init-2">init/2</a></td><td></td></tr><tr><td valign="top"><a href="#start_link-1">start_link/1</a></td><td></td></tr></table>
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#handle_msg-2">handle_msg/2</a></td><td></td></tr><tr><td valign="top"><a href="#init-3">init/3</a></td><td></td></tr><tr><td valign="top"><a href="#start_link-1">start_link/1</a></td><td></td></tr><tr><td valign="top"><a href="#terminate-2">terminate/2</a></td><td></td></tr></table>
<a name="functions"></a>
## Function Details ##
<a name="init-2"></a>
<a name="handle_msg-2"></a>
### handle_msg/2 ###
<pre><code>
handle_msg(X1::term(), State::term()) -&gt; {ok, term()}
</code></pre>
<br />
<a name="init-3"></a>
### init/2 ###
### init/3 ###
<pre><code>
init(Parent::pid(), Name::atom()) -&gt; no_return()
init(Name::atom(), Parent::pid(), Opts::term()) -&gt; {ok, term()} | {stop, atom()}
</code></pre>
<br />
@@ -34,3 +45,12 @@ start_link(Name::atom()) -&gt; {ok, pid()}
</code></pre>
<br />
<a name="terminate-2"></a>
### terminate/2 ###
<pre><code>
terminate(Reason::term(), State::term()) -&gt; ok
</code></pre>
<br />
View
@@ -12,6 +12,7 @@
{deps, [
{foil, "0.1.0"},
{granderl, "0.1.5"},
{metal, "0.1.1"},
{parse_trans, "3.0.0"}
]}.
View
@@ -1,7 +1,7 @@
{"1.1.0",
[{<<"foil">>,{pkg,<<"foil">>,<<"0.1.0">>},0},
{<<"granderl">>,{pkg,<<"granderl">>,<<"0.1.5">>},0},
{<<"metal">>,{pkg,<<"metal">>,<<"0.1.1">>},1},
{<<"metal">>,{pkg,<<"metal">>,<<"0.1.1">>},0},
{<<"parse_trans">>,{pkg,<<"parse_trans">>,<<"3.0.0">>},0}]}.
[
{pkg_hash,[
View
@@ -1,5 +1,5 @@
{application, statsderl, [
{applications, [kernel, stdlib, granderl, foil]},
{applications, [kernel, stdlib, granderl, foil, metal]},
{description, "High Performance Erlang StatsD Client"},
{env, []},
{licenses, ["MIT"]},
View
@@ -5,20 +5,33 @@
-compile({inline_size, 512}).
-export([
init/2,
start_link/1
]).
-behaviour(metal).
-export([
init/3,
handle_msg/2,
terminate/2
]).
-record(state, {
header :: iodata(),
socket :: inet:socket()
}).
%% public
-spec init(pid(), atom()) ->
no_return().
-spec start_link(atom()) ->
{ok, pid()}.
init(Parent, Name) ->
start_link(Name) ->
metal:start_link(?SERVER, Name, undefined).
%% metal callbacks
-spec init(atom(), pid(), term()) ->
{ok, term()} | {stop, atom()}.
init(_Name, _Parent, _Opts) ->
BaseKey = ?ENV(?ENV_BASEKEY, ?DEFAULT_BASEKEY),
Hostname = ?ENV(?ENV_HOSTNAME, ?DEFAULT_HOSTNAME),
Port = ?ENV(?ENV_PORT, ?DEFAULT_PORT),
@@ -27,43 +40,40 @@ init(Parent, Name) ->
{ok, Header} ->
case gen_udp:open(0, [{active, false}]) of
{ok, Socket} ->
register(Name, self()),
proc_lib:init_ack(Parent, {ok, self()}),
loop(#state {
{ok, #state {
socket = Socket,
header = Header
});
}};
{error, Reason} ->
exit(Reason)
{stop, Reason}
end;
{error, Reason} ->
exit(Reason)
{stop, Reason}
end.
-spec start_link(atom()) ->
{ok, pid()}.
-spec handle_msg(term(), term()) ->
{ok, term()}.
start_link(Name) ->
proc_lib:start_link(?MODULE, init, [self(), Name]).
%% private
loop(#state {
handle_msg({cast, Packet}, #state {
header = Header,
socket = Socket
} = State) ->
receive
{cast, Packet} ->
erlang:port_command(Socket, [Header, Packet]),
loop(State);
{inet_reply, _Socket, ok} ->
loop(State);
{inet_reply, _Socket, {error, Reason}} ->
statsderl_utils:error_msg("inet_reply error: ~p~n", [Reason]),
loop(State)
end.
erlang:port_command(Socket, [Header, Packet]),
{ok, State};
handle_msg({inet_reply, _Socket, ok}, State) ->
{ok, State};
handle_msg({inet_reply, _Socket, {error, Reason}}, State) ->
statsderl_utils:error_msg("inet_reply error: ~p~n", [Reason]),
{ok, State}.
-spec terminate(term(), term()) ->
ok.
terminate(_Reason, _State) ->
ok.
%% private
udp_header(Hostname, Port, BaseKey) ->
case statsderl_utils:getaddrs(Hostname) of
{ok, {A, B, C, D}} ->

0 comments on commit c6515dc

Please sign in to comment.