我正在使用riak-erlang-client。
根据本指南,我可以连接到一个节点:
1> {ok, Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087).
{ok,<0.56.0>}
我知道当节点关闭时,它会发送消息{tcp_closed, _FromPid}
,但是我应该在哪里接收到这个消息并连接到另一个活动节点?还是有更好的方法来做到这一点?
讯息
{tcp_closed, Socket}
将在第 1026 行传递给 riakc_pb_socket 进程,然后断开功能将通过发送通知任何关闭请求
{error, disconnected}
如果设置了 auto_reconnect,riakc_pb_socket 进程将尝试重新建立连接。
您的代码将看到断开连接错误以响应对服务器的某些请求。
riakc_pb_socket.erl 的摘录:
第 1025 行:
handle_info({tcp_closed, _Socket}, State) ->
disconnect(State);
断线功能:
%% @private
%% Disconnect socket if connected
disconnect(State) ->
%% Tell any pending requests we've disconnected
_ = case State#state.active of
undefined ->
ok;
Request ->
send_caller({error, disconnected}, Request)
end,
%% Make sure the connection is really closed
case State#state.sock of
undefined ->
ok;
Sock ->
gen_tcp:close(Sock)
end,
%% Decide whether to reconnect or exit
NewState = State#state{sock = undefined, active = undefined},
case State#state.auto_reconnect of
true ->
%% Schedule the reconnect message and return state
erlang:send_after(State#state.reconnect_interval, self(), reconnect),
{noreply, increase_reconnect_interval(NewState)};
false ->
{stop, disconnected, NewState}
end.