Skip to content

Commit

Permalink
bump hackney to latest stable 0.14.3 .
Browse files Browse the repository at this point in the history
- fix memory leaks
- fix socket lingering
  • Loading branch information
benoitc committed Oct 28, 2014
1 parent bb864b6 commit 4da9d58
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

%% hackney for doing HTTP requests
{hackney, ".*", {git, "git://github.com/benoitc/hackney.git",
{tag, "0.13.0"}}}
{tag, "0.14.3"}}}
]}.
2 changes: 1 addition & 1 deletion rebar_dev.config
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

%% hackney for doing HTTP requests
{hackney, ".*", {git, "git://github.com/benoitc/hackney.git",
{tag, "0.13.0"}}},
{tag, "0.14.3"}}},

{edown, ".*",
{git, "git://github.com/esl/edown.git", "HEAD"}}
Expand Down
9 changes: 3 additions & 6 deletions src/couchbeam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ all_dbs(#server{url=ServerUrl, options=Opts}) ->
db_exists(#server{url=ServerUrl, options=Opts}, DbName) ->
Url = hackney_url:make_url(ServerUrl, dbname(DbName), []),
case couchbeam_httpc:db_request(head, Url, [], <<>>, Opts, [200]) of
{ok, _, _, Ref} ->
hackney:skip_body(Ref),
{ok, 200, _}->
true;
_Error ->
false
Expand Down Expand Up @@ -441,9 +440,7 @@ doc_exists(#db{server=Server, options=Opts}=Db, DocId) ->
DocId1 = couchbeam_util:encode_docid(DocId),
Url = hackney_url:make_url(server_url(Server), doc_url(Db, DocId1), []),
case couchbeam_httpc:db_request(head, Url, [], <<>>, Opts, [200]) of
{ok, _, _, Ref} ->
hackney:skip_body(Ref),
true;
{ok, _, _} -> true;
_Error -> false
end.

Expand Down Expand Up @@ -765,7 +762,7 @@ lookup_doc_rev(#db{server=Server, options=Opts}=Db, DocId, Params) ->
Url = hackney_url:make_url(server_url(Server), doc_url(Db, DocId1),
Params),
case couchbeam_httpc:db_request(head, Url, [], <<>>, Opts, [200]) of
{ok, _, Headers, _} ->
{ok, _, Headers} ->
HeadersDict = hackney_headers:new(Headers),
re:replace(hackney_headers:get_value(<<"etag">>, HeadersDict),
<<"\"">>, <<>>, [global, {return, binary}]);
Expand Down
8 changes: 7 additions & 1 deletion src/couchbeam_view.erl
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,20 @@ fold_view_results(Ref, Fun, Acc) ->
collect_view_results(Ref, Acc) ->
receive
{Ref, done} ->
io:format("got done ~n", []),

This comment has been minimized.

Copy link
@gdamjan

gdamjan Nov 12, 2014

this, and the one below were probably not supposed to be commited.

Rows = lists:reverse(Acc),
{ok, Rows};
{Ref, {row, Row}} ->
io:format("got row ~p~n", [Row]),

This comment has been minimized.

Copy link
@gdamjan

gdamjan Nov 12, 2014

this one too

collect_view_results(Ref, [Row|Acc]);
{Ref, {error, Error}} ->
%% in case we got some results
Rows = lists:reverse(Acc),
{error, Error, Rows}
{error, Error, Rows};
Else ->
io:format("got unknown message ~p~n", [Else])
after 10000 ->
{error, timeout}
end.

view_request(#db{options=Opts}, Url, Args) ->
Expand Down
52 changes: 29 additions & 23 deletions src/couchbeam_view_stream.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,44 @@ init_stream(Parent, Owner, StreamRef, {_Db, _Url, _Args}=Req,
do_init_stream({#db{options=Opts}, Url, Args}, #state{mref=MRef}=State) ->
%% we are doing the request asynchronously
FinalOpts = [{async, once} | Opts],
{ok, Ref} = case Args#view_query_args.method of
Reply = case Args#view_query_args.method of
get ->
hackney:request(get, Url, [], <<>>, FinalOpts);
post ->
Body = couchbeam_ejson:encode({[{<<"keys">>,
Args#view_query_args.keys}]}),
hackney:request(post, Url, [], Body, FinalOpts)
end,
receive
{'DOWN', MRef, _, _, _} ->
%% parent exited there is no need to continue
exit(normal);
{hackney_response, Ref, {status, 200, _}} ->
#state{parent=Parent,
owner=Owner,
ref=StreamRef,
async=Async} = State,

DecoderFun = jsx:decoder(?MODULE, [Parent, Owner, StreamRef,
MRef, Ref, Async],
[stream]),
{ok, State#state{client_ref=Ref,
decoder=DecoderFun}};

{hackney_response, Ref, {error, Reason}} ->
exit(Reason)
after ?TIMEOUT ->
exit(timeout)

case Reply of
{ok, Ref} ->
receive
{'DOWN', MRef, _, _, _} ->
%% parent exited there is no need to continue
exit(normal);
{hackney_response, Ref, {status, 200, _}} ->
#state{parent=Parent,
owner=Owner,
ref=StreamRef,
async=Async} = State,

DecoderFun = jsx:decoder(?MODULE, [Parent, Owner,
StreamRef, MRef, Ref,
Async], [stream]),
{ok, State#state{client_ref=Ref,
decoder=DecoderFun}};

{hackney_response, Ref, {error, Reason}} ->
exit(Reason)
after ?TIMEOUT ->
exit(timeout)
end;
Error ->
{error, Error}
end.



loop(#state{owner=Owner,
ref=StreamRef,
mref=MRef,
Expand Down Expand Up @@ -147,7 +154,7 @@ decode_data(Data, #state{owner=Owner,
ref=StreamRef,
client_ref=ClientRef,
decoder=DecodeFun}=State) ->
try
try
{incomplete, DecodeFun2} = DecodeFun(Data),
try DecodeFun2(end_stream) of done ->
%% stop the request
Expand Down Expand Up @@ -343,7 +350,6 @@ send_row(Row, #viewst{owner=Owner, ref=Ref}=ViewSt) ->
Owner ! {Ref, {row, Row}},
maybe_continue_decoding(ViewSt).


%% eventually wait for the next call from the parent
maybe_continue_decoding(#viewst{parent=Parent,
owner=Owner,
Expand Down

0 comments on commit 4da9d58

Please sign in to comment.