Skip to content

Commit

Permalink
Add default timeout for network requests (#1528)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored May 25, 2024
1 parent 84a3989 commit 11b13ea
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/history/hatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
***Fixed:***

- The `fmt` command no longer hides the commands that are being executed
- Add default timeout for network requests, useful when installing Python distributions

## [1.11.1](https://github.com/pypa/hatch/releases/tag/hatch-v1.11.1) - 2024-05-23 ## {: #hatch-v1.11.1 }

Expand Down
6 changes: 6 additions & 0 deletions src/hatch/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

MINIMUM_SLEEP = 2
MAXIMUM_SLEEP = 20
# The timeout should be slightly larger than a multiple of 3,
# which is the default TCP packet retransmission window. See:
# https://tools.ietf.org/html/rfc2988
DEFAULT_TIMEOUT = 10


@contextmanager
Expand All @@ -34,6 +38,8 @@ def streaming_response(*args: Any, **kwargs: Any) -> Generator[httpx.Response, N


def download_file(path: Path, *args: Any, **kwargs: Any) -> None:
kwargs.setdefault('timeout', DEFAULT_TIMEOUT)

with path.open(mode='wb', buffering=0) as f, streaming_response('GET', *args, **kwargs) as response:
for chunk in response.iter_bytes(16384):
f.write(chunk)

0 comments on commit 11b13ea

Please sign in to comment.