Skip to content

Commit

Permalink
[Serve] Minor cleaning up of the backoff sequence in HTTPProxy (ray…
Browse files Browse the repository at this point in the history
…-project#37900)

- Added cap to backoff period
 - Tidying up a bit
  • Loading branch information
alexeykudinkin authored Jul 31, 2023
1 parent 6f88b0c commit 3e6fc0d
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions python/ray/serve/_private/http_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
)


INITIAL_BACKOFF_PERIOD_SEC = 0.05
MAX_BACKOFF_PERIOD_SEC = 5
BACKOFF_FACTOR = 2


class LongestPrefixRouter:
"""Router that performs longest prefix matches on incoming routes."""

Expand Down Expand Up @@ -588,12 +593,11 @@ async def send_request_to_replica_unary(
request = pickle.dumps(request)

retries = 0
backoff_time_s = 0.05
backoff = False
loop = get_or_create_event_loop()
# We have received all the http request conent. The next `receive`
# call might never arrive; if it does, it can only be `http.disconnect`.
while retries < HTTP_REQUEST_MAX_RETRIES + 1:
should_backoff = False
assignment_task: asyncio.Task = handle.remote(request)
client_disconnection_task = loop.create_task(receive())
done, _ = await asyncio.wait(
Expand Down Expand Up @@ -636,7 +640,7 @@ async def send_request_to_replica_unary(
'by setting "request_timeout_s" in your Serve config\'s '
"`http_options` field."
)
backoff = True
should_backoff = True
else:
result = await object_ref
break
Expand All @@ -656,15 +660,14 @@ async def send_request_to_replica_unary(
f"{HTTP_REQUEST_MAX_RETRIES - retries} retries "
"remaining."
)
backoff = True
if backoff:
await asyncio.sleep(backoff_time_s)
# Be careful about the expotential backoff scaling here.
# Assuming 10 retries, 1.5x scaling means the last retry is 38x the
# initial backoff time, while 2x scaling means 512x the initial.
backoff_time_s *= 1.5
should_backoff = True

if should_backoff:
backoff_period = min(
INITIAL_BACKOFF_PERIOD_SEC * pow(2, retries), MAX_BACKOFF_PERIOD_SEC
)
retries += 1
backoff = False
await asyncio.sleep(backoff_period)
else:
error_message = f"Task failed with {HTTP_REQUEST_MAX_RETRIES} retries."
await Response(error_message, status_code=500).send(scope, receive, send)
Expand Down

0 comments on commit 3e6fc0d

Please sign in to comment.