Skip to content

Orphaned open requests #211

Closed
Closed
@gaynetdinov

Description

@gaynetdinov

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:

  1. I create a new mint_conn to the same host and port and send the second request through the new mint_conn, because the previous one was closed. If I do so, the first request which was sent using the first mint_conn would never come back, i.e. I'd never receive it in handle_info in my GenServer. So this way I'd loose this request because I'd never receive the response.

  2. 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 first mint_conn in handle_message and Mint.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! 💜

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions