ready_checker.py 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Utilities for checking endpoint readiness."""

import asyncio
import time

import aiohttp
from tqdm.asyncio import tqdm

11
from .endpoint_request_func import RequestFunc, RequestFuncInput, RequestFuncOutput
12
13
14


async def wait_for_endpoint(
15
    request_func: RequestFunc,
16
    test_input: RequestFuncInput,
17
    session: aiohttp.ClientSession,
18
19
20
21
22
    timeout_seconds: int = 600,
    retry_interval: int = 5,
) -> RequestFuncOutput:
    """
    Wait for an endpoint to become available before starting benchmarks.
23

24
25
26
27
28
    Args:
        request_func: The async request function to call
        test_input: The RequestFuncInput to test with
        timeout_seconds: Maximum time to wait in seconds (default: 10 minutes)
        retry_interval: Time between retries in seconds (default: 5 seconds)
29

30
31
    Returns:
        RequestFuncOutput: The successful response
32

33
34
35
36
37
38
    Raises:
        ValueError: If the endpoint doesn't become available within the timeout
    """
    deadline = time.perf_counter() + timeout_seconds
    output = RequestFuncOutput(success=False)
    print(f"Waiting for endpoint to become up in {timeout_seconds} seconds")
39

40
    with tqdm(
41
        total=timeout_seconds,
42
43
44
        bar_format="{desc} |{bar}| {elapsed} elapsed, {remaining} remaining",
        unit="s",
    ) as pbar:
45
        while True:
46
47
48
49
50
51
52
53
54
55
56
57
            # update progress bar
            remaining = deadline - time.perf_counter()
            elapsed = timeout_seconds - remaining
            update_amount = min(elapsed - pbar.n, timeout_seconds - pbar.n)
            pbar.update(update_amount)
            pbar.refresh()
            if remaining <= 0:
                pbar.close()
                break

            # ping the endpoint using request_func
            try:
58
                output = await request_func(
59
60
                    request_func_input=test_input, session=session
                )
61
62
63
64
65
                if output.success:
                    pbar.close()
                    return output
            except aiohttp.ClientConnectorError:
                pass
66

67
68
69
70
            # retry after a delay
            sleep_duration = min(retry_interval, remaining)
            if sleep_duration > 0:
                await asyncio.sleep(sleep_duration)
71

72
    return output