frequenz-client-base 0.6.1__tar.gz → 0.8.0__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.
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/PKG-INFO +1 -1
- frequenz-client-base-0.8.0/RELEASE_NOTES.md +60 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/pyproject.toml +13 -13
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/channel.py +111 -4
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/client.py +44 -19
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/retry.py +3 -2
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/streaming.py +23 -4
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz_client_base.egg-info/PKG-INFO +1 -1
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz_client_base.egg-info/requires.txt +13 -13
- frequenz-client-base-0.6.1/RELEASE_NOTES.md +0 -5
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/LICENSE +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/MANIFEST.in +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/README.md +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/setup.cfg +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/__init__.py +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/conftest.py +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/conversion.py +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/exception.py +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/py.typed +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz_client_base.egg-info/SOURCES.txt +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz_client_base.egg-info/dependency_links.txt +0 -0
- {frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz_client_base.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Frequenz Client Base Library Release Notes
|
|
2
|
+
|
|
3
|
+
## Upgrading
|
|
4
|
+
|
|
5
|
+
The `BaseApiClient` class is generic again. There was too many issues with the new approach, so it was rolled back.
|
|
6
|
+
|
|
7
|
+
- If you are upgrading from v0.7.x, you should be able to roll back your changes with the upgrade and just keep the new `stub` property.
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
# Old
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
import my_service_pb2_grpc
|
|
13
|
+
class MyApiClient(BaseApiClient):
|
|
14
|
+
def __init__(self, server_url: str, *, ...) -> None:
|
|
15
|
+
super().__init__(server_url, ...)
|
|
16
|
+
stub = my_service_pb2_grpc.MyServiceStub(self.channel)
|
|
17
|
+
self._stub: my_service_pb2_grpc.MyServiceAsyncStub = stub # type: ignore
|
|
18
|
+
...
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def stub(self) -> my_service_pb2_grpc.MyServiceAsyncStub:
|
|
22
|
+
if self.channel is None:
|
|
23
|
+
raise ClientNotConnected(server_url=self.server_url, operation="stub")
|
|
24
|
+
return self._stub
|
|
25
|
+
|
|
26
|
+
# New
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
import my_service_pb2_grpc
|
|
29
|
+
from my_service_pb2_grpc import MyServiceStub
|
|
30
|
+
class MyApiClient(BaseApiClient[MyServiceStub]):
|
|
31
|
+
def __init__(self, server_url: str, *, ...) -> None:
|
|
32
|
+
super().__init__(server_url, MyServiceStub, ...)
|
|
33
|
+
...
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def stub(self) -> my_service_pb2_grpc.MyServiceAsyncStub:
|
|
37
|
+
"""The gRPC stub for the API."""
|
|
38
|
+
if self.channel is None or self._stub is None:
|
|
39
|
+
raise ClientNotConnected(server_url=self.server_url, operation="stub")
|
|
40
|
+
# This type: ignore is needed because we need to cast the sync stub to
|
|
41
|
+
# the async stub, but we can't use cast because the async stub doesn't
|
|
42
|
+
# actually exists to the eyes of the interpreter, it only exists for the
|
|
43
|
+
# type-checker, so it can only be used for type hints.
|
|
44
|
+
return self._stub # type: ignore
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- If you are upgrading from v0.6.x, you should only need to add the `stub` property to your client class and then use that property instead of `_stub` in your code.
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
@property
|
|
51
|
+
def stub(self) -> my_service_pb2_grpc.MyServiceAsyncStub:
|
|
52
|
+
"""The gRPC stub for the API."""
|
|
53
|
+
if self.channel is None or self._stub is None:
|
|
54
|
+
raise ClientNotConnected(server_url=self.server_url, operation="stub")
|
|
55
|
+
# This type: ignore is needed because we need to cast the sync stub to
|
|
56
|
+
# the async stub, but we can't use cast because the async stub doesn't
|
|
57
|
+
# actually exists to the eyes of the interpreter, it only exists for the
|
|
58
|
+
# type-checker, so it can only be used for type hints.
|
|
59
|
+
return self._stub # type: ignore
|
|
60
|
+
```
|
|
@@ -42,45 +42,45 @@ dev-flake8 = [
|
|
|
42
42
|
"flake8 == 7.1.1",
|
|
43
43
|
"flake8-docstrings == 1.7.0",
|
|
44
44
|
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
|
|
45
|
-
"pydoclint == 0.5.
|
|
45
|
+
"pydoclint == 0.5.9",
|
|
46
46
|
"pydocstyle == 6.3.0",
|
|
47
47
|
"frequenz-client-base",
|
|
48
48
|
]
|
|
49
|
-
dev-formatting = ["black == 24.
|
|
49
|
+
dev-formatting = ["black == 24.10.0", "isort == 5.13.2"]
|
|
50
50
|
dev-mkdocs = [
|
|
51
|
-
"black == 24.
|
|
51
|
+
"black == 24.10.0",
|
|
52
52
|
"Markdown==3.7",
|
|
53
53
|
"mike == 2.1.3",
|
|
54
54
|
"mkdocs-gen-files == 0.5.0",
|
|
55
55
|
"mkdocs-literate-nav == 0.6.1",
|
|
56
|
-
"mkdocs-macros-plugin == 1.
|
|
57
|
-
"mkdocs-material == 9.5.
|
|
58
|
-
"mkdocstrings[python] == 0.
|
|
59
|
-
"mkdocstrings-python == 1.
|
|
56
|
+
"mkdocs-macros-plugin == 1.3.7",
|
|
57
|
+
"mkdocs-material == 9.5.43",
|
|
58
|
+
"mkdocstrings[python] == 0.26.2",
|
|
59
|
+
"mkdocstrings-python == 1.12.2",
|
|
60
60
|
"frequenz-repo-config[lib] == 0.10.0",
|
|
61
61
|
"frequenz-client-base",
|
|
62
62
|
]
|
|
63
63
|
dev-mypy = [
|
|
64
|
-
"mypy == 1.
|
|
64
|
+
"mypy == 1.13.0",
|
|
65
65
|
"types-Markdown == 3.7.0.20240822",
|
|
66
|
-
"types-protobuf == 5.
|
|
66
|
+
"types-protobuf == 5.28.3.20241030",
|
|
67
67
|
"grpc-stubs == 1.53.0.5", # This dependency introduces breaking changes in patch releases
|
|
68
68
|
# For checking the noxfile, docs/ script, and tests
|
|
69
69
|
"frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]",
|
|
70
70
|
]
|
|
71
|
-
dev-noxfile = ["nox == 2024.
|
|
71
|
+
dev-noxfile = ["nox == 2024.10.9", "frequenz-repo-config[lib] == 0.10.0"]
|
|
72
72
|
dev-pylint = [
|
|
73
|
-
"pylint == 3.
|
|
73
|
+
"pylint == 3.3.1",
|
|
74
74
|
# For checking the noxfile, docs/ script, and tests
|
|
75
75
|
"frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]",
|
|
76
76
|
]
|
|
77
77
|
dev-pytest = [
|
|
78
|
-
"pytest == 8.3.
|
|
78
|
+
"pytest == 8.3.3",
|
|
79
79
|
"frequenz-repo-config[extra-lint-examples] == 0.10.0",
|
|
80
80
|
"pytest-mock == 3.14.0",
|
|
81
81
|
"pytest-asyncio == 0.24.0",
|
|
82
82
|
"async-solipsism == 0.7",
|
|
83
|
-
"hypothesis == 6.
|
|
83
|
+
"hypothesis == 6.116.0",
|
|
84
84
|
"frequenz-client-base",
|
|
85
85
|
]
|
|
86
86
|
dev = [
|
{frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/channel.py
RENAMED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import dataclasses
|
|
7
7
|
import pathlib
|
|
8
|
+
from datetime import timedelta
|
|
8
9
|
from typing import assert_never
|
|
9
10
|
from urllib.parse import parse_qs, urlparse
|
|
10
11
|
|
|
@@ -41,16 +42,33 @@ class SslOptions:
|
|
|
41
42
|
"""
|
|
42
43
|
|
|
43
44
|
|
|
45
|
+
@dataclasses.dataclass(frozen=True)
|
|
46
|
+
class KeepAliveOptions:
|
|
47
|
+
"""Options for HTTP2 keep-alive pings."""
|
|
48
|
+
|
|
49
|
+
enabled: bool = True
|
|
50
|
+
"""Whether HTTP2 keep-alive should be enabled."""
|
|
51
|
+
|
|
52
|
+
interval: timedelta = timedelta(seconds=60)
|
|
53
|
+
"""The interval between HTTP2 pings."""
|
|
54
|
+
|
|
55
|
+
timeout: timedelta = timedelta(seconds=20)
|
|
56
|
+
"""The time to wait for a HTTP2 keep-alive response."""
|
|
57
|
+
|
|
58
|
+
|
|
44
59
|
@dataclasses.dataclass(frozen=True)
|
|
45
60
|
class ChannelOptions:
|
|
46
61
|
"""Options for a gRPC channel."""
|
|
47
62
|
|
|
48
|
-
port: int =
|
|
63
|
+
port: int | None = None
|
|
49
64
|
"""The port number to connect to."""
|
|
50
65
|
|
|
51
66
|
ssl: SslOptions = SslOptions()
|
|
52
67
|
"""SSL options for the channel."""
|
|
53
68
|
|
|
69
|
+
keep_alive: KeepAliveOptions = KeepAliveOptions()
|
|
70
|
+
"""HTTP2 keep-alive options for the channel."""
|
|
71
|
+
|
|
54
72
|
|
|
55
73
|
def parse_grpc_uri(
|
|
56
74
|
uri: str,
|
|
@@ -68,7 +86,8 @@ def parse_grpc_uri(
|
|
|
68
86
|
A few things to consider about URI components:
|
|
69
87
|
|
|
70
88
|
- If any other components are present in the URI, a [`ValueError`][] is raised.
|
|
71
|
-
- If the port is omitted, the `default_port` is used
|
|
89
|
+
- If the port is omitted, the `default_port` is used unless it is `None`, in which
|
|
90
|
+
case a `ValueError` is raised
|
|
72
91
|
- If a query parameter is passed many times, the last value is used.
|
|
73
92
|
- Boolean query parameters can be specified with the following values
|
|
74
93
|
(case-insensitive): `true`, `1`, `on`, `false`, `0`, `off`.
|
|
@@ -110,10 +129,49 @@ def parse_grpc_uri(
|
|
|
110
129
|
|
|
111
130
|
options = _parse_query_params(uri, parsed_uri.query)
|
|
112
131
|
|
|
132
|
+
if parsed_uri.port is None and defaults.port is None:
|
|
133
|
+
raise ValueError(
|
|
134
|
+
f"The gRPC URI '{uri}' doesn't specify a port and there is no default."
|
|
135
|
+
)
|
|
136
|
+
|
|
113
137
|
target = (
|
|
114
138
|
parsed_uri.netloc if parsed_uri.port else f"{parsed_uri.netloc}:{defaults.port}"
|
|
115
139
|
)
|
|
116
140
|
|
|
141
|
+
keep_alive = (
|
|
142
|
+
defaults.keep_alive.enabled
|
|
143
|
+
if options.keep_alive is None
|
|
144
|
+
else options.keep_alive
|
|
145
|
+
)
|
|
146
|
+
channel_options = (
|
|
147
|
+
[
|
|
148
|
+
("grpc.http2.max_pings_without_data", 0),
|
|
149
|
+
("grpc.keepalive_permit_without_calls", 1),
|
|
150
|
+
(
|
|
151
|
+
"grpc.keepalive_time_ms",
|
|
152
|
+
(
|
|
153
|
+
(
|
|
154
|
+
defaults.keep_alive.interval
|
|
155
|
+
if options.keep_alive_interval is None
|
|
156
|
+
else options.keep_alive_interval
|
|
157
|
+
).total_seconds()
|
|
158
|
+
* 1000
|
|
159
|
+
),
|
|
160
|
+
),
|
|
161
|
+
(
|
|
162
|
+
"grpc.keepalive_timeout_ms",
|
|
163
|
+
(
|
|
164
|
+
defaults.keep_alive.timeout
|
|
165
|
+
if options.keep_alive_timeout is None
|
|
166
|
+
else options.keep_alive_timeout
|
|
167
|
+
).total_seconds()
|
|
168
|
+
* 1000,
|
|
169
|
+
),
|
|
170
|
+
]
|
|
171
|
+
if keep_alive
|
|
172
|
+
else None
|
|
173
|
+
)
|
|
174
|
+
|
|
117
175
|
ssl = defaults.ssl.enabled if options.ssl is None else options.ssl
|
|
118
176
|
if ssl:
|
|
119
177
|
return secure_channel(
|
|
@@ -135,8 +193,9 @@ def parse_grpc_uri(
|
|
|
135
193
|
defaults.ssl.certificate_chain,
|
|
136
194
|
),
|
|
137
195
|
),
|
|
196
|
+
channel_options,
|
|
138
197
|
)
|
|
139
|
-
return insecure_channel(target)
|
|
198
|
+
return insecure_channel(target, channel_options)
|
|
140
199
|
|
|
141
200
|
|
|
142
201
|
def _to_bool(value: str) -> bool:
|
|
@@ -154,6 +213,9 @@ class _QueryParams:
|
|
|
154
213
|
ssl_root_certificates_path: pathlib.Path | None
|
|
155
214
|
ssl_private_key_path: pathlib.Path | None
|
|
156
215
|
ssl_certificate_chain_path: pathlib.Path | None
|
|
216
|
+
keep_alive: bool | None
|
|
217
|
+
keep_alive_interval: timedelta | None
|
|
218
|
+
keep_alive_timeout: timedelta | None
|
|
157
219
|
|
|
158
220
|
|
|
159
221
|
def _parse_query_params(uri: str, query_string: str) -> _QueryParams:
|
|
@@ -194,6 +256,26 @@ def _parse_query_params(uri: str, query_string: str) -> _QueryParams:
|
|
|
194
256
|
f"Option(s) {', '.join(erros)} found in URI {uri!r}, but SSL is disabled",
|
|
195
257
|
)
|
|
196
258
|
|
|
259
|
+
keep_alive_option = options.pop("keep_alive", None)
|
|
260
|
+
keep_alive: bool | None = None
|
|
261
|
+
if keep_alive_option is not None:
|
|
262
|
+
keep_alive = _to_bool(keep_alive_option)
|
|
263
|
+
|
|
264
|
+
keep_alive_opts = {
|
|
265
|
+
k: options.pop(k, None)
|
|
266
|
+
for k in ("keep_alive_interval_s", "keep_alive_timeout_s")
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if keep_alive is False:
|
|
270
|
+
erros = []
|
|
271
|
+
for opt_name, opt in keep_alive_opts.items():
|
|
272
|
+
if opt is not None:
|
|
273
|
+
erros.append(opt_name)
|
|
274
|
+
if erros:
|
|
275
|
+
raise ValueError(
|
|
276
|
+
f"Option(s) {', '.join(erros)} found in URI {uri!r}, but keep_alive is disabled",
|
|
277
|
+
)
|
|
278
|
+
|
|
197
279
|
if options:
|
|
198
280
|
names = ", ".join(options)
|
|
199
281
|
raise ValueError(
|
|
@@ -203,7 +285,32 @@ def _parse_query_params(uri: str, query_string: str) -> _QueryParams:
|
|
|
203
285
|
|
|
204
286
|
return _QueryParams(
|
|
205
287
|
ssl=ssl,
|
|
206
|
-
|
|
288
|
+
ssl_root_certificates_path=(
|
|
289
|
+
pathlib.Path(ssl_opts["ssl_root_certificates_path"])
|
|
290
|
+
if ssl_opts["ssl_root_certificates_path"] is not None
|
|
291
|
+
else None
|
|
292
|
+
),
|
|
293
|
+
ssl_private_key_path=(
|
|
294
|
+
pathlib.Path(ssl_opts["ssl_private_key_path"])
|
|
295
|
+
if ssl_opts["ssl_private_key_path"] is not None
|
|
296
|
+
else None
|
|
297
|
+
),
|
|
298
|
+
ssl_certificate_chain_path=(
|
|
299
|
+
pathlib.Path(ssl_opts["ssl_certificate_chain_path"])
|
|
300
|
+
if ssl_opts["ssl_certificate_chain_path"] is not None
|
|
301
|
+
else None
|
|
302
|
+
),
|
|
303
|
+
keep_alive=keep_alive,
|
|
304
|
+
keep_alive_interval=(
|
|
305
|
+
timedelta(seconds=float(keep_alive_opts["keep_alive_interval_s"]))
|
|
306
|
+
if keep_alive_opts["keep_alive_interval_s"] is not None
|
|
307
|
+
else None
|
|
308
|
+
),
|
|
309
|
+
keep_alive_timeout=(
|
|
310
|
+
timedelta(seconds=float(keep_alive_opts["keep_alive_timeout_s"]))
|
|
311
|
+
if keep_alive_opts["keep_alive_timeout_s"] is not None
|
|
312
|
+
else None
|
|
313
|
+
),
|
|
207
314
|
)
|
|
208
315
|
|
|
209
316
|
|
{frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/client.py
RENAMED
|
@@ -24,6 +24,16 @@ class BaseApiClient(abc.ABC, Generic[StubT]):
|
|
|
24
24
|
server. It is designed to be subclassed by specific API clients that provide a more
|
|
25
25
|
specific interface.
|
|
26
26
|
|
|
27
|
+
Note:
|
|
28
|
+
It is recommended to add a `stub` property to the subclass that returns the gRPC
|
|
29
|
+
stub to use but using the *async stub* type instead of the *sync stub* type.
|
|
30
|
+
This is because the gRPC library provides async stubs that have proper async
|
|
31
|
+
type hints, but they only live in `.pyi` files, so they can be used in a very
|
|
32
|
+
limited way (only as type hints). Because of this, a `type: ignore` comment is
|
|
33
|
+
needed to cast the sync stub to the async stub.
|
|
34
|
+
|
|
35
|
+
Please see the example below for a recommended way to implement this property.
|
|
36
|
+
|
|
27
37
|
Some extra tools are provided to make it easier to write API clients:
|
|
28
38
|
|
|
29
39
|
- [call_stub_method()][frequenz.client.base.client.call_stub_method] is a function
|
|
@@ -51,6 +61,16 @@ class BaseApiClient(abc.ABC, Generic[StubT]):
|
|
|
51
61
|
float_value: float
|
|
52
62
|
|
|
53
63
|
class ExampleStub:
|
|
64
|
+
def example_method(
|
|
65
|
+
self,
|
|
66
|
+
request: ExampleRequest # pylint: disable=unused-argument
|
|
67
|
+
) -> ExampleResponse:
|
|
68
|
+
...
|
|
69
|
+
|
|
70
|
+
def example_stream(self) -> AsyncIterable[ExampleResponse]:
|
|
71
|
+
...
|
|
72
|
+
|
|
73
|
+
class ExampleAsyncStub:
|
|
54
74
|
async def example_method(
|
|
55
75
|
self,
|
|
56
76
|
request: ExampleRequest # pylint: disable=unused-argument
|
|
@@ -65,10 +85,19 @@ class BaseApiClient(abc.ABC, Generic[StubT]):
|
|
|
65
85
|
def __init__(self, response: ExampleResponse):
|
|
66
86
|
self.transformed_value = f"{response.float_value:.2f}"
|
|
67
87
|
|
|
88
|
+
# Change defaults as needed
|
|
89
|
+
DEFAULT_CHANNEL_OPTIONS = ChannelOptions()
|
|
90
|
+
|
|
68
91
|
class MyApiClient(BaseApiClient[ExampleStub]):
|
|
69
|
-
def __init__(
|
|
92
|
+
def __init__(
|
|
93
|
+
self,
|
|
94
|
+
server_url: str,
|
|
95
|
+
*,
|
|
96
|
+
connect: bool = True,
|
|
97
|
+
channel_defaults: ChannelOptions = DEFAULT_CHANNEL_OPTIONS,
|
|
98
|
+
) -> None:
|
|
70
99
|
super().__init__(
|
|
71
|
-
server_url, ExampleStub, connect=connect
|
|
100
|
+
server_url, ExampleStub, connect=connect, channel_defaults=channel_defaults
|
|
72
101
|
)
|
|
73
102
|
self._broadcaster = GrpcStreamBroadcaster(
|
|
74
103
|
"stream",
|
|
@@ -76,6 +105,16 @@ class BaseApiClient(abc.ABC, Generic[StubT]):
|
|
|
76
105
|
ExampleResponseWrapper,
|
|
77
106
|
)
|
|
78
107
|
|
|
108
|
+
@property
|
|
109
|
+
def stub(self) -> ExampleAsyncStub:
|
|
110
|
+
if self.channel is None or self._stub is None:
|
|
111
|
+
raise ClientNotConnected(server_url=self.server_url, operation="stub")
|
|
112
|
+
# This type: ignore is needed because we need to cast the sync stub to
|
|
113
|
+
# the async stub, but we can't use cast because the async stub doesn't
|
|
114
|
+
# actually exists to the eyes of the interpreter, it only exists for the
|
|
115
|
+
# type-checker, so it can only be used for type hints.
|
|
116
|
+
return self._stub # type: ignore
|
|
117
|
+
|
|
79
118
|
async def example_method(
|
|
80
119
|
self, int_value: int, str_value: str
|
|
81
120
|
) -> ExampleResponseWrapper:
|
|
@@ -165,22 +204,6 @@ class BaseApiClient(abc.ABC, Generic[StubT]):
|
|
|
165
204
|
"""The default options for the gRPC channel."""
|
|
166
205
|
return self._channel_defaults
|
|
167
206
|
|
|
168
|
-
@property
|
|
169
|
-
def stub(self) -> StubT:
|
|
170
|
-
"""The underlying gRPC stub.
|
|
171
|
-
|
|
172
|
-
Warning:
|
|
173
|
-
This stub is provided as a last resort for advanced users. It is not
|
|
174
|
-
recommended to use this property directly unless you know what you are
|
|
175
|
-
doing and you don't care about being tied to a specific gRPC library.
|
|
176
|
-
|
|
177
|
-
Raises:
|
|
178
|
-
ClientNotConnected: If the client is not connected to the server.
|
|
179
|
-
"""
|
|
180
|
-
if self._stub is None:
|
|
181
|
-
raise ClientNotConnected(server_url=self.server_url, operation="stub")
|
|
182
|
-
return self._stub
|
|
183
|
-
|
|
184
207
|
@property
|
|
185
208
|
def is_connected(self) -> bool:
|
|
186
209
|
"""Whether the client is connected to the server."""
|
|
@@ -258,7 +281,9 @@ async def call_stub_method(
|
|
|
258
281
|
) -> StubOutT: ...
|
|
259
282
|
|
|
260
283
|
|
|
261
|
-
|
|
284
|
+
# We need the `noqa: DOC503` because `pydoclint` can't figure out that
|
|
285
|
+
# `ApiClientError.from_grpc_error()` returns a `GrpcError` instance.
|
|
286
|
+
async def call_stub_method( # noqa: DOC503
|
|
262
287
|
client: BaseApiClient[StubT],
|
|
263
288
|
stub_method: Callable[[], Awaitable[StubOutT]],
|
|
264
289
|
*,
|
|
@@ -79,6 +79,7 @@ class LinearBackoff(Strategy):
|
|
|
79
79
|
|
|
80
80
|
def __init__(
|
|
81
81
|
self,
|
|
82
|
+
*,
|
|
82
83
|
interval: float = DEFAULT_RETRY_INTERVAL,
|
|
83
84
|
jitter: float = DEFAULT_RETRY_JITTER,
|
|
84
85
|
limit: int | None = None,
|
|
@@ -124,9 +125,9 @@ class ExponentialBackoff(Strategy):
|
|
|
124
125
|
DEFAULT_MULTIPLIER = 2.0
|
|
125
126
|
"""Default multiplier for exponential increment."""
|
|
126
127
|
|
|
127
|
-
# pylint: disable=too-many-arguments
|
|
128
|
-
def __init__(
|
|
128
|
+
def __init__( # pylint: disable=too-many-arguments
|
|
129
129
|
self,
|
|
130
|
+
*,
|
|
130
131
|
initial_interval: float = DEFAULT_INTERVAL,
|
|
131
132
|
max_interval: float = DEFAULT_MAX_INTERVAL,
|
|
132
133
|
multiplier: float = DEFAULT_MULTIPLIER,
|
{frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/streaming.py
RENAMED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
import asyncio
|
|
7
7
|
import logging
|
|
8
|
-
from collections.abc import
|
|
9
|
-
from typing import Generic, TypeVar
|
|
8
|
+
from collections.abc import Callable
|
|
9
|
+
from typing import AsyncIterable, Generic, TypeVar
|
|
10
10
|
|
|
11
11
|
import grpc.aio
|
|
12
12
|
|
|
@@ -27,12 +27,13 @@ OutputT = TypeVar("OutputT")
|
|
|
27
27
|
class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
|
|
28
28
|
"""Helper class to handle grpc streaming methods."""
|
|
29
29
|
|
|
30
|
-
def __init__(
|
|
30
|
+
def __init__( # pylint: disable=too-many-arguments,too-many-positional-arguments
|
|
31
31
|
self,
|
|
32
32
|
stream_name: str,
|
|
33
|
-
stream_method: Callable[[],
|
|
33
|
+
stream_method: Callable[[], AsyncIterable[InputT]],
|
|
34
34
|
transform: Callable[[InputT], OutputT],
|
|
35
35
|
retry_strategy: retry.Strategy | None = None,
|
|
36
|
+
retry_on_exhausted_stream: bool = False,
|
|
36
37
|
):
|
|
37
38
|
"""Initialize the streaming helper.
|
|
38
39
|
|
|
@@ -43,6 +44,8 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
|
|
|
43
44
|
transform: A function to transform the input type to the output type.
|
|
44
45
|
retry_strategy: The retry strategy to use, when the connection is lost. Defaults
|
|
45
46
|
to retries every 3 seconds, with a jitter of 1 second, indefinitely.
|
|
47
|
+
retry_on_exhausted_stream: Whether to retry when the stream is exhausted, i.e.
|
|
48
|
+
when the server closes the stream. Defaults to False.
|
|
46
49
|
"""
|
|
47
50
|
self._stream_name = stream_name
|
|
48
51
|
self._stream_method = stream_method
|
|
@@ -50,6 +53,7 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
|
|
|
50
53
|
self._retry_strategy = (
|
|
51
54
|
retry.LinearBackoff() if retry_strategy is None else retry_strategy.copy()
|
|
52
55
|
)
|
|
56
|
+
self._retry_on_exhausted_stream = retry_on_exhausted_stream
|
|
53
57
|
|
|
54
58
|
self._channel: channels.Broadcast[OutputT] = channels.Broadcast(
|
|
55
59
|
name=f"GrpcStreamBroadcaster-{stream_name}"
|
|
@@ -67,6 +71,15 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
|
|
|
67
71
|
"""
|
|
68
72
|
return self._channel.new_receiver(limit=maxsize)
|
|
69
73
|
|
|
74
|
+
@property
|
|
75
|
+
def is_running(self) -> bool:
|
|
76
|
+
"""Return whether the streaming helper is running.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
Whether the streaming helper is running.
|
|
80
|
+
"""
|
|
81
|
+
return not self._task.done()
|
|
82
|
+
|
|
70
83
|
async def stop(self) -> None:
|
|
71
84
|
"""Stop the streaming helper."""
|
|
72
85
|
if self._task.done():
|
|
@@ -91,6 +104,12 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
|
|
|
91
104
|
await sender.send(self._transform(msg))
|
|
92
105
|
except grpc.aio.AioRpcError as err:
|
|
93
106
|
error = err
|
|
107
|
+
if error is None and not self._retry_on_exhausted_stream:
|
|
108
|
+
_logger.info(
|
|
109
|
+
"%s: connection closed, stream exhausted", self._stream_name
|
|
110
|
+
)
|
|
111
|
+
await self._channel.close()
|
|
112
|
+
break
|
|
94
113
|
error_str = f"Error: {error}" if error else "Stream exhausted"
|
|
95
114
|
interval = self._retry_strategy.next_interval()
|
|
96
115
|
if interval is None:
|
|
@@ -10,47 +10,47 @@ frequenz-client-base[dev-flake8,dev-formatting,dev-mkdocs,dev-mypy,dev-noxfile,d
|
|
|
10
10
|
flake8==7.1.1
|
|
11
11
|
flake8-docstrings==1.7.0
|
|
12
12
|
flake8-pyproject==1.2.3
|
|
13
|
-
pydoclint==0.5.
|
|
13
|
+
pydoclint==0.5.9
|
|
14
14
|
pydocstyle==6.3.0
|
|
15
15
|
frequenz-client-base
|
|
16
16
|
|
|
17
17
|
[dev-formatting]
|
|
18
|
-
black==24.
|
|
18
|
+
black==24.10.0
|
|
19
19
|
isort==5.13.2
|
|
20
20
|
|
|
21
21
|
[dev-mkdocs]
|
|
22
|
-
black==24.
|
|
22
|
+
black==24.10.0
|
|
23
23
|
Markdown==3.7
|
|
24
24
|
mike==2.1.3
|
|
25
25
|
mkdocs-gen-files==0.5.0
|
|
26
26
|
mkdocs-literate-nav==0.6.1
|
|
27
|
-
mkdocs-macros-plugin==1.
|
|
28
|
-
mkdocs-material==9.5.
|
|
29
|
-
mkdocstrings[python]==0.
|
|
30
|
-
mkdocstrings-python==1.
|
|
27
|
+
mkdocs-macros-plugin==1.3.7
|
|
28
|
+
mkdocs-material==9.5.43
|
|
29
|
+
mkdocstrings[python]==0.26.2
|
|
30
|
+
mkdocstrings-python==1.12.2
|
|
31
31
|
frequenz-repo-config[lib]==0.10.0
|
|
32
32
|
frequenz-client-base
|
|
33
33
|
|
|
34
34
|
[dev-mypy]
|
|
35
|
-
mypy==1.
|
|
35
|
+
mypy==1.13.0
|
|
36
36
|
types-Markdown==3.7.0.20240822
|
|
37
|
-
types-protobuf==5.
|
|
37
|
+
types-protobuf==5.28.3.20241030
|
|
38
38
|
grpc-stubs==1.53.0.5
|
|
39
39
|
frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]
|
|
40
40
|
|
|
41
41
|
[dev-noxfile]
|
|
42
|
-
nox==2024.
|
|
42
|
+
nox==2024.10.9
|
|
43
43
|
frequenz-repo-config[lib]==0.10.0
|
|
44
44
|
|
|
45
45
|
[dev-pylint]
|
|
46
|
-
pylint==3.
|
|
46
|
+
pylint==3.3.1
|
|
47
47
|
frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]
|
|
48
48
|
|
|
49
49
|
[dev-pytest]
|
|
50
|
-
pytest==8.3.
|
|
50
|
+
pytest==8.3.3
|
|
51
51
|
frequenz-repo-config[extra-lint-examples]==0.10.0
|
|
52
52
|
pytest-mock==3.14.0
|
|
53
53
|
pytest-asyncio==0.24.0
|
|
54
54
|
async-solipsism==0.7
|
|
55
|
-
hypothesis==6.
|
|
55
|
+
hypothesis==6.116.0
|
|
56
56
|
frequenz-client-base
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/__init__.py
RENAMED
|
File without changes
|
{frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/conftest.py
RENAMED
|
File without changes
|
{frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/conversion.py
RENAMED
|
File without changes
|
{frequenz-client-base-0.6.1 → frequenz-client-base-0.8.0}/src/frequenz/client/base/exception.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|