Skip to content

Commit 4353e9d

Browse files
author
Tim Smart
committed
Change stream_response to stream_to_pid
Hackney only allows the controller process to access the ref
1 parent c896989 commit 4353e9d

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

lib/tesla/adapter/hackney.ex

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ if Code.ensure_loaded?(:hackney) do
7373
defp request(method, url, headers, body, opts) do
7474
response = :hackney.request(method, url, headers, body || '', opts)
7575

76-
case Keyword.get(opts, :stream_response, false) do
77-
true -> handle_stream(response)
78-
false -> handle(response)
76+
case Keyword.get(opts, :stream_to_pid) do
77+
pid when is_pid(pid) -> handle_stream(response, pid)
78+
_ -> handle(response)
7979
end
8080
end
8181

@@ -114,35 +114,34 @@ if Code.ensure_loaded?(:hackney) do
114114

115115
defp handle({:ok, status, headers, body}), do: {:ok, status, headers, body}
116116

117-
defp handle_stream({:ok, status, headers, ref}) when is_reference(ref) do
118-
state = :hackney_manager.get_state(ref)
117+
defp handle_stream({:ok, status, headers, ref}, pid) when is_reference(ref) do
118+
:hackney.controlling_process(ref, pid)
119119

120120
body =
121121
Stream.resource(
122-
fn -> state end,
123-
fn
124-
{:done, state} ->
125-
{:halt, state}
126-
127-
{:ok, data, state} ->
128-
{[data], state}
129-
130-
{:error, reason} ->
131-
raise inspect(reason)
132-
133-
state ->
134-
{[], :hackney_response.stream_body(state)}
122+
fn -> nil end,
123+
fn _ ->
124+
case :hackney.stream_body(ref) do
125+
:done ->
126+
{:halt, nil}
127+
128+
{:ok, data} ->
129+
{[data], nil}
130+
131+
{:error, reason} ->
132+
raise inspect(reason)
133+
end
135134
end,
136-
&:hackney_response.close/1
135+
fn _ -> :hackney.close(ref) end
137136
)
138137

139138
{:ok, status, headers, body}
140139
end
141140

142-
defp handle_stream(response) do
141+
defp handle_stream(response, pid) do
143142
case handle(response) do
144143
{:ok, _status, _headers, ref} = response when is_reference(ref) ->
145-
handle_stream(response)
144+
handle_stream(response, pid)
146145

147146
response ->
148147
response

test/tesla/adapter/hackney_test.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ defmodule Tesla.Adapter.HackneyTest do
6363
assert {:error, :fake_error} = call(request)
6464
end
6565

66-
test "get with `stream_response: true` option" do
66+
test "get with `stream_to_pid: pid` option" do
6767
request = %Env{
6868
method: :get,
6969
url: "#{@http}/ip"
7070
}
7171

72-
assert {:ok, %Env{} = response} = call(request, stream_response: true)
72+
assert {:ok, %Env{} = response} = call(request, stream_to_pid: self())
7373

7474
assert response.status == 200
75-
assert(is_function(response.body))
75+
assert is_function(response.body)
76+
assert is_bitstring(Enum.join(response.body))
7677
end
7778
end

0 commit comments

Comments
 (0)