Skip to content

Commit a4b3802

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 a4b3802

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

lib/tesla/adapter/hackney.ex

Lines changed: 16 additions & 16 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,35 @@ 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,
122+
fn -> nil end,
123123
fn
124-
{:done, state} ->
125-
{:halt, state}
124+
nil ->
125+
{[], :hackney.stream_body(ref)}
126126

127-
{:ok, data, state} ->
128-
{[data], state}
127+
:done ->
128+
{:halt, nil}
129+
130+
{:ok, data} ->
131+
{[data], nil}
129132

130133
{:error, reason} ->
131134
raise inspect(reason)
132-
133-
state ->
134-
{[], :hackney_response.stream_body(state)}
135135
end,
136-
&:hackney_response.close/1
136+
fn _ -> :hackney.close(ref) end
137137
)
138138

139139
{:ok, status, headers, body}
140140
end
141141

142-
defp handle_stream(response) do
142+
defp handle_stream(response, pid) do
143143
case handle(response) do
144144
{:ok, _status, _headers, ref} = response when is_reference(ref) ->
145-
handle_stream(response)
145+
handle_stream(response, pid)
146146

147147
response ->
148148
response

test/tesla/adapter/hackney_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ 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
7575
assert(is_function(response.body))

0 commit comments

Comments
 (0)