-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Orphaned open requests #211
Comments
Hey @gaynetdinov, I'm struggling to understand the problem here. I don't know what you mean when you say things like "create a new |
You get the
This should not happen since the old message has a different socket reference than the new connection, see https://github.com/ericmj/mint/blob/master/lib/mint/http1.ex#L379. What version of mint are you using?
While the specific scenario you are reporting here shouldn't happen you will always need a way to handle this. Requests can fail at any time and you need to decide if you should retry or let the error bubble up.
This is not handled by Mint. Mint will wait for more messages when you call |
Yes, when I get
Everything is happening inside one GenServer.
I don't actually get Take a look at the example in the docs. I've noticed this behaviour because my GenServer takes a lot of memory over time. So I checked its state and the state was full of
I've tried 0.2.1 and 0.4.0. |
This is expected. You do not get more data on closed connections, you can only expect messages on open connections.
Regardless of this issue you need a way to time out requests and if you hit a timeout you need to delete all references and restart the connection to clear up the memory. |
Also a very important thing: when a connection is closed, all the requests that were open on that connection can be considered closed. |
Sorry I'm confused now. what does it mean that requests are closed? does it mean that it's safe to retry all these requests? Or I should always consider such requests as 'lost' and reply to the client something like 503, retry_later? |
If the connection is closed, you could still get one last message because it might be sitting in your mailbox already when you notice that the connection is closed. I would just throw away the connection and throw away all the messages that return
This is not a Mint problem in any way. It's a computer science/HTTP problem 😃 Some requests are probably safe to retry, like |
Thank you so much for the detailed answers. You helped me to clarify some open questions I've had. Much appreciated. 💜 |
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 viaMint.HTTP.connect(:http, host, port)
. Imagine a sitation when one request have been sent usingmint_conn
. This request is stillopen
(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! 💜
The text was updated successfully, but these errors were encountered: