From e9fbad1b5a9e67034fa77b354d85eb96ae4e9e8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 25 Jul 2023 15:25:43 +0200 Subject: [PATCH 1/2] Do not use streams with async_request Closes #235. --- lib/finch.ex | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/lib/finch.ex b/lib/finch.ex index ee08aea0..f91a9305 100644 --- a/lib/finch.ex +++ b/lib/finch.ex @@ -381,6 +381,10 @@ defmodule Finch do @doc """ Sends an HTTP request asynchronously, returning a request reference. + This function works as a firehose and it will send messages to the + current process as the HTTP response arrives. If you want more control + over how messages are streamed, see `stream/5`. + If the request is sent using HTTP1, an extra process is spawned to consume messages from the underlying socket. If you wish to maximize request rate, a strategy using `request/3` or `stream/5` should be @@ -411,26 +415,13 @@ defmodule Finch do ## Example - iex> Stream.resource( - ...> fn -> - ...> Finch.build(:get, "https://httpbin.org/stream/5") - ...> |> Finch.async_request(MyFinch) - ...> end, - ...> fn ref -> - ...> receive do - ...> {^ref, :done} -> {:halt, ref} - ...> {^ref, response} -> {[response], ref} - ...> end - ...> end, - ...> fn _ref -> :ok end - ...> ) |> Enum.to_list() - [ - {:status, 200}, - {:headers, [...]}, - {:data, "..."}, - ... - :done - ] + iex> req = Finch.build(:get, "https://httpbin.org/stream/5") + iex> ref = Finch.async_request(req, MyFinch) + iex> flush() + {ref, {:status, 200}} + {ref, {:headers, [...]}} + {ref, {:data, "..."}} + {ref, :done} ## Options From 0ee3b7c085d8fc111176967dd07af09cbb067f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 25 Jul 2023 15:54:46 +0200 Subject: [PATCH 2/2] Update finch.ex --- lib/finch.ex | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/finch.ex b/lib/finch.ex index f91a9305..f5fd7cd2 100644 --- a/lib/finch.ex +++ b/lib/finch.ex @@ -381,14 +381,12 @@ defmodule Finch do @doc """ Sends an HTTP request asynchronously, returning a request reference. - This function works as a firehose and it will send messages to the - current process as the HTTP response arrives. If you want more control - over how messages are streamed, see `stream/5`. - If the request is sent using HTTP1, an extra process is spawned to - consume messages from the underlying socket. If you wish to maximize - request rate, a strategy using `request/3` or `stream/5` should be - used to avoid this overhead. + consume messages from the underlying socket. The messages are sent + to the current process as soon as they arrive, as a firehose. If + you wish to maximize request rate or have more control over how + messages are streamed, a strategy using `request/3` or `stream/5` + should be used instead. ## Receiving the response