0

I'm using riak-erlang-client.

According to this guide, I can connect to one node:

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

I know when the node is down, it will send the message {tcp_closed, _FromPid}, but where shall I receive this message and connect to another alive node? Or is there better way to do this?

goofansu
  • 2,277
  • 3
  • 30
  • 48

1 Answers1

1

The message

{tcp_closed, Socket}

Will be delivered to the riakc_pb_socket process, at line 1026, and then the disconnect function will notify any requests of the closure by sending

{error, disconnected} 

If auto_reconnect is set, the riakc_pb_socket process will attempt to re-establish the connection.

Your code will see the disconnected error in response to some request to the server.

Excerpts from riakc_pb_socket.erl:

Line 1025:

handle_info({tcp_closed, _Socket}, State) ->
    disconnect(State);

The disconnect function:

%% @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.
Joe
  • 25,000
  • 3
  • 22
  • 44
  • Thank you. I found riak-ruby-client can connect to a nodes cluster. But there is no such method for riak-erlang-client. – goofansu Jun 18 '13 at 05:28