Skip to content

Commit

Permalink
Timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
dvolodin7 committed Feb 28, 2024
1 parent 908dcd6 commit 2a70d45
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/async_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use reqwest::{
Method,
};
use std::collections::HashMap;
use std::time::Duration;

#[pyclass(module = "gufo.http.async_client")]
pub struct AsyncClient {
Expand All @@ -28,6 +29,8 @@ impl AsyncClient {
#[new]
fn new(
validate_cert: bool,
connect_timeout: u64,
timeout: u64,
max_redirect: Option<usize>,
headers: Option<HashMap<&str, &[u8]>>,
compression: Option<u8>,
Expand Down Expand Up @@ -61,10 +64,14 @@ impl AsyncClient {
builder = builder.brotli(true);
}
}
// Disable certificate validation
// Disable certificate validation when necessary
if !validate_cert {
builder = builder.danger_accept_invalid_certs(true);
}
// Set timeouts
builder = builder
.connect_timeout(Duration::from_nanos(connect_timeout))
.timeout(Duration::from_nanos(timeout));
//
let client = builder
.build()
Expand Down
2 changes: 2 additions & 0 deletions src/gufo/http/_fast.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class AsyncClient(object):
def __init__(
self: "AsyncClient",
validate_cert: bool,
connect_timeout_ns: int,
timeout_ns: int,
max_redirects: Optional[int],
headers: Optional[Dict[str, bytes]],
compression: Optional[int],
Expand Down
10 changes: 10 additions & 0 deletions src/gufo/http/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from .util import merge_dict

MAX_REDIRECTS = 10
DEFAULT_CONNECT_TIMEOUT = 30.0
DEFAULT_TIMEOUT = 3600.0
NS = 1_000_000_000.0


class HttpClient(object):
Expand All @@ -34,19 +37,26 @@ class HttpClient(object):
Set to `None` to disable compression support.
validate_cert: Set to `False` to disable TLS certificate
validation.
connect_timeout: Timeout to establish connection, in seconds.
timeout: Request timeout, in seconds.
"""

headers: Optional[Dict[str, bytes]] = None

def __init__(
self: "HttpClient",
/,
max_redirects: Optional[int] = MAX_REDIRECTS,
headers: Optional[Dict[str, bytes]] = None,
compression: Optional[int] = DEFLATE | GZIP | BROTLI,
validate_cert: bool = True,
connect_timeout: float = DEFAULT_CONNECT_TIMEOUT,
timeout: float = DEFAULT_TIMEOUT,
) -> None:
self._client = AsyncClient(
validate_cert,
int(connect_timeout * NS),
int(timeout * NS),
max_redirects,
merge_dict(self.headers, headers),
compression,
Expand Down
11 changes: 10 additions & 1 deletion tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from gufo.http.httpd import Httpd
from gufo.http import GZIP

from .util import URL_PREFIX
from .util import URL_PREFIX, UNROUTABLE_URL


def test_get(httpd: Httpd) -> None:
Expand Down Expand Up @@ -298,3 +298,12 @@ async def inner() -> None:
assert b"</html>" in data

asyncio.run(inner())


def test_connect_timeout() -> None:
async def inner() -> None:
async with HttpClient(connect_timeout=1.0) as client:
resp = await client.get(UNROUTABLE_URL)
assert resp.status == 200

asyncio.run(inner())
1 change: 1 addition & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
HTTPD_ADDRESS = "127.0.0.1"
HTTPD_PORT = random.randint(52000, 53999)
URL_PREFIX = f"http://{HTTPD_HOST}:{HTTPD_PORT}"
UNROUTABLE_URL = "http://192.0.2.1/"

0 comments on commit 2a70d45

Please sign in to comment.