Skip to content
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

Make server stream work with mint client and add keepalive. #352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nulian
Copy link

@nulian nulian commented Feb 23, 2024

This is what I been using for a while in our application.

Changed connection open? check to :read so server streaming is seen as a valid state for the connection.

And added a keepalive to make sure the connection is still open of the server stream because if a service like nginx breaks the connection you won't get any notification so you will never close the connection even if it's already broken.

Also added some error handling cases so it won't cause any server errors.

#312

Copy link
Contributor

@beligante beligante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall code LGTM, great job!
The connection process has a good test coverage, could you add some tests for this ?

@@ -94,6 +106,9 @@ defmodule GRPC.Client.Adapters.Mint.ConnectionProcess do
def handle_call(:disconnect, _from, state) do
# TODO add a code to if disconnect is brutal we just stop if is friendly we wait for pending requests
{:ok, conn} = Mint.HTTP.close(state.conn)

finish_all_pending_requests(state)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

Comment on lines +117 to +119
keep_alive = Keyword.get(opts, :keep_alive, false)

[transport_opts: Keyword.merge(@default_transport_opts, transport_opts), keep_alive: keep_alive]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, it's better if you add a question mark to this option, since, usually keep_alive is a provided as a number by convention.

Suggested change
keep_alive = Keyword.get(opts, :keep_alive, false)
[transport_opts: Keyword.merge(@default_transport_opts, transport_opts), keep_alive: keep_alive]
keep_alive? = Keyword.get(opts, :keep_alive?, false)
[transport_opts: Keyword.merge(@default_transport_opts, transport_opts), keep_alive?: keep_alive?]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's valuable to add a module doc describing this adapter-specific option. WDYT?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transport_opts is forwarded to gen_tcp or ssl. The Mint docs list the most common ones, but there are more of them.

https://www.erlang.org/doc/man/gen_tcp.html#type-option

Now that you mentioned, keepalive on gen_tcp doesn't have a underscore

Comment on lines +204 to +205
check_connection_status(state)
{:noreply, state}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want the check_connection_status to handle the GS reply for you

Suggested change
check_connection_status(state)
{:noreply, state}
check_connection_status(state)

defp process_response({:error, request_ref, error}, state) do
pid = State.stream_response_pid(state, request_ref)
:ok = StreamResponseProcess.consume(pid, :error, error)
:ok = StreamResponseProcess.done(pid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you're sending done/1 here? By default, even if a error happens, the connection will produce a :done message when the stream is closed by the server

@polvalente
Copy link
Contributor

@nulian did you get a chance to act on the reviews?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants