connections.py 5.23 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
from collections.abc import Mapping, MutableMapping
5
6
7
8
from pathlib import Path

import aiohttp
import requests
9
from urllib3.util import parse_url
10
11
12
13
14
15
16
17
18
19
20
21

from vllm.version import __version__ as VLLM_VERSION


class HTTPConnection:
    """Helper class to send HTTP requests."""

    def __init__(self, *, reuse_client: bool = True) -> None:
        super().__init__()

        self.reuse_client = reuse_client

22
23
        self._sync_client: requests.Session | None = None
        self._async_client: aiohttp.ClientSession | None = None
24
25
26
27
28
29
30
31
32
33
34

    def get_sync_client(self) -> requests.Session:
        if self._sync_client is None or not self.reuse_client:
            self._sync_client = requests.Session()

        return self._sync_client

    # NOTE: We intentionally use an async function even though it is not
    # required, so that the client is only accessible inside async event loop
    async def get_async_client(self) -> aiohttp.ClientSession:
        if self._async_client is None or not self.reuse_client:
35
            self._async_client = aiohttp.ClientSession(trust_env=True)
36
37
38
39

        return self._async_client

    def _validate_http_url(self, url: str):
40
        parsed_url = parse_url(url)
41
42

        if parsed_url.scheme not in ("http", "https"):
43
44
45
            raise ValueError(
                "Invalid HTTP URL: A valid HTTP URL must have scheme 'http' or 'https'."
            )
46

47
    def _headers(self, **extras: str) -> MutableMapping[str, str]:
48
49
50
51
52
53
54
        return {"User-Agent": f"vLLM/{VLLM_VERSION}", **extras}

    def get_response(
        self,
        url: str,
        *,
        stream: bool = False,
55
56
        timeout: float | None = None,
        extra_headers: Mapping[str, str] | None = None,
57
        allow_redirects: bool = True,
58
59
60
61
62
63
    ):
        self._validate_http_url(url)

        client = self.get_sync_client()
        extra_headers = extra_headers or {}

64
65
66
67
68
69
70
        return client.get(
            url,
            headers=self._headers(**extra_headers),
            stream=stream,
            timeout=timeout,
            allow_redirects=allow_redirects,
        )
71
72
73
74
75

    async def get_async_response(
        self,
        url: str,
        *,
76
77
        timeout: float | None = None,
        extra_headers: Mapping[str, str] | None = None,
78
        allow_redirects: bool = True,
79
80
81
82
83
84
    ):
        self._validate_http_url(url)

        client = await self.get_async_client()
        extra_headers = extra_headers or {}

85
86
87
88
89
90
91
92
        return client.get(
            url,
            headers=self._headers(**extra_headers),
            timeout=timeout,
            allow_redirects=allow_redirects,
        )

    def get_bytes(
93
        self, url: str, *, timeout: float | None = None, allow_redirects: bool = True
94
95
96
97
    ) -> bytes:
        with self.get_response(
            url, timeout=timeout, allow_redirects=allow_redirects
        ) as r:
98
99
100
101
102
103
104
105
            r.raise_for_status()

            return r.content

    async def async_get_bytes(
        self,
        url: str,
        *,
106
        timeout: float | None = None,
107
        allow_redirects: bool = True,
108
    ) -> bytes:
109
        async with await self.get_async_response(
110
111
            url, timeout=timeout, allow_redirects=allow_redirects
        ) as r:
112
113
114
115
            r.raise_for_status()

            return await r.read()

116
    def get_text(self, url: str, *, timeout: float | None = None) -> str:
117
118
119
120
121
122
123
124
125
        with self.get_response(url, timeout=timeout) as r:
            r.raise_for_status()

            return r.text

    async def async_get_text(
        self,
        url: str,
        *,
126
        timeout: float | None = None,
127
128
129
130
131
132
    ) -> str:
        async with await self.get_async_response(url, timeout=timeout) as r:
            r.raise_for_status()

            return await r.text()

133
    def get_json(self, url: str, *, timeout: float | None = None) -> str:
134
135
136
137
138
139
140
141
142
        with self.get_response(url, timeout=timeout) as r:
            r.raise_for_status()

            return r.json()

    async def async_get_json(
        self,
        url: str,
        *,
143
        timeout: float | None = None,
144
145
146
147
148
149
150
151
152
153
154
    ) -> str:
        async with await self.get_async_response(url, timeout=timeout) as r:
            r.raise_for_status()

            return await r.json()

    def download_file(
        self,
        url: str,
        save_path: Path,
        *,
155
        timeout: float | None = None,
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
        chunk_size: int = 128,
    ) -> Path:
        with self.get_response(url, timeout=timeout) as r:
            r.raise_for_status()

            with save_path.open("wb") as f:
                for chunk in r.iter_content(chunk_size):
                    f.write(chunk)

        return save_path

    async def async_download_file(
        self,
        url: str,
        save_path: Path,
        *,
172
        timeout: float | None = None,
173
174
175
176
177
178
179
180
181
182
183
184
185
        chunk_size: int = 128,
    ) -> Path:
        async with await self.get_async_response(url, timeout=timeout) as r:
            r.raise_for_status()

            with save_path.open("wb") as f:
                async for chunk in r.content.iter_chunked(chunk_size):
                    f.write(chunk)

        return save_path


global_http_connection = HTTPConnection()
186
187
188
189
"""
The global [`HTTPConnection`][vllm.connections.HTTPConnection] instance used
by vLLM.
"""