Skip to content

Commit

Permalink
fix: add additional restrictions when using batching (#72)
Browse files Browse the repository at this point in the history
cancellation and rate limit can't be used with event batching, so make
sure to add those validation as well.

---------

Co-authored-by: Darwin D Wu <darwin67@users.noreply.github.com>
  • Loading branch information
darwin67 and darwin67 authored Nov 27, 2023
1 parent 427374b commit 54e4755
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/inngest/function/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,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 @@ -140,6 +141,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 @@ -145,6 +145,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

0 comments on commit 54e4755

Please sign in to comment.