Skip to content

Require grpc ~> 1.1 - #113

Merged
rockneurotiko merged 5 commits into
mainfrom
upgrade-grpc-1-1-0
Nov 20, 2026
Merged

rockneurotiko merged 5 commits into
mainfrom
upgrade-grpc-1-1-0

Conversation

@rockneurotiko

@rockneurotiko rockneurotiko commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Bumps the requirement to grpc ~> 1.1.

  • The Mint adapter in 1.1 rejects unknown options, so it no longer gets Gun's :http2_opts. It now gets :keepalive and :keepalive_tolerance (3): :keepalive_interval_ms applies to :mint, and a connection that stops answering pings is closed and replaced.
  • Adds a close_send/2 callback to Streaming.Client (GRPC.Stub.end_stream/1). On graceful shutdown the stream manager half-closes the stream and waits up to 3s for the server to end it, falling back to cancel. This avoids killing a reader that still owns the stream.

On grpc 1.0 the Mint adapter silently ignores the keepalive options, so Mint would run without keepalive.

@rockneurotiko

Copy link
Copy Markdown
Contributor Author

Hey @Linuus if you have time and you can take a look, I would appreciate it.
I've had it deployed for a day now, and I do see less reconnects with mint thanks to the keepalive, Google still closes it from time to time, but much less.

@Linuus

Linuus commented Nov 17, 2026

Copy link
Copy Markdown

Will check tomorrow👍

stream and the receiving side finishes on its own. Returns `:ok` if the
half-close was sent, or `{:error, reason}` if the stream could not be written.
"""
@callback close_send(stream(), config()) :: :ok | {:error, term()}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

One quick feedback. This is a new, required callback so it could break old custom clients, right? Should we make it optional?

Something like
@optional_callbacks close_send: 2 + function_exported? fallback to old kill path

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I changed it to optional callback

@Linuus Linuus left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does Pub/Sub actually end the stream on half-close, or does every close fall back to cancel after 3s? Have you seen close durations of less than 3s in your deploys?

Comment thread CHANGELOG.md

- Require `grpc ~> 1.1`.
- Fix the `:mint` adapter failing to connect: it rejects unknown adapter options as of `grpc` 1.1, so it now gets its native `:keepalive` option. `:keepalive_interval_ms` applies to `:mint` too, which also closes and replaces a connection after three unanswered pings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should we also mention the half-close on shutdown/drain?


Returns `{:ok, nacked_count}` where `nacked_count` is the number of
buffered messages that were nacked and removed.
"""

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think this doc is outdated now and needs som tweaking.

:ok -> %{state | send_closed: true}
_error -> state
end
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After the half-close the keepalive timer keeps running. During a drain the stream stays open until the server ends it, so if that takes longer than keepalive_interval_ms, :send_keepalive writes to a half-closed stream. That write errors, and reset_connection closes the stream abruptly. Cancel the timer here, e.g. :ok -> %{cancel_keepalive_timer(state) | send_closed: true}.

{:stream_closed} -> :ok
after
0 -> :ok
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The reader can also end with {:stream_error, error} (stream_reader.ex:66 and :106), for example if the server answers the half-close with a non-OK status. That message isn't discarded here, so on the close path without a drain it's still in the mailbox afterwards. handle_info({:stream_error, _}, ...) then handles it as a connection loss and schedules a reconnect.
Discard both messages, e.g. add a {:stream_error, _} -> :ok clause.

@rockneurotiko

Copy link
Copy Markdown
Contributor Author

@Linuus thanks! I apply the comments.
About the half-close, I did a lot of measures on real pubsub, and it does take around 2s. But, adding the active wait in each producer makes the shutdown so slow for actual no benefits, the only benefit is that there are no warning log in mint if a message is received.

What I've made is do the half-close so we notify google pubsub that we don't want more, but do not wait. In normal work since we do the graceful shutdown the time is already be covered, and if not it's just a warning.

@Linuus

Linuus commented Nov 18, 2026

Copy link
Copy Markdown

Makes sense, thanks for measuring. Agree the wait isn't worth ~2s per producer, and the simpler version is nicer. Two things left:

  1. When the half-close doesn't happen (client without close_send/2, error, or stream still opening when the drain starts) the reader now stays alive for the whole drain, where main killed it. Can we cancel + stop the reader in that case, and half-close in {:stream_opened} when draining?
  2. The assert state.reconnect_ref == nil assertions were removed in four tests, but they still pass for me. Should we keep them?

@rockneurotiko

Copy link
Copy Markdown
Contributor Author

Makes sense, thanks for measuring. Agree the wait isn't worth ~2s per producer, and the simpler version is nicer. Two things left:

1. When the half-close doesn't happen (client without `close_send/2`, error, or stream still opening when the drain starts) the reader now stays alive for the whole drain, where main killed it. Can we cancel + stop the reader in that case, and half-close in `{:stream_opened}` when draining?

2. The `assert state.reconnect_ref == nil` assertions were removed in four tests, but they still pass for me. Should we keep them?

Good catches as always! Both addressed already

@rockneurotiko
rockneurotiko requested a review from Linuus November 18, 2026 12:42

@Linuus Linuus left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good, thanks! Approving. A few optional nits: the fallback in end_send_side could probably just call close_stream/1 (acks use the unary channel, so the stream's channel isn't needed during drain), and the graceful-shutdown wording in producer.ex (~178, ~520) still describes the old behaviour.

We now also emit the :disconnect telemetry for graceful disconnects. I don't think we did that before, but I also don't know if that matters or not though. 😅

|> stop_reader()
|> cancel_keepalive_timer()
|> Map.merge(%{reader_pid: nil, grpc_stream: nil})
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: This else can just be close_stream(state) I believe.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated all the documentation to have both half-close and the fallback, changed to close_stream(state)

For the :disconnect telemetry, I think it makes sense to keep it, it is technically disconnecting the channel

@rockneurotiko
rockneurotiko merged commit 6c64666 into main Nov 20, 2026
3 checks passed
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.

2 participants