Skip to content
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

Access proxy timeout #654

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 62 additions & 23 deletions apps/sim/lib/sim/realm/access_proxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,65 +27,99 @@ defmodule Sim.AccessProxy do
45 = Sim.AccessProxy.get()
"""

# milliseconds
@max_duration 5_000

def start_link(opts) do
GenServer.start_link(__MODULE__, Keyword.delete(opts, :name), name: opts[:name] || __MODULE__)
end

def get(func \\ & &1, server \\ __MODULE__) do
def get(server \\ __MODULE__, func \\ & &1) do
GenServer.call(server, {:get, func})
end

def exclusive_get(func \\ & &1, server \\ __MODULE__) do
def exclusive_get(server \\ __MODULE__, func \\ & &1) do
GenServer.call(server, {:exclusive_get, func})
end

def update(data, server \\ __MODULE__)
def update(server \\ __MODULE__, data)

def update(func, server) when is_function(func) do
def update(server, func) when is_function(func) do
GenServer.call(server, {:update, func})
end

def update(data, server) do
def update(server, data) do
GenServer.call(server, {:update, fn _ -> data end})
end

def init(agent: agent) do
{:ok, %{caller: nil, agent: agent, requests: []}}
def init(opts) do
{:ok,
%{
caller: nil,
agent: opts[:agent],
requests: [],
max_duration: opts[:max_duration] || @max_duration
}}
end

def handle_call({:get, func}, _from, state) do
{:reply, get_data(state.agent, func), state}
end

def handle_call({:exclusive_get, func}, {pid, _}, %{caller: nil} = state) do
ref = Process.monitor(pid)
{:reply, get_data(state.agent, func), %{state | caller: {pid, ref}}}
def handle_call({:exclusive_get, func}, {pid, _} = from, %{caller: nil} = state) do
monitor_ref = Process.monitor(pid)
start_check_timeout(from, monitor_ref, state.max_duration)
{:reply, get_data(state.agent, func), %{state | caller: {pid, monitor_ref}}}
end

def handle_call({:exclusive_get, func}, {pid, _}, %{caller: {pid, _}} = state) do
{:reply, get_data(state.agent, func), state}
end

def handle_call({:exclusive_get, func}, {pid, _} = from, state) do
ref = Process.monitor(pid)
{:noreply, %{state | requests: state.requests ++ [{from, ref, func}]}}
def handle_call({:exclusive_get, func}, {pid, _} = from, %{caller: caller} = state) do
monitor_ref = Process.monitor(pid)
{:noreply, %{state | requests: state.requests ++ [{from, monitor_ref, func}]}}
end

def handle_call({:update, func}, {pid, _}, %{caller: {pid, ref}, requests: []} = state) do
def handle_call({:update, func}, {pid, _}, %{caller: {pid, monitor_ref}, requests: []} = state) do
update_data(state.agent, func)
Process.demonitor(ref, [:flush])
Process.demonitor(monitor_ref, [:flush])
{:reply, :ok, %{state | caller: nil}}
end

def handle_call({:update, func}, {pid, _}, %{caller: {pid, ref}} = state) do
def handle_call({:update, func}, {pid, _}, %{caller: {pid, monitor_ref}} = state) do
update_data(state.agent, func)
Process.demonitor(ref, [:flush])
{next_caller, requests} = reply_to_next_caller(state)
{:reply, :ok, %{state | caller: next_caller, requests: requests}}
Process.demonitor(monitor_ref, [:flush])
{next_caller, state} = reply_to_next_caller(state)
{:reply, :ok, %{state | caller: next_caller}}
end

def handle_call({:update, _func}, _from, state) do
{:reply, {:error, "request the data first with AccessProxy#exclusive_get"}, state}
{:reply,
{:error,
"request the data first with AccessProxy#exclusive_get or maybe too much time elapsed since exclusive_get was called"},
state}
end

def handle_info(
{:check_timeout, {pid, _ref}, monitor_ref},
%{caller: {pid, monitor_ref}, requests: []} = state
) do
Process.demonitor(monitor_ref, [:flush])
{:noreply, %{state | caller: nil}}
end

def handle_info(
{:check_timeout, {pid, _ref}, monitor_ref},
%{caller: {pid, monitor_ref}} = state
) do
Process.demonitor(monitor_ref, [:flush])
{next_caller, state} = reply_to_next_caller(state)
{:noreply, %{state | caller: next_caller}}
end

def handle_info({:check_timeout, _}, state) do
{:noreply, state}
end

def handle_info({:DOWN, _ref, :process, _pid, _reason}, %{caller: nil} = state) do
Expand All @@ -100,8 +134,8 @@ defmodule Sim.AccessProxy do
end

def handle_info({:DOWN, _ref, :process, pid, _reason}, %{caller: {pid, _}} = state) do
{next_caller, requests} = reply_to_next_caller(state)
{:noreply, %{state | caller: next_caller, requests: requests}}
{next_caller, state} = reply_to_next_caller(state)
{:noreply, %{state | caller: next_caller}}
end

def handle_info({:DOWN, _ref, :process, pid, _reason}, state) do
Expand All @@ -124,6 +158,11 @@ defmodule Sim.AccessProxy do
defp reply_to_next_caller(state) do
[{{pid, _ref} = next_caller, monitor_ref, get_func} | requests] = state.requests
:ok = GenServer.reply(next_caller, get_data(state.agent, get_func))
{{pid, monitor_ref}, requests}
start_check_timeout(next_caller, monitor_ref, state.max_duration)
{{pid, monitor_ref}, %{state | requests: requests}}
end

defp start_check_timeout(current_caller, monitor_ref, max_duration) do
Process.send_after(self(), {:check_timeout, current_caller, monitor_ref}, max_duration)
end
end
71 changes: 49 additions & 22 deletions apps/sim/test/sim/realm/access_process_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,43 @@ defmodule Sim.AccessProxyTest do
end

describe "exclusive_get" do
test "are executed in sequence" do
test "are executed in sequence", %{proxy: proxy} do
1..3
|> Enum.map(fn _n ->
Task.async(fn ->
value = AccessProxy.exclusive_get()
value = AccessProxy.exclusive_get(proxy)
Process.sleep(100)
:ok = AccessProxy.update(value + 1)
:ok = AccessProxy.update(proxy, value + 1)
end)
end)
|> Task.await_many()

assert 45 == AccessProxy.get()
end

test "allow update only after exclusive_get" do
value = AccessProxy.get()
assert {:error, _} = AccessProxy.update(value + 1)
assert 42 == AccessProxy.get()
test "allow update only after exclusive_get", %{proxy: proxy} do
value = AccessProxy.get(proxy)
assert {:error, _} = AccessProxy.update(proxy, value + 1)
assert 42 == AccessProxy.get(proxy)
end

test "get never blocks" do
test "get never blocks", %{proxy: proxy} do
result =
[
Task.async(fn ->
value = AccessProxy.exclusive_get()
value = AccessProxy.exclusive_get(proxy)
Process.sleep(100)
:ok = AccessProxy.update(value + 1)
:ok = AccessProxy.update(proxy, value + 1)
AccessProxy.get()
end),
Task.async(fn ->
Process.sleep(10)
AccessProxy.get()
end),
Task.async(fn ->
value = AccessProxy.exclusive_get()
value = AccessProxy.exclusive_get(proxy)
Process.sleep(100)
:ok = AccessProxy.update(value + 1)
:ok = AccessProxy.update(proxy, value + 1)
AccessProxy.get()
end)
]
Expand All @@ -57,18 +57,18 @@ defmodule Sim.AccessProxyTest do
assert [43, 42, 44] = result
end

test "remove lock if current client crashes", %{supervisor: supervisor} do
test "remove lock if current client crashes", %{proxy: proxy, supervisor: supervisor} do
result =
[
Task.Supervisor.async_stream_nolink(supervisor, [1], fn _n ->
_value = AccessProxy.exclusive_get()
_value = AccessProxy.exclusive_get(proxy)
Process.sleep(10)
Process.exit(self(), :upps)
end),
Task.Supervisor.async_stream_nolink(supervisor, [2], fn _n ->
value = AccessProxy.exclusive_get()
value = AccessProxy.exclusive_get(proxy)
Process.sleep(100)
:ok = AccessProxy.update(value + 1)
:ok = AccessProxy.update(proxy, value + 1)
AccessProxy.get()
end)
]
Expand All @@ -78,24 +78,24 @@ defmodule Sim.AccessProxyTest do
assert [exit: :upps, ok: 43] = result
end

test "remove from requests if queued client crashes", %{supervisor: supervisor} do
test "remove from requests if queued client crashes", %{proxy: proxy, supervisor: supervisor} do
result =
[
Task.Supervisor.async_stream_nolink(supervisor, [2], fn _n ->
value = AccessProxy.exclusive_get()
value = AccessProxy.exclusive_get(proxy)
Process.sleep(100)
:ok = AccessProxy.update(value + 1)
:ok = AccessProxy.update(proxy, value + 1)
AccessProxy.get()
end),
Task.Supervisor.async_stream_nolink(supervisor, [1], fn _n ->
_value = AccessProxy.exclusive_get()
_value = AccessProxy.exclusive_get(proxy)
Process.sleep(10)
Process.exit(self(), :upps)
end),
Task.Supervisor.async_stream_nolink(supervisor, [2], fn _n ->
value = AccessProxy.exclusive_get()
value = AccessProxy.exclusive_get(proxy)
Process.sleep(100)
:ok = AccessProxy.update(value + 1)
:ok = AccessProxy.update(proxy, value + 1)
AccessProxy.get()
end)
]
Expand All @@ -104,5 +104,32 @@ defmodule Sim.AccessProxyTest do

assert [ok: 43, exit: :upps, ok: 44] = result
end

test "timeouted request should not be able to update", %{agent: agent} do
proxy =
start_supervised!(
{AccessProxy, [name: :fast_access, agent: agent, max_duration: 50]},
id: :fast_access
)

[
Task.async(fn ->
value = AccessProxy.exclusive_get(proxy)
Process.sleep(100)
{:error, _msg} = AccessProxy.update(proxy, value + 10)
end),
Task.async(fn ->
value = AccessProxy.exclusive_get(proxy)
:ok = AccessProxy.update(proxy, value + 1)
end),
Task.async(fn ->
value = AccessProxy.exclusive_get(proxy)
:ok = AccessProxy.update(proxy, value + 1)
end)
]
|> Task.await_many()

assert 44 == AccessProxy.get()
end
end
end
Loading