1. Basho
  2. Untitled project
  3. wriaki


Bryan Fink  committed fbc6efe

close connections explicitly when done with them

This is important for the PB client because it holds a socket open in a
gen_server. The link from the webmachine resource to the
riakc_pb_socket is not enough to clean up when the resource finishes,
because it exits with Reason=normal in that case, so the riakc_pb_socket
gen_server lives on.

This adds wrc:disconnect/1, which closes the PB socket, but does nothing
for the HTTP client (since nothing is needed there).

The resources set the 'client' field of their context records or
'disconnected' after closing their client connections, in an effort to
help find bugs where code may try to use a client after it has been

  • Participants
  • Parent commits 2b12420
  • Branches default

Comments (0)

Files changed (7)

File apps/wriaki/src/article_history.erl

View file
  • Ignore whitespace
                    [{link, <<"archive">>, '_', false},
 %%%{reduce, {jsanon, time_order_fun()}, <<>>, false}, %TODO: paging
                     {map, {jsanon, summary_fun()}, <<>>, true}]),
+    wrc:disconnect(Client),
     {ok, Results}.
 %% code for summary map phase is in priv/mapred/summary_map.js

File apps/wriaki/src/session_resource.erl

View file
  • Ignore whitespace
+         finish_request/2,
     {ok, Client} = wrc:connect(),
     {ok, #ctx{client=Client}}.
+finish_request(RD, Ctx=#ctx{client=C}) ->
+    wrc:disconnect(C),
+    {true, RD, Ctx#ctx{client=disconnected}}.
 allowed_methods(RD, Ctx) ->
     {['HEAD','GET','DELETE'], RD, Ctx}.

File apps/wriaki/src/user_resource.erl

View file
  • Ignore whitespace
                 wriaki_auth:start_session(RD, User),
             {ok, UC} = wrc:set_client_id(C, username(RD)),
             wrc:put(UC, NewUser),
-            {true, NewRD, NewUser};
+            {true, NewRD, Ctx#ctx{user=NewUser}};
         false ->
             {{halt, 409},
 edit_mode(RD) ->
     wrq:get_qs_value("edit", RD) /= undefined.
-finish_request(RD, Ctx) ->
+finish_request(RD, Ctx=#ctx{client=C}) ->
+    wrc:disconnect(C),
+    DCtx = Ctx#ctx{client=disconnected},
     case wrq:response_code(RD) of
         404 ->
             {ok, Content} = user_404_dtl:render(
                "Content-type", "text/html; charset=utf-8",
                wrq:set_resp_body(Content, RD)),
-             Ctx};
+             DCtx};
         _ ->
-            {true, RD, Ctx}
+            {true, RD, DCtx}

File apps/wriaki/src/wiki_resource.erl

View file
  • Ignore whitespace
                     true  -> render_404_editor(RD, Ctx);
                     false -> render_404(RD, Ctx)
+            wrc:disconnect(NewCtx#ctx.client),
                "Content-type", "text/html; charset=utf-8",
                wrq:set_resp_body(Content, NewRD)),
-             NewCtx};
+             NewCtx#ctx{client=disconnected}};
         _ ->
-            {true, RD, Ctx}
+            wrc:disconnect(Ctx#ctx.client),
+            {true, RD, Ctx#ctx{client=disconnected}}
 render_404_editor(RD, Ctx) ->

File apps/wriaki/src/wrc.erl

View file
  • Ignore whitespace
 -export([connect/0, connect/1,
+         disconnect/1,
          get_client_id/1, set_client_id/2,
     {ok, #wrc{module = riakc_pb_socket,
               client = C}}.
+disconnect(#wrc{module=riakc_pb_socket, client=C}) ->
+    riakc_pb_socket:stop(C);
+disconnect(_WRC) ->
+    ok.
 ping(RC) ->
     ?PASS0(RC, ping).

File apps/wriaki/src/wriaki.erl

View file
  • Ignore whitespace
     {ok, Client} = wrc:connect(),
     ok = wrc:set_bucket(Client, ?B_ARTICLE,
                         [{allow_mult, true}|search_hook()]),
-    ok = wrc:set_bucket(Client, ?B_HISTORY, [{allow_mult, true}]).
+    ok = wrc:set_bucket(Client, ?B_HISTORY, [{allow_mult, true}]),
+    wrc:disconnect(Client).
 search_hook() ->
     case search_enabled() of

File apps/wriaki/src/wriaki_auth.erl

View file
  • Ignore whitespace
 check_user_pass(Username, Password) ->
     {ok, Client} = wrc:connect(),
-    case wrc:get(Client, ?B_USER, Username) of
+    LookupResult = wrc:get(Client, ?B_USER, Username),
+    wrc:disconnect(Client),
+    case LookupResult of
         {ok, User} ->
             case user_resource:password_matches(User, Password) of
                 true  -> {ok, User};
                     {ok, UC} = wrc:set_client_id(C, session:get_user(Session)),
                     wrc:put(UC, session:refresh(Session)),
                     {ok, User} = wuser:fetch(UC, session:get_user(Session)),
+                    wrc:disconnect(UC),
                     {{cookie, SessionCookie}, User};
                 false ->
+                    wrc:disconnect(C),
         {error, notfound} ->
+            wrc:disconnect(C),
     SessionCookie = wobj:key(Session),
     {ok, C} = wrc:connect(Username),
     ok = wrc:put(C, Session),
+    wrc:disconnect(C),
       [ mochiweb_cookies:cookie(K, V, [{path, "/"}])
         || {K, V} <- [{?USERNAME_COOKIE, Username},