Skip to content

Commit 305d7ba

Browse files
committed
Basic rate limited retries
1 parent c136736 commit 305d7ba

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/flame/fly_backend.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ defmodule FLAME.FlyBackend do
135135
runner_node_name: nil,
136136
log: nil
137137

138+
@retry 10
139+
138140
@valid_opts [
139141
:app,
140142
:region,
@@ -253,7 +255,7 @@ defmodule FLAME.FlyBackend do
253255
def remote_boot(%FlyBackend{parent_ref: parent_ref} = state) do
254256
{resp, req_connect_time} =
255257
with_elapsed_ms(fn ->
256-
http_post!("#{state.host}/v1/apps/#{state.app}/machines",
258+
http_post!("#{state.host}/v1/apps/#{state.app}/machines", @retry,
257259
content_type: "application/json",
258260
headers: [
259261
{"Content-Type", "application/json"},
@@ -333,7 +335,7 @@ defmodule FLAME.FlyBackend do
333335
|> binary_part(0, len)
334336
end
335337

336-
defp http_post!(url, opts) do
338+
defp http_post!(url, remaining_tries, opts) do
337339
Keyword.validate!(opts, [:headers, :body, :connect_timeout, :content_type])
338340

339341
headers =
@@ -362,6 +364,10 @@ defmodule FLAME.FlyBackend do
362364
{:ok, {{_, 200, _}, _, response_body}} ->
363365
JSON.decode!(response_body)
364366

367+
{:ok, {{_, 429, _}, _, _response_body}} when remaining_tries > 0 ->
368+
Process.sleep(1000)
369+
http_post!(url, remaining_tries - 1, opts)
370+
365371
{:ok, {{_, status, reason}, _, resp_body}} ->
366372
raise "failed POST #{url} with #{inspect(status)} (#{inspect(reason)}): #{inspect(resp_body)} #{inspect(headers)}"
367373

0 commit comments

Comments
 (0)