Skip to content

Commit

Permalink
Merge branch 'master' into doc/add_description_to_readme
Browse files Browse the repository at this point in the history
  • Loading branch information
escobera authored Jun 18, 2024
2 parents 3a2a294 + 2622f25 commit af76b71
Show file tree
Hide file tree
Showing 29 changed files with 546 additions and 248 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ klife-*.tar

# Temporary files, for example, from tests.
/tmp/


example/*
4 changes: 4 additions & 0 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:ok = Klife.Utils.create_topics()

opts = [strategy: :one_for_one, name: Test.Supervisor]
{:ok, _} = Supervisor.start_link([MyClient], opts)
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ It leverages the [klife_protocol](https://github.com/oliveigah/klife_protocol) i
- SASL

- Producer System
- Rename client to client
- Add default producer and partition as client option
- Implement test helper functions (assert_produced)
- Improve input errors handling
- Accept more versions of the protocol
Expand Down
6 changes: 4 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Config

config :klife, MyTestClient,
config :klife, MyClient,
connection: [
bootstrap_servers: ["localhost:19092", "localhost:29092"],
# bootstrap_servers: ["localhost:19093", "localhost:29093"],
Expand Down Expand Up @@ -83,7 +83,9 @@ config :klife, MyTestClient,
[
name: "test_async_topic"
],
[name: "my_topic"]
[name: "my_topic_1"],
[name: "my_topic_2"],
[name: "my_topic_3"]
]

if config_env() == :dev do
Expand Down
4 changes: 4 additions & 0 deletions example/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 26 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
example-*.tar

# Temporary files, for example, from tests.
/tmp/
3 changes: 3 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Example

This is just an example project to show some Klife features and configurations.
8 changes: 8 additions & 0 deletions example/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Config

config :example, Example.MySimplestClient,
connection: [
bootstrap_servers: ["localhost:19092", "localhost:29092"],
ssl: false
],
topics: [[name: "my_topic_1"]]
6 changes: 6 additions & 0 deletions example/lib/example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule Example do
def produce(client, rec, opts \\ []), do: client.produce(rec, opts)
def produce_batch(client, recs, opts \\ []), do: client.produce_batch(recs, opts)
def produce_batch_txn(client, recs, opts \\ []), do: client.produce_batch_txn(recs, opts)
def transaction(client, fun, opts \\ []), do: client.transaction(fun, opts)
end
19 changes: 19 additions & 0 deletions example/lib/example/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Example.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false

use Application

@impl true
def start(_type, _args) do
children = [
Example.MySimplestClient
]

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Example.Supervisor]
Supervisor.start_link(children, opts)
end
end
3 changes: 3 additions & 0 deletions example/lib/example/clients/my_simplest_client.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Example.MySimplestClient do
use Klife.Client, otp_app: :example
end
26 changes: 26 additions & 0 deletions example/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Example.MixProject do
use Mix.Project

def project do
[
app: :example,
version: "0.1.0",
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

def application do
[
extra_applications: [:logger],
mod: {Example.Application, []}
]
end

defp deps do
[
{:klife, path: "../"}
]
end
end
7 changes: 7 additions & 0 deletions example/mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%{
"crc32cer": {:hex, :crc32cer, "0.1.10", "fb87abbf34b72f180f8c3a908cd1826c6cb9a59787d156a29e05de9e98be385e", [:rebar3], [], "hexpm", "5b1f47efd0a1b4b7411f1f35e14d3c8c6da6e6a2a725ec8f2cf1ab13703e5f38"},
"klife_protocol": {:hex, :klife_protocol, "0.5.0", "08278e7f065a11f87484911fdbb5ec29c67208ab9a4030f90babf2e89e91cb3c", [:mix], [{:crc32cer, "~> 0.1.8", [hex: :crc32cer, repo: "hexpm", optional: false]}, {:snappyer, "~> 1.2.7", [hex: :snappyer, repo: "hexpm", optional: false]}], "hexpm", "d448e56edc498b5bdee5fb7decdaa5c26eaa29b88e045bc9d7fd3c103bc06021"},
"nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
"snappyer": {:hex, :snappyer, "1.2.9", "9cc58470798648ce34c662ca0aa6daae31367667714c9a543384430a3586e5d3", [:rebar3], [], "hexpm", "18d00ca218ae613416e6eecafe1078db86342a66f86277bd45c95f05bf1c8b29"},
}
14 changes: 14 additions & 0 deletions example/test/example_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule ExampleTest do
use ExUnit.Case
alias Klife.Record
alias Klife.Test, as: KlifeTest

test "simplest client" do
client = Example.MySimplestClient

rec = %Record{value: :rand.bytes(10), topic: "my_topic_1"}
{:ok, %Record{partition: partition, offset: offset}} = Example.produce(client, rec)

KlifeTest.assert_offset(client, rec, offset, partition: partition)
end
end
1 change: 1 addition & 0 deletions example/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()
34 changes: 32 additions & 2 deletions lib/klife.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@ defmodule Klife do
alias Klife.TxnProducerPool
alias Klife.Producer.Controller, as: PController

@produce_opts [
producer: [
type: :atom,
required: false,
doc:
"Producer's name that will override the `default_producer` configuration. Ignored inside transactions."
],
async: [
type: :boolean,
required: false,
default: false,
doc:
"Makes the produce asynchronous. When `true` the return value will be `:ok`. Ignored inside transactions."
],
partitioner: [
type: :atom,
required: false,
doc: "Module that will override `default_partitioner` configuration."
]
]

@txn_opts [
pool_name: [
type: :atom,
required: false,
doc: "Txn pool's name that will override the `default_txn_pool` configuration."
]
]

def get_produce_opts(), do: @produce_opts
def get_txn_opts(), do: @txn_opts

def produce(%Record{} = record, client, opts \\ []) do
case produce_batch([record], client, opts) do
[resp] -> resp
Expand Down Expand Up @@ -46,8 +78,6 @@ defmodule Klife do
TxnProducerPool.run_txn(client, get_txn_pool(client, opts), fun)
end

def in_txn?(client), do: TxnProducerPool.in_txn?(client)

defp get_txn_pool(client, opts) do
case Keyword.get(opts, :pool_name) do
nil -> apply(client, :get_default_txn_pool, [])
Expand Down
Loading

0 comments on commit af76b71

Please sign in to comment.