当旧节点关闭时如何连接到新的riak节点?

时间:2013-06-16 15:06:15

标签: erlang riak

我正在使用riak-erlang-client。

根据this guide,我可以连接到一个节点:

 1> {ok, Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087).
    {ok,<0.56.0>}

我知道节点何时关闭,它会发送消息{tcp_closed, _FromPid},但是我应该在哪里收到此消息并连接到另一个活动节点?或者有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

消息

{tcp_closed, Socket}

将发送到line 1026的riakc_pb_socket进程,然后disconnect function将通过发送

通知任何关闭请求
{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.