-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat!: update producer to match consumer style #70
Changes from all commits
74f84d4
1ee3701
6097666
7cfc994
9fb384b
940b718
22c919e
0233c21
059870c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"skip_files": [ | ||
"test/support" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Kafee do | ||
@moduledoc """ | ||
Kafee is an abstraction layer above multiple different lower level Kafka libraries, while also adding features relevant to Stord. This allows switching between `:brod` or `Broadway` for message consuming with a quick configuration change and no code changes. Features include: | ||
|
||
- Behaviour based adapters allowing quick low level changes. | ||
- Built in support for testing without mocking. | ||
- Automatic encoding and decoding of message values with `Jason` or `Protobuf`. | ||
- `:telemetry` metrics for producing and consuming messages. | ||
- Open Telemetry traces with correct attributes. | ||
- DataDog data streams support via `data-streams-ex`. | ||
""" | ||
|
||
@typedoc "A Kafka message key." | ||
@type key :: binary() | ||
|
||
@typedoc "A Kafka message value encoded." | ||
@type value :: binary() | ||
|
||
@typedoc "A Kafka partition." | ||
@type topic :: String.t() | ||
|
||
@typedoc "Any valid Kafka partition." | ||
@type partition :: -2_147_483_648..2_147_483_647 | ||
|
||
@typedoc "A function to assign a partition to a message." | ||
@type partition_fun :: :hash | :random | (topic(), [partition()], key(), value() -> partition()) | ||
|
||
@typedoc "A valid Kafka offset for a message." | ||
@type offset :: -9_223_372_036_854_775_808..9_223_372_036_854_775_807 | ||
|
||
@typedoc "A group id for consumers in a Kafka cluster." | ||
@type consumer_group_id :: binary() | ||
|
||
@typedoc "A list of Kafka message headers." | ||
@type headers :: [{binary(), binary()}] | ||
|
||
@doc """ | ||
Checks if the valid is a valid Kafka partition. | ||
""" | ||
defguard is_partition(number) when number in -2_147_483_648..2_147_483_647 | ||
|
||
@doc """ | ||
Checks if a value is a valid Kafka offset. | ||
""" | ||
defguard is_offset(number) when number in -9_223_372_036_854_775_808..9_223_372_036_854_775_807 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,12 @@ defmodule Kafee.Application do | |
@doc false | ||
@spec start(Application.start_type(), term()) :: {:ok, pid} | {:error, term()} | ||
def start(_type, _args) do | ||
:ets.new(:kafee_config, [:public, :set, :named_table, {:read_concurrency, true}]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, currently the configuration is held in |
||
|
||
children = [ | ||
{Registry, keys: :unique, name: Kafee.Producer.AsyncRegistry} | ||
{Registry, keys: :unique, name: Kafee.Registry} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed this to just the Kafee registry because it can be used for other things (although I've tried to avoid it and it's only used for the dynamic async workers.) |
||
] | ||
|
||
Supervisor.start_link(children, strategy: :one_for_one) | ||
Supervisor.start_link(children, name: Kafee.Application, strategy: :one_for_one) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ defmodule Kafee.Consumer do | |
Kafee has built in support for Jason and Protobuf encoding and decoding. See | ||
individual encoder decoder modules for more options. | ||
""", | ||
type: {:or, [nil, :mod_arg]} | ||
type: {:or, [nil, :atom, :mod_arg]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used to be |
||
], | ||
host: [ | ||
default: "localhost", | ||
|
@@ -189,8 +189,6 @@ defmodule Kafee.Consumer do | |
@doc "Handles an error while processing a Kafka message" | ||
@callback handle_failure(any(), Kafee.Consumer.Message.t()) :: :ok | ||
|
||
@optional_callbacks handle_failure: 2 | ||
|
||
@doc false | ||
defmacro __using__(opts \\ []) do | ||
quote location: :keep, bind_quoted: [opts: opts, module: __CALLER__.module] do | ||
|
@@ -254,12 +252,12 @@ defmodule Kafee.Consumer do | |
for starting the whole process tree. | ||
""" | ||
@spec start_link(module(), options()) :: Supervisor.on_start() | ||
def start_link(module, options) do | ||
def start_link(consumer, options) do | ||
with {:ok, options} <- NimbleOptions.validate(options, @options_schema) do | ||
case options[:adapter] do | ||
nil -> :ignore | ||
adapter when is_atom(adapter) -> adapter.start_link(module, options) | ||
{adapter, _adapter_options} -> adapter.start_link(module, options) | ||
adapter when is_atom(adapter) -> adapter.start_link(consumer, options) | ||
{adapter, __options} -> adapter.start_link(consumer, options) | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oddly enough dialyzer was complaining about using
is_integer
for a guard, so I made this one instead. Seems to pass. I'm assuming this has to do with how large the number is and not technically being an integer low level or something. Haven't dug into why.