Skip to content

Commit

Permalink
Add RequestsFetcher settings as attributes
Browse files Browse the repository at this point in the history
Stop using the settings module and add the RequestsFetcher
specific config as instance attributes.
Users of the requests-based fetcher implementation can
modify them after instantiating a RequestsFetcher
object if needed.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
  • Loading branch information
sechkova committed Jul 6, 2021
1 parent 520b344 commit 37defa9
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions tuf/ngclient/_internal/requests_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

import logging
import time
from typing import Optional
from urllib import parse

# Imports
import requests
import urllib3.exceptions

import tuf
from tuf import exceptions, settings
from tuf import exceptions
from tuf.ngclient.fetcher import FetcherInterface

# Globals
Expand Down Expand Up @@ -47,6 +48,11 @@ def __init__(self):
# Some cookies may not be HTTP-safe.
self._sessions = {}

# Default settings
self.socket_timeout: int = 4 # seconds
self.chunk_size: int = 400000 # bytes
self.sleep_before_round: Optional[int] = None

def fetch(self, url, required_length):
"""Fetches the contents of HTTP/HTTPS url from a remote server.
Expand Down Expand Up @@ -75,9 +81,7 @@ def fetch(self, url, required_length):
# requests as:
# - connect timeout (max delay before first byte is received)
# - read (gap) timeout (max delay between bytes received)
response = session.get(
url, stream=True, timeout=settings.SOCKET_TIMEOUT
)
response = session.get(url, stream=True, timeout=self.socket_timeout)
# Check response status.
try:
response.raise_for_status()
Expand All @@ -99,11 +103,11 @@ def chunks():
# large file in one shot. Before beginning the round, sleep
# (if set) for a short amount of time so that the CPU is not
# hogged in the while loop.
if settings.SLEEP_BEFORE_ROUND:
time.sleep(settings.SLEEP_BEFORE_ROUND)
if self.sleep_before_round:
time.sleep(self.sleep_before_round)

read_amount = min(
settings.CHUNK_SIZE,
self.chunk_size,
required_length - bytes_received,
)

Expand Down

0 comments on commit 37defa9

Please sign in to comment.