Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client.stream: Raise exception on connection loss #94

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Upgrading

<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
* `Client.stream()` will now raise an Exception when the connection is lost.

## New Features

Expand Down
7 changes: 7 additions & 0 deletions src/frequenz/client/dispatch/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from frequenz.client.base.channel import ChannelOptions, SslOptions
from frequenz.client.base.client import BaseApiClient
from frequenz.client.base.conversion import to_timestamp
from frequenz.client.base.retry import LinearBackoff
from frequenz.client.base.streaming import GrpcStreamBroadcaster

from ._internal_types import DispatchCreateRequest
Expand Down Expand Up @@ -208,6 +209,11 @@ def _get_stream(
) -> GrpcStreamBroadcaster[StreamMicrogridDispatchesResponse, DispatchEvent]:
"""Get an instance to the streaming helper."""
broadcaster = self.streams.get(microgrid_id)
# pylint: disable=protected-access
if broadcaster is not None and broadcaster._channel.is_closed:
Comment on lines +212 to +213
Copy link
Contributor

@llucax llucax Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not nice. We should add a broadcaster.is_running() or something. I think there is no one day that goes by without me thinking "we need to implement this using core.asyncio.Service". 😬

After I finish with the microgrid API client... 🙏

# pylint: enable=protected-access
del self.streams[microgrid_id]
broadcaster = None
if broadcaster is None:
request = StreamMicrogridDispatchesRequest(microgrid_id=microgrid_id)
broadcaster = GrpcStreamBroadcaster(
Expand All @@ -219,6 +225,7 @@ def _get_stream(
),
),
transform=DispatchEvent.from_protobuf,
retry_strategy=LinearBackoff(interval=1, limit=0),
)
self.streams[microgrid_id] = broadcaster

Expand Down
Loading