-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "fix: BufferCounter removal and considation to BufferProducer"
This reverts commit f88421f.
- Loading branch information
Showing
22 changed files
with
565 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.6.6 | ||
1.6.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Logflare.Buffers.Buffer do | ||
@moduledoc """ | ||
Defines a behaviour for a buffer. | ||
""" | ||
|
||
@doc """ | ||
Adds a list of payloads to the buffer. | ||
""" | ||
@callback add_many(identifier(), payloads :: [term()]) :: :ok | ||
|
||
@doc """ | ||
Clears the buffer and removes all enqueued items. | ||
""" | ||
@callback clear(identifier()) :: :ok | ||
|
||
@doc """ | ||
Returns the length of the buffer | ||
""" | ||
@callback length(identifier()) :: non_neg_integer() | ||
|
||
@doc """ | ||
Returns multiple items from the buffer | ||
""" | ||
@callback pop_many(identifier(), non_neg_integer()) :: [term()] | ||
|
||
@doc """ | ||
Adds payload to the buffer. | ||
""" | ||
@spec add(module(), identifier(), term()) :: :ok | ||
def add(mod, ident, payload), | ||
do: mod.add_many(ident, [payload]) | ||
|
||
@doc """ | ||
Adds a list of payloads to the buffer. | ||
""" | ||
@spec add_many(module(), identifier(), [term()]) :: :ok | ||
def add_many(mod, ident, payloads) when is_list(payloads), | ||
do: mod.add_many(ident, payloads) | ||
|
||
@doc """ | ||
Clears the buffer and removes all enqueued items. | ||
""" | ||
@spec clear(module(), identifier()) :: :ok | ||
def clear(mod, ident), do: mod.clear(ident) | ||
|
||
@doc """ | ||
Returns the length of the buffer | ||
""" | ||
@spec length(module(), identifier()) :: non_neg_integer() | ||
def length(mod, ident), do: mod.length(ident) | ||
|
||
@doc """ | ||
Returns single item from the buffer | ||
""" | ||
@spec pop(module(), identifier()) :: term() | ||
def pop(mod, ident), do: mod.pop_many(ident, 1) | ||
|
||
@doc """ | ||
Returns multiple items from the buffer | ||
""" | ||
@spec pop_many(module(), identifier(), non_neg_integer()) :: [term()] | ||
def pop_many(mod, ident, count) when is_integer(count) and count > 0, | ||
do: mod.pop_many(ident, count) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Logflare.Buffers.BufferProducer do | ||
@moduledoc """ | ||
A generic producer that acts as a producer but doesn't actually produce anything. | ||
""" | ||
use GenStage | ||
|
||
def start_link(opts) do | ||
GenStage.start_link(__MODULE__, opts) | ||
end | ||
|
||
def init(opts) do | ||
state = Enum.into(opts, %{buffer_module: nil, buffer_pid: nil, demand: 0}) | ||
{:producer, state} | ||
end | ||
|
||
def handle_info(:resolve, state) do | ||
{items, state} = resolve_demand(state) | ||
{:noreply, items, state} | ||
end | ||
|
||
def handle_demand(demand, state) do | ||
{items, state} = resolve_demand(state, demand) | ||
{:noreply, items, state} | ||
end | ||
|
||
defp resolve_demand( | ||
%{demand: prev_demand} = state, | ||
new_demand \\ 0 | ||
) do | ||
total_demand = prev_demand + new_demand | ||
{[], %{state | demand: total_demand}} | ||
end | ||
end |
Oops, something went wrong.