Description
Hi! 👋
I'm using Mint in a proxy gateway phoenix app. The phoenix app accepts a request, sends another HTTP request using Mint and then proxying response back to the client using Plug.Conn.chunk/2
function every time Mint returns {:data, request_ref, data}
.
For handling Mint connections I'm (more or less) using the same architecture you've described in the documentation https://hexdocs.pm/mint/architecture.html#wrapping-a-mint-connection-in-a-genserver
The GenServer is started, mint_conn
is obtained via Mint.HTTP.connect(:http, host, port)
. Imagine a sitation when one request have been sent using mint_conn
. This request is still open
(i.e. I haven't received :done
yet for this request, Mint.HTTP.open_request_count(mint_conn)
would return 1). Sometimes when I send another request, mint_conn
would return :closed
error. When I encounter this scenario I can take 2 different approaches:
-
I create a new
mint_conn
to the same host and port and send the second request through the newmint_conn
, because the previous one was closed. If I do so, the first request which was sent using the firstmint_conn
would never come back, i.e. I'd never receive it inhandle_info
in my GenServer. So this way I'd loose this request because I'd never receive the response. -
I create a new
mint_conn
to the same host and port, take the first request from the GenServer state and re-send it again using the new just created Mint connection. This way I'd not loose this request, but I'd send it twice. Another interesting thing is that I'd eventually receive the first orphaned open requests from the firstmint_conn
inhandle_message
andMint.HTTP.stream(state.mint_conn, message)
would return{:error, mint_conn, %Mint.HTTPError{reason: :unexpected_data, data}}
.
Could you please help me understand how to properly deal with this sitation? I don't want to loose requests and I also don't want to re-send it (it would not work for non-GET requests anyways). Maybe there is a way to instruct Mint to still receive/wait for the open requests even if I create a new connection to the same host/port?
Thank you! 💜