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

fix: add additional restrictions when using batching #72

Merged
merged 2 commits into from
Nov 27, 2023
Merged
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
14 changes: 14 additions & 0 deletions lib/inngest/function/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ defmodule Inngest.FnOpts do
Validate the event batch settings
"""
@spec validate_batch_events(t(), map()) :: map()
# credo:disable-for-next-line
def validate_batch_events(fnopts, config) do
case fnopts |> Map.get(:batch_events) do
nil ->
Expand All @@ -115,6 +116,19 @@ defmodule Inngest.FnOpts do
message: "'max_size' and 'timeout' must be set for batch_events"
end

rate_limit = Map.get(fnopts, :rate_limit)
cancel_on = Map.get(fnopts, :cancel_on)

if !is_nil(rate_limit) do
raise Inngest.BatchEventConfigError,
message: "'rate_limit' cannot be used with event_batches"
end

if !is_nil(cancel_on) do
raise Inngest.BatchEventConfigError,
message: "'cancel_on' cannot be used with event_batches"
end

case Util.parse_duration(timeout) do
{:error, error} ->
raise Inngest.BatchEventConfigError, message: error
Expand Down
20 changes: 20 additions & 0 deletions test/inngest/function/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ defmodule Inngest.FnOptsTest do
FnOpts.validate_batch_events(opts, @config)
end
end

test "should raise if rate limit is set with batching" do
opts = Map.put(@fn_opts, :rate_limit, %{limit: 1, period: "10m"})

assert_raise Inngest.BatchEventConfigError,
"'rate_limit' cannot be used with event_batches",
fn ->
FnOpts.validate_batch_events(opts, @config)
end
end

test "should raise if cancel_on is set with batching" do
opts = Map.put(@fn_opts, :cancel_on, %{event: "hello"})

assert_raise Inngest.BatchEventConfigError,
"'cancel_on' cannot be used with event_batches",
fn ->
FnOpts.validate_batch_events(opts, @config)
end
end
end

describe "validate_rate_limit/2" do
Expand Down