aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2008-03-02 15:57:29 -0500
committerBrian Cully <github.20.shmit@spamgourmet.com>2008-03-02 15:57:29 -0500
commit2891b40b3a7799f67ba83c4914500cba7ca8aa1c (patch)
tree41a13132e8480fa67439acdfdc01beb52e705b8a
parent04f18c9d501b444d3bde4d38fd674df8c3a2db3d (diff)
downloadmysqlerl-2891b40b3a7799f67ba83c4914500cba7ca8aa1c.tar.gz
mysqlerl-2891b40b3a7799f67ba83c4914500cba7ca8aa1c.zip
Handle port failure properly.
Don't shut down during a call, but deliver a connection_closed message instead. Restart the port automatically.
-rw-r--r--src/mysqlerl_port.erl28
1 files changed, 15 insertions, 13 deletions
diff --git a/src/mysqlerl_port.erl b/src/mysqlerl_port.erl
index afcdbf8..572bc11 100644
--- a/src/mysqlerl_port.erl
+++ b/src/mysqlerl_port.erl
@@ -32,8 +32,21 @@ terminate(Reason, State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-handle_call(#req{request = Request}, _From, State) ->
- {reply, make_request(State#state.ref, Request), State}.
+handle_call(#req{request = Request}, From, #state{ref = Ref} = State) ->
+ io:format("DEBUG: Sending request: ~p~n", [Request]),
+ port_command(Ref, term_to_binary(Request)),
+ receive
+ {Ref, {data, Res}} ->
+ {reply, binary_to_term(Res), State};
+ {'EXIT', Ref, Reason} ->
+ gen_server:reply(From, {error, connection_closed}),
+ {stop, #port_closed{reason = Reason}, State};
+ Other ->
+ error_logger:warning_msg("Got unknown query response: ~p~n",
+ [Other]),
+ gen_server:reply(From, {error, connection_closed}),
+ {stop, {unknownreply, Other}, State}
+ end.
handle_cast(_Request, State) ->
{noreply, State}.
@@ -41,14 +54,3 @@ handle_cast(_Request, State) ->
handle_info({'EXIT', Ref, Reason}, #state{ref = Ref} = State) ->
io:format("DEBUG: Port ~p closed on ~p.~n", [Ref, State]),
{stop, #port_closed{reason = Reason}, State}.
-
-make_request(Ref, Req) ->
- io:format("DEBUG: Sending request: ~p~n", [Req]),
- port_command(Ref, term_to_binary(Req)),
- receive
- {Ref, {data, Res}} -> binary_to_term(Res);
- Other ->
- error_logger:warning_msg("Got unknown query response: ~p~n",
- [Other]),
- exit({badreply, Other})
- end.