Skip to content

Commit cc88c46

Browse files
committed
Add timeouts to all api calls
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent fa059ed commit cc88c46

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
## New Features
1212

1313
* `dispatch-cli` supports now the parameter `--type` and `--running` to filter the list of running services by type and status, respectively.
14+
* Every call now has a default timeout of 60 seconds, streams terminate after five minutes. This can be influenced by the two new parameters for`DispatchApiClient.__init__()`:
15+
* `default_timeout: timedelta` (default: 60 seconds)
16+
* `stream_timeout: timedelta` (default: 5 minutes)
1417

1518
## Bug Fixes
1619

src/frequenz/client/dispatch/_client.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
# pylint: enable=no-name-in-module
5050
DEFAULT_DISPATCH_PORT = 443
5151

52+
DEFAULT_GRPC_CALL_TIMEOUT = timedelta(seconds=60)
53+
"""Default gRPC call timeout."""
54+
55+
DEFAULT_GRPC_STREAM_TIMEOUT = timedelta(seconds=5)
56+
"""Default gRPC stream timeout."""
57+
5258

5359
class DispatchApiClient(BaseApiClient[dispatch_pb2_grpc.MicrogridDispatchServiceStub]):
5460
"""Dispatch API client."""
@@ -59,13 +65,17 @@ def __init__(
5965
server_url: str,
6066
key: str,
6167
connect: bool = True,
68+
call_timeout: timedelta | None = None,
69+
stream_timeout: timedelta | None = None,
6270
) -> None:
6371
"""Initialize the client.
6472
6573
Args:
6674
server_url: The URL of the server to connect to.
6775
key: API key to use for authentication.
6876
connect: Whether to connect to the service immediately.
77+
call_timeout: Timeout for gRPC calls, default is 60 seconds.
78+
stream_timeout: Timeout for gRPC streams, default is 5 minutes.
6979
"""
7080
super().__init__(
7181
server_url,
@@ -82,6 +92,11 @@ def __init__(
8292
] = {}
8393
"""A dictionary of streamers, keyed by microgrid_id."""
8494

95+
self._call_timeout = (call_timeout or DEFAULT_GRPC_CALL_TIMEOUT).total_seconds()
96+
self._stream_timeout = (
97+
stream_timeout or DEFAULT_GRPC_STREAM_TIMEOUT
98+
).total_seconds()
99+
85100
@property
86101
def stub(self) -> dispatch_pb2_grpc.MicrogridDispatchServiceAsyncStub:
87102
"""The stub for the service."""
@@ -177,7 +192,9 @@ def to_interval(
177192
while True:
178193
response = await cast(
179194
Awaitable[ListMicrogridDispatchesResponse],
180-
self.stub.ListMicrogridDispatches(request, metadata=self._metadata),
195+
self.stub.ListMicrogridDispatches(
196+
request, metadata=self._metadata, timeout=self._call_timeout
197+
),
181198
)
182199

183200
yield (Dispatch.from_protobuf(dispatch) for dispatch in response.dispatches)
@@ -234,7 +251,9 @@ def _get_stream(
234251
stream_method=lambda: cast(
235252
AsyncIterator[StreamMicrogridDispatchesResponse],
236253
self.stub.StreamMicrogridDispatches(
237-
request, metadata=self._metadata
254+
request,
255+
metadata=self._metadata,
256+
timeout=self._stream_timeout,
238257
),
239258
),
240259
transform=DispatchEvent.from_protobuf,
@@ -303,7 +322,9 @@ async def create( # pylint: disable=too-many-positional-arguments
303322
response = await cast(
304323
Awaitable[CreateMicrogridDispatchResponse],
305324
self.stub.CreateMicrogridDispatch(
306-
request.to_protobuf(), metadata=self._metadata
325+
request.to_protobuf(),
326+
metadata=self._metadata,
327+
timeout=self._call_timeout,
307328
),
308329
)
309330

@@ -394,7 +415,9 @@ async def update(
394415

395416
response = await cast(
396417
Awaitable[UpdateMicrogridDispatchResponse],
397-
self.stub.UpdateMicrogridDispatch(msg, metadata=self._metadata),
418+
self.stub.UpdateMicrogridDispatch(
419+
msg, metadata=self._metadata, timeout=self._call_timeout
420+
),
398421
)
399422

400423
return Dispatch.from_protobuf(response.dispatch)
@@ -414,7 +437,9 @@ async def get(self, *, microgrid_id: int, dispatch_id: int) -> Dispatch:
414437
)
415438
response = await cast(
416439
Awaitable[GetMicrogridDispatchResponse],
417-
self.stub.GetMicrogridDispatch(request, metadata=self._metadata),
440+
self.stub.GetMicrogridDispatch(
441+
request, metadata=self._metadata, timeout=self._call_timeout
442+
),
418443
)
419444
return Dispatch.from_protobuf(response.dispatch)
420445

@@ -430,5 +455,7 @@ async def delete(self, *, microgrid_id: int, dispatch_id: int) -> None:
430455
)
431456
await cast(
432457
Awaitable[None],
433-
self.stub.DeleteMicrogridDispatch(request, metadata=self._metadata),
458+
self.stub.DeleteMicrogridDispatch(
459+
request, metadata=self._metadata, timeout=self._call_timeout
460+
),
434461
)

0 commit comments

Comments
 (0)