Commits

Oscar Hellström  committed 4474caf

Refactor manager so that the previously re-added code isn't needed.

  • Participants
  • Parent commits 80c928a

Comments (0)

Files changed (1)

File src/lhttpc_manager.erl

                     {_, Timer} = dict:fetch(Socket, State#httpc_man.sockets),
                     cancel_timer(Timer, Sockets),
                     NewState = State#httpc_man{
-                        destinations = dict:store(Destination, Sockets,
+                        destinations = update_dest(Destination, Sockets,
                             Destinations),
                         sockets = dict:erase(Socket,
                             State#httpc_man.sockets)
                     lhttpc_sock:setopts(Socket, [{active, true}], Ssl),
                     {no_socket, State}
             end;
-        {ok, []} ->
-            {no_socket, State};
         error ->
             {no_socket, State}
     end.
             cancel_timer(Timer, Socket),
             lhttpc_sock:close(Socket, Ssl),
             OldSockets = dict:fetch(Destination, Destinations),
-            NewDestinations = case lists:delete(Socket, OldSockets) of
-                []      -> dict:erase(Destination, Destinations);
-                Sockets -> dict:store(Destination, Sockets, Destinations)
-            end,
             State#httpc_man{
-                destinations = NewDestinations,
+                destinations = update_dest(Destination, lists:delete(Socket,
+                        OldSockets), Destinations),
                 sockets = dict:erase(Socket, State#httpc_man.sockets)
             };
         error ->
     lhttpc_sock:setopts(Socket, [{active, once}], Ssl),
     Destinations = State#httpc_man.destinations,
     Sockets = case dict:find(Destination, Destinations) of
-        {ok, S} -> 
-            S;
-        error ->
-            []
+        {ok, S} -> S;
+        error   -> []
     end,
     State#httpc_man{
         destinations = dict:store(Destination, [Socket | Sockets],
             State#httpc_man.sockets)
     }.
 
+update_dest(Destination, [], Destinations) ->
+    dict:erase(Destination, Destinations);
+update_dest(Destination, Sockets, Destinations) ->
+    dict:store(Destination, Sockets, Destinations).
+
 close_sockets(Sockets) ->
     lists:foreach(fun({Socket, {{_, _, Ssl}, Timer}}) ->
                 lhttpc_sock:close(Socket, Ssl),