Skip to content
Open
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
9 changes: 3 additions & 6 deletions lib/hyper/firecracker/api/transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ defmodule Hyper.Firecracker.Api.Transport do
@max_line_bytes Information.as_bytes(Information.kib(8))
@max_headers 100

# One deadline for the whole call, below `Hyper.Node.FireVMM.Client`'s 35 s
# call cap, so a daemon drip-feeding bytes cannot keep the client busy.
@call_budget_ms Unit.Time.as_ms(Unit.Time.s(30))

@type result ::
:ok
| {:ok, term()}
Expand All @@ -49,7 +45,8 @@ defmodule Hyper.Firecracker.Api.Transport do
@spec request(map()) :: result()
@decorate with_span("Hyper.Firecracker.Api.Transport.request", include: [:method, :url])
def request(%{method: method, url: url, opts: opts} = info) do
deadline = System.monotonic_time(:millisecond) + @call_budget_ms
deadline =
System.monotonic_time(:millisecond) + Unit.Time.as_ms(Keyword.fetch!(opts, :budget))

case call(Keyword.fetch!(opts, :socket_path), encode(method, url, info), deadline) do
{:ok, status, headers, body} ->
Expand Down Expand Up @@ -92,7 +89,7 @@ defmodule Hyper.Firecracker.Api.Transport do
end

defp call(socket_path, request, deadline) do
opts = [:binary, active: false, packet: :raw, send_timeout: @call_budget_ms]
opts = [:binary, active: false, packet: :raw, send_timeout: remaining(deadline)]

with {:ok, sock} <- :gen_tcp.connect({:local, socket_path}, 0, opts, remaining(deadline)) do
try do
Expand Down
6 changes: 5 additions & 1 deletion lib/hyper/node/fire_vmm/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ defmodule Hyper.Node.FireVMM.Client do
# Cap on a single Firecracker API call.
@call_timeout :timer.seconds(35)

# One deadline for the whole transport call, below `@call_timeout`, so a
# daemon drip-feeding bytes cannot keep the client busy.
@transport_budget Unit.Time.s(30)

@doc "Run a generated operation against this VM's daemon, serialized."
@spec run(GenServer.server(), (keyword() -> result)) :: result when result: var
def run(server, op_fun) when is_function(op_fun, 1) do
Expand Down Expand Up @@ -98,7 +102,7 @@ defmodule Hyper.Node.FireVMM.Client do

@impl true
def handle_call({:run, op_fun}, _from, %State{socket_path: socket_path} = state) do
{:reply, op_fun.(socket_path: socket_path), state}
{:reply, op_fun.(socket_path: socket_path, budget: @transport_budget), state}
end

@spec gen_opts(GenServer.name() | nil) :: [{:name, GenServer.name()}]
Expand Down
27 changes: 18 additions & 9 deletions test/hyper/firecracker/api/transport_cap_properties_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Hyper.Firecracker.Api.TransportCapPropertiesTest do
alias Hyper.Firecracker.Api.{InstanceInfo, Transport}

@cap Unit.Information.as_bytes(Unit.Information.mib(1))
@client_call_timeout :timer.seconds(35)
@budget Unit.Time.s(30)

defmodule RawDaemon do
@moduledoc false
Expand Down Expand Up @@ -113,7 +113,10 @@ defmodule Hyper.Firecracker.Api.TransportCapPropertiesTest do
end

defp request(path, extra \\ []) do
Map.merge(%{method: :get, url: "/", opts: [socket_path: path]}, Map.new(extra))
Map.merge(
%{method: :get, url: "/", opts: [socket_path: path, budget: @budget]},
Map.new(extra)
)
end

defp json_of_size(n) do
Expand Down Expand Up @@ -296,8 +299,9 @@ defmodule Hyper.Firecracker.Api.TransportCapPropertiesTest do
Transport.request(request(path, response: [{200, {InstanceInfo, :t}}]))
end

@tag timeout: 120_000
test "a drip-fed reply is cut off before Client.run/2's call timeout" do
test "a drip-fed reply is cut off at the call's budget" do
budget = Unit.Time.s(1)

path =
RawDaemon.start(fn sock, _n ->
_ =
Expand All @@ -307,18 +311,23 @@ defmodule Hyper.Firecracker.Api.TransportCapPropertiesTest do
)

Stream.repeatedly(fn ->
Process.sleep(2_000)
Process.sleep(100)
:gen_tcp.send(sock, " ")
end)
|> Enum.find(&match?({:error, _}, &1))

:close
end)

task = Task.async(fn -> Transport.request(request(path)) end)
got = Task.yield(task, @client_call_timeout) || Task.shutdown(task, :brutal_kill)
task =
Task.async(fn ->
Transport.request(%{method: :get, url: "/", opts: [socket_path: path, budget: budget]})
end)

deadline_ms = Unit.Time.as_ms(budget) + 1_000
got = Task.yield(task, deadline_ms) || Task.shutdown(task, :brutal_kill)

assert match?({:ok, {:error, _}}, got),
"Transport.request still running after #{@client_call_timeout} ms: #{inspect(got)}"
assert got == {:ok, {:error, {:transport, :timeout}}},
"Transport.request after #{deadline_ms} ms: #{inspect(got)}"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ defmodule Hyper.Firecracker.Api.TransportHostileHeadersTest do

task =
Task.async(fn ->
Transport.request(%{method: :get, url: "/", opts: [socket_path: path]})
Transport.request(%{
method: :get,
url: "/",
opts: [socket_path: path, budget: Unit.Time.s(30)]
})
end)

result =
Expand Down
7 changes: 6 additions & 1 deletion test/hyper/firecracker/api/transport_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ defmodule Hyper.Firecracker.Api.TransportTest do
:ok
end

@budget Unit.Time.s(30)

defp request(socket_path, extra) do
Map.merge(%{method: :get, url: "/", opts: [socket_path: socket_path]}, Map.new(extra))
Map.merge(
%{method: :get, url: "/", opts: [socket_path: socket_path, budget: @budget]},
Map.new(extra)
)
end

test "204 against a :null response spec is :ok" do
Expand Down
Loading