Skip to content

Commit

Permalink
reorder producer func chain
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino committed Nov 11, 2022
1 parent be68183 commit 12eda16
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
5 changes: 5 additions & 0 deletions apps/worker/lib/worker/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ defmodule Worker.Base do
end

def empty(state), do: {:noreply, [], state}

def drain(state), do: %{state | draining: FT.K8S.TrafficDrainHandler.draining?()}

def demand(%{} = state, d, v) when is_integer(v), do: %{state | demand: d - v}
def demand(%{} = state, d, l) when is_list(l), do: %{state | demand: d - length(l)}
end
13 changes: 4 additions & 9 deletions apps/worker/lib/worker/poll_producer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ defmodule Worker.PollProducer do

defmodule State, do: defstruct unquote(state_keys)

def handle_info(:poll, %State{draining: true} = state), do: empty(state) |> drain()
def handle_info(:poll, %State{demand: 0} = state), do: empty(state) |> drain()
def handle_info(:poll, %State{draining: true} = state), do: drain(state) |> empty()
def handle_info(:poll, %State{demand: 0} = state), do: drain(state) |> empty()
def handle_info(:poll, %State{demand: demand} = state),
do: deliver(demand, drain(state))

def handle_demand(_, %State{draining: true} = state), do: empty(state) |> drain()
def handle_demand(_, %State{draining: true} = state), do: drain(state) |> empty()
def handle_demand(demand, %State{demand: remaining} = state) when demand > 0,
do: deliver(demand + remaining, drain(state))

def handle_demand(_, state), do: empty(state) |> drain()

defp drain(state), do: %{state | draining: FT.K8S.TrafficDrainHandler.draining?()}

defp demand(state, d, v) when is_integer(v), do: %{state | demand: d - v}
defp demand(state, d, l) when is_list(l), do: %{state | demand: d - length(l)}
def handle_demand(_, state), do: drain(state) |> empty()
end
end
end

0 comments on commit 12eda16

Please sign in to comment.