forcingkit-client 0.2.0__tar.gz → 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/PKG-INFO +1 -1
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/pyproject.toml +1 -1
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/src/forcingkit_client/__init__.py +1 -1
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/src/forcingkit_client/store.py +44 -3
- forcingkit_client-0.2.1/tests/test_store.py +64 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/uv.lock +1 -1
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/.gitignore +0 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/LICENSE +0 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/README.md +0 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/src/forcingkit_client/client.py +0 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/src/forcingkit_client/schema.py +0 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/src/forcingkit_client/trajectory.py +0 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/tests/conftest.py +0 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/tests/test_client.py +0 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/tests/test_schema.py +0 -0
- {forcingkit_client-0.2.0 → forcingkit_client-0.2.1}/tests/test_trajectory.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: forcingkit-client
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: Lightweight Python client for a forcingkit service: ocean, atmosphere and station forcing as xarray, and forcing interpolated along a trajectory.
|
|
5
5
|
Project-URL: Documentation, https://forcingkit.docs.lhzn.io/client.html
|
|
6
6
|
Project-URL: Issues, https://github.com/lhzn-io/forcingkit/issues
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "forcingkit-client"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.1"
|
|
8
8
|
description = "Lightweight Python client for a forcingkit service: ocean, atmosphere and station forcing as xarray, and forcing interpolated along a trajectory."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -5,11 +5,18 @@ directory store that, on a miss, fetches the key from the server and keeps it, s
|
|
|
5
5
|
read remotely the first time and from disk afterwards, and only the chunks actually read are ever
|
|
6
6
|
transferred. The cache directory is keyed by store id and store version, so a store the server
|
|
7
7
|
rebuilt under the same id is fetched afresh.
|
|
8
|
+
|
|
9
|
+
Each chunk read opens its own connection. A connection kept open between reads can go stale while
|
|
10
|
+
the caller waits on the next job (the service closes idle connections after a few seconds); a
|
|
11
|
+
request written to it may then get no answer at all, and the read hangs until its timeout. A read
|
|
12
|
+
that fails in transport (a timeout, a reset, a refused connection) is retried on a new connection.
|
|
8
13
|
"""
|
|
9
14
|
|
|
10
15
|
import asyncio
|
|
16
|
+
import logging
|
|
11
17
|
import os
|
|
12
18
|
import threading
|
|
19
|
+
import time
|
|
13
20
|
from pathlib import Path
|
|
14
21
|
from typing import Any, Optional
|
|
15
22
|
|
|
@@ -17,26 +24,60 @@ import httpx
|
|
|
17
24
|
import xarray as xr
|
|
18
25
|
from zarr.storage import LocalStore
|
|
19
26
|
|
|
27
|
+
log = logging.getLogger(__name__)
|
|
28
|
+
|
|
20
29
|
|
|
21
30
|
class CachingHTTPStore(LocalStore):
|
|
22
|
-
def __init__(
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
root: str | Path,
|
|
34
|
+
url: str,
|
|
35
|
+
timeout_s: float = 60.0,
|
|
36
|
+
attempts: int = 4,
|
|
37
|
+
retry_wait_s: float = 2.0,
|
|
38
|
+
):
|
|
23
39
|
os.makedirs(root, exist_ok=True)
|
|
24
40
|
super().__init__(root, read_only=True)
|
|
25
41
|
self.url = url.rstrip("/")
|
|
26
42
|
self._timeout_s = timeout_s
|
|
43
|
+
self._attempts = max(1, attempts)
|
|
44
|
+
self._retry_wait_s = retry_wait_s
|
|
27
45
|
self._http: Optional[httpx.Client] = None
|
|
28
46
|
self._lock = threading.Lock()
|
|
29
47
|
|
|
30
48
|
def _client(self) -> httpx.Client:
|
|
31
49
|
with self._lock:
|
|
32
50
|
if self._http is None:
|
|
33
|
-
self._http = httpx.Client(
|
|
51
|
+
self._http = httpx.Client(
|
|
52
|
+
timeout=httpx.Timeout(self._timeout_s, connect=10.0),
|
|
53
|
+
limits=httpx.Limits(max_keepalive_connections=0),
|
|
54
|
+
)
|
|
34
55
|
return self._http
|
|
35
56
|
|
|
57
|
+
def _get(self, key: str) -> httpx.Response:
|
|
58
|
+
"""GET `key`, retrying a transport failure on a new connection."""
|
|
59
|
+
for attempt in range(1, self._attempts + 1):
|
|
60
|
+
try:
|
|
61
|
+
return self._client().get(f"{self.url}/{key}")
|
|
62
|
+
except httpx.TransportError as e:
|
|
63
|
+
if attempt == self._attempts:
|
|
64
|
+
raise
|
|
65
|
+
log.warning(
|
|
66
|
+
"store read %s/%s failed (%s: %s); retry %d of %d",
|
|
67
|
+
self.url,
|
|
68
|
+
key,
|
|
69
|
+
type(e).__name__,
|
|
70
|
+
e,
|
|
71
|
+
attempt,
|
|
72
|
+
self._attempts - 1,
|
|
73
|
+
)
|
|
74
|
+
time.sleep(self._retry_wait_s * attempt)
|
|
75
|
+
raise AssertionError("unreachable")
|
|
76
|
+
|
|
36
77
|
def _fetch(self, key: str) -> bool:
|
|
37
78
|
"""Copy `key` from the server into the cache; False when the server has no such key
|
|
38
79
|
(an unwritten chunk, which reads as the fill value)."""
|
|
39
|
-
response = self.
|
|
80
|
+
response = self._get(key)
|
|
40
81
|
if response.status_code == 404:
|
|
41
82
|
return False
|
|
42
83
|
response.raise_for_status()
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""CachingHTTPStore: chunk reads that fail in transport are retried on a new connection."""
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from forcingkit_client.store import CachingHTTPStore
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _store(tmp_path, handler, attempts=3):
|
|
10
|
+
store = CachingHTTPStore(
|
|
11
|
+
tmp_path / "cache",
|
|
12
|
+
"http://svc/api/v1/stores/s.zarr",
|
|
13
|
+
attempts=attempts,
|
|
14
|
+
retry_wait_s=0.0,
|
|
15
|
+
)
|
|
16
|
+
store._http = httpx.Client(transport=httpx.MockTransport(handler))
|
|
17
|
+
return store
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_read_that_times_out_is_retried(tmp_path):
|
|
21
|
+
calls = []
|
|
22
|
+
|
|
23
|
+
def handler(request):
|
|
24
|
+
calls.append(request.url.path)
|
|
25
|
+
if len(calls) == 1:
|
|
26
|
+
raise httpx.ReadTimeout("no answer", request=request)
|
|
27
|
+
return httpx.Response(200, content=b"chunk")
|
|
28
|
+
|
|
29
|
+
store = _store(tmp_path, handler)
|
|
30
|
+
assert store._fetch("u/0.0.0.0")
|
|
31
|
+
assert (tmp_path / "cache" / "u" / "0.0.0.0").read_bytes() == b"chunk"
|
|
32
|
+
assert calls == ["/api/v1/stores/s.zarr/u/0.0.0.0"] * 2
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_read_gives_up_after_its_attempts(tmp_path):
|
|
36
|
+
calls = []
|
|
37
|
+
|
|
38
|
+
def handler(request):
|
|
39
|
+
calls.append(1)
|
|
40
|
+
raise httpx.ConnectError("refused", request=request)
|
|
41
|
+
|
|
42
|
+
store = _store(tmp_path, handler, attempts=3)
|
|
43
|
+
with pytest.raises(httpx.ConnectError):
|
|
44
|
+
store._fetch("u/0.0.0.0")
|
|
45
|
+
assert len(calls) == 3
|
|
46
|
+
assert not (tmp_path / "cache" / "u" / "0.0.0.0").exists()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_missing_chunk_is_not_retried(tmp_path):
|
|
50
|
+
calls = []
|
|
51
|
+
|
|
52
|
+
def handler(request):
|
|
53
|
+
calls.append(1)
|
|
54
|
+
return httpx.Response(404)
|
|
55
|
+
|
|
56
|
+
store = _store(tmp_path, handler)
|
|
57
|
+
assert not store._fetch("u/0.0.0.0")
|
|
58
|
+
assert len(calls) == 1
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_connections_are_not_kept_between_reads(tmp_path):
|
|
62
|
+
store = CachingHTTPStore(tmp_path / "cache", "http://svc/s.zarr")
|
|
63
|
+
pool = store._client()._transport._pool
|
|
64
|
+
assert pool._max_keepalive_connections == 0
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|