Skip to content

Commit 0d75982

Browse files
committed
_prepare_credentials: fixes for fsspec>=2022.12
simplify _prepare credentials, cleanup
1 parent 7a97ab4 commit 0d75982

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

dvc_http/__init__.py

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import threading
22
from getpass import getpass
3-
from typing import BinaryIO, Optional, Union
3+
from typing import TYPE_CHECKING, BinaryIO, Optional, Union
44

55
from dvc_objects.fs.base import AnyFSPath, FileSystem
66
from dvc_objects.fs.callbacks import DEFAULT_CALLBACK, Callback
77
from dvc_objects.fs.errors import ConfigError
88
from funcy import cached_property, memoize, wrap_with
99

10+
if TYPE_CHECKING:
11+
from ssl import SSLContext
12+
1013

1114
@wrap_with(threading.Lock())
1215
@memoize
1316
def ask_password(host, user):
1417
return getpass(f"Enter a password for host '{host}' user '{user}':\n")
1518

1619

17-
def make_context(ssl_verify):
20+
def make_context(
21+
ssl_verify: Union[bool, str, None]
22+
) -> Union["SSLContext", bool, None]:
1823
if isinstance(ssl_verify, bool) or ssl_verify is None:
1924
return ssl_verify
2025

@@ -40,7 +45,6 @@ class HTTPFileSystem(FileSystem):
4045

4146
def _prepare_credentials(self, **config):
4247
import aiohttp
43-
from fsspec.asyn import fsspec_loop
4448

4549
credentials = {}
4650
client_kwargs = credentials.setdefault("client_kwargs", {})
@@ -74,27 +78,12 @@ def _prepare_credentials(self, **config):
7478
f"Auth method {auth_method!r} is not supported."
7579
)
7680

77-
# Force cleanup of closed SSL transports.
78-
# https://github.com/iterative/dvc/issues/7414
79-
connector_kwargs = {"enable_cleanup_closed": True}
80-
8181
if "ssl_verify" in config:
82-
connector_kwargs.update(ssl=make_context(config["ssl_verify"]))
83-
84-
with fsspec_loop():
85-
client_kwargs["connector"] = aiohttp.TCPConnector(
86-
**connector_kwargs
87-
)
88-
# The connector should not be owned by aiohttp.ClientSession since
89-
# it is closed by fsspec (HTTPFileSystem.close_session)
90-
client_kwargs["connector_owner"] = False
91-
92-
client_kwargs["connect_timeout"] = config.get(
93-
"connect_timeout", self.REQUEST_TIMEOUT
94-
)
95-
client_kwargs["read_timeout"] = config.get(
96-
"read_timeout", self.REQUEST_TIMEOUT
97-
)
82+
client_kwargs["ssl_verify"] = config["ssl_verify"]
83+
84+
for timeout in ("connect_timeout", "read_timeout"):
85+
if timeout in config:
86+
client_kwargs[timeout] = config.get(timeout)
9887

9988
# Allow reading proxy configurations from the environment.
10089
client_kwargs["trust_env"] = True
@@ -105,8 +94,6 @@ def _prepare_credentials(self, **config):
10594

10695
async def get_client(
10796
self,
108-
connect_timeout: Optional[float],
109-
read_timeout: Optional[float],
11097
**kwargs,
11198
):
11299
import aiohttp
@@ -121,16 +108,24 @@ async def get_client(
121108
exceptions={aiohttp.ClientError},
122109
)
123110

124-
# The default timeout for the aiohttp is 300 seconds
125-
# which is too low for DVC's interactions (especially
126-
# on the read) when dealing with large data blobs. We
127-
# unlimit the total time to read, and only limit the
128-
# time that is spent when connecting to the remote server.
111+
# The default total timeout for an aiohttp request is 300 seconds
112+
# which is too low for DVC's interactions when dealing with large
113+
# data blobs. We remove the total timeout, and only limit the time
114+
# that is spent when connecting to the remote server and waiting
115+
# for new data portions.
116+
connect_timeout = kwargs.get("connect_timeout", self.REQUEST_TIMEOUT)
129117
kwargs["timeout"] = aiohttp.ClientTimeout(
130118
total=None,
131119
connect=connect_timeout,
132120
sock_connect=connect_timeout,
133-
sock_read=read_timeout,
121+
sock_read=kwargs.get("read_timeout", self.REQUEST_TIMEOUT),
122+
)
123+
124+
kwargs["connector"] = aiohttp.TCPConnector(
125+
# Force cleanup of closed SSL transports.
126+
# See https://github.com/iterative/dvc/issues/7414
127+
enable_cleanup_closed=True,
128+
ssl=make_context(kwargs.get("ssl_verify")),
134129
)
135130

136131
return ReadOnlyRetryClient(**kwargs)

0 commit comments

Comments
 (0)