Skip to content

Commit

Permalink
* Resolve host address at init time (saving multiple DNS lookups late…
Browse files Browse the repository at this point in the history
…r on...)

* Moved send from cast to call -- Slightly slower but eliminates the problem where hermes remains busy after load has subsided.
  • Loading branch information
bigkevmcd committed Feb 23, 2011
1 parent 667287e commit e6459ed
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/syslog.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ send(Name, Msg, Opts) when is_atom(Name), is_list(Msg), is_list(Opts) ->
Ident = get_ident(Opts),
Pid = get_pid(Opts),
Packet = ["<", Level, "> ", Ident, "[", Pid, "]: ", Msg, "\n"],
gen_server:cast(Name, {send, iolist_to_binary(Packet)}).
gen_server:call(Name, {send, iolist_to_binary(Packet)}).

%%====================================================================
%% gen_server callbacks
Expand All @@ -70,11 +70,12 @@ send(Name, Msg, Opts) when is_atom(Name), is_list(Msg), is_list(Opts) ->
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([Host, Port]) ->
{ok, Addr} = inet:getaddr(Host, inet),
case gen_udp:open(0) of
{ok, Socket} ->
{ok, Socket} ->
{ok, #state{
socket = Socket,
address = Host,
address = Addr,
port = Port
}};
{error, Reason} ->
Expand All @@ -90,7 +91,8 @@ init([Host, Port]) ->
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(_Msg, _From, State) ->
handle_call({send, Packet}, _From, #state{socket=Socket, address=Address, port=Port}=State) when is_binary(Packet) ->
gen_udp:send(Socket, Address, Port, Packet),
{reply, ok, State}.

%%--------------------------------------------------------------------
Expand All @@ -99,10 +101,6 @@ handle_call(_Msg, _From, State) ->
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast({send, Packet}, #state{socket=Socket, address=Address, port=Port}=State) when is_binary(Packet) ->
gen_udp:send(Socket, Address, Port, Packet),
{noreply, State};

handle_cast(_Msg, State) ->
{noreply, State}.

Expand Down

0 comments on commit e6459ed

Please sign in to comment.