Commits

Dmitry Belyaev  committed 0916ed8

do not hang if connection cannot be established

  • Participants
  • Parent commits 52c008d
  • Tags emongo_0.0.4-6

Comments (0)

Files changed (2)

File src/emongo_pool.erl

 %%                                       {stop, Reason, State}
 %% Description: Handling all non call/cast messages
 %%--------------------------------------------------------------------
-handle_info({'EXIT', Pid, Reason}, #pool{conn_pid=Pids}=State) ->
-    case case Reason of
-             tcp_closed -> true;
-             {tcp_error, _Reason} -> true;
-             _ -> false
-         end
-        of
-        true ->
-            Pids1 = queue:filter(fun(Item) -> Item =/= Pid end, Pids),
-
-            NewState = do_open_connections(State#pool{conn_pid = Pids1}),
-            {noreply, NewState};
-        _ ->
-            {noreply, State}
-    end;
+handle_info({'EXIT', Pid, _Reason}, #pool{conn_pid=Pids}=State) ->
+    Pids1 = queue:filter(fun(Item) -> Item =/= Pid end, Pids),
+    {noreply, State#pool{conn_pid = Pids1, active=false}};
 
 handle_info(poll, State) ->
     erlang:send_after(?POLL_TIME, self(), poll),
     case get_pid(Pool) of
         {{Pid, Database, ReqId}, NewPool} ->
             PacketLast = emongo_packet:get_last_error(Database, ReqId),
-            case catch emongo_server:send_recv(Pid, ReqId, PacketLast, ?TIMEOUT) of
+            case catch emongo_server:send_recv(Pid, ReqId, PacketLast, 500) of
                 {'EXIT', _} ->
                     NewPool#pool{active=false};
                 _ ->

File src/emongo_server.erl

 	end.
 
 open_socket(Host, Port) ->
-	case gen_tcp:connect(Host, Port, [binary, {active, true}, {nodelay, true}]) of
+	case gen_tcp:connect(Host, Port, [binary, {active, true}, {nodelay, true}], ?TIMEOUT) of
 		{ok, Sock} ->
 			Sock;
 		{error, Reason} ->