This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make legacy ensemble members connect through websocket
- Loading branch information
Showing
9 changed files
with
254 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import websockets | ||
from websockets import ConnectionClosedOK | ||
import asyncio | ||
import cloudevents | ||
from websockets.exceptions import ConnectionClosed | ||
|
||
|
||
class Client: | ||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, exc_traceback): | ||
if self.websocket is not None: | ||
self.loop.run_until_complete(self.websocket.close()) | ||
self.loop.close() | ||
|
||
def __init__(self, url, max_retries=10, timeout_multiplier=5): | ||
if url is None: | ||
raise ValueError("url was None") | ||
self.url = url | ||
self._max_retries = max_retries | ||
self._timeout_multiplier = timeout_multiplier | ||
self.websocket = None | ||
self.loop = asyncio.new_event_loop() | ||
|
||
async def get_websocket(self): | ||
return await websockets.connect(self.url) | ||
|
||
async def _send(self, msg): | ||
for retry in range(self._max_retries + 1): | ||
try: | ||
if self.websocket is None: | ||
self.websocket = await self.get_websocket() | ||
await self.websocket.send(msg) | ||
return | ||
except ConnectionClosedOK: | ||
# Connection was closed no point in trying to send more messages | ||
raise | ||
except (ConnectionClosed, ConnectionRefusedError, OSError): | ||
if retry == self._max_retries: | ||
raise | ||
await asyncio.sleep(0.2 + self._timeout_multiplier * retry) | ||
self.websocket = None | ||
|
||
def send(self, msg): | ||
self.loop.run_until_complete(self._send(msg)) | ||
|
||
def send_event(self, ev_type, ev_source, ev_data=None): | ||
if ev_data is None: | ||
ev_data = {} | ||
event = cloudevents.http.CloudEvent( | ||
{ | ||
"type": ev_type, | ||
"source": ev_source, | ||
"datacontenttype": "application/json", | ||
}, | ||
ev_data, | ||
) | ||
self.send(cloudevents.http.to_json(event).decode()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ decorator | |
pylint | ||
black | ||
click | ||
pytest-asyncio |
Oops, something went wrong.