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

ACK non processed messages when draining #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/broadway_cloud_pub_sub/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ defmodule BroadwayCloudPubSub.Options do
""",
default: :ack
],
on_draining: [
type:
{:custom, __MODULE__, :type_atom_action_or_nack_with_bounded_integer,
[[{:name, :on_success}, {:min, 0}, {:max, 600}]]},
doc: """
Configures the acking behaviour for the non consumed messages when draining.
See the "Acknowledgements" section below for all the possible values.
""",
default: {:nack, 0}
],
receive_interval: [
type: :integer,
default: @default_receive_interval,
Expand Down
20 changes: 15 additions & 5 deletions lib/broadway_cloud_pub_sub/producer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ defmodule BroadwayCloudPubSub.Producer do
def init(opts) do
receive_interval = opts[:receive_interval]
client = opts[:client]
on_draining = opts[:on_draining]

{:ok, config} = client.init(opts)
ack_ref = opts[:broadway][:name]
Expand All @@ -146,6 +147,7 @@ defmodule BroadwayCloudPubSub.Producer do
%{
demand: 0,
draining: false,
on_draining: on_draining,
receive_timer: nil,
receive_interval: receive_interval,
client: {client, config},
Expand Down Expand Up @@ -205,6 +207,18 @@ defmodule BroadwayCloudPubSub.Producer do
handle_receive_messages(%{state | receive_timer: nil})
end

def handle_info(
{ref, messages},
%{draining: true, on_draining: on_draining, worker_task: %{ref: ref}} = state
) do
# Drain messages, not acknowledging them and setting the ack to draining_deadline
messages
|> Enum.map(&Broadway.Message.configure_ack(&1, on_success: on_draining))
|> Broadway.Message.ack_immediately()

{:noreply, [], %{state | worker_task: nil}}
end

def handle_info({ref, messages}, %{demand: demand, worker_task: %{ref: ref}} = state) do
new_demand = demand - length(messages)

Expand All @@ -231,11 +245,7 @@ defmodule BroadwayCloudPubSub.Producer do

@impl Producer
def prepare_for_draining(state) do
if state.worker_task do
Task.shutdown(state.worker_task, :brutal_kill)
end

{:noreply, [], %{state | worker_task: nil, draining: true}}
{:noreply, [], %{state | draining: true}}
end

defp handle_receive_messages(%{draining: true} = state) do
Expand Down
Loading