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

[Pyamqp] Possible solutions for network disruption using async websocket #26856

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from .._platform import KNOWN_TCP_OPTS, SOL_TCP
from .._encode import encode_frame
from .._decode import decode_frame, decode_empty_frame
from ..constants import TLS_HEADER_FRAME, WEBSOCKET_PORT, AMQP_WS_SUBPROTOCOL
from ..constants import DEFAULT_WEBSOCKET_HEARTBEAT_SECONDS, TLS_HEADER_FRAME, WEBSOCKET_PORT, AMQP_WS_SUBPROTOCOL
from .._transport import (
AMQP_FRAME,
get_errno,
Expand Down Expand Up @@ -478,6 +478,14 @@ async def connect(self):
parsed_url = urlsplit(url)
url = f"{parsed_url.scheme}://{parsed_url.netloc}:{self.port}{parsed_url.path}"

# Enabling heartbeat that sends a ping message every n seconds and waits for pong response.
# if pong response is not received then close connection. This raises an error when trying
# to communicate with the websocket which is no longer active.
# We are waiting a bug fix in aiohttp for these 2 bugs where aiohttp ws might hang on network disconnect
# and the heartbeat mechanism helps mitigate these two.
# https://github.com/aio-libs/aiohttp/pull/5860
# https://github.com/aio-libs/aiohttp/issues/2309

self.ws = await self.session.ws_connect(
url=url,
timeout=self._connect_timeout,
Expand All @@ -486,6 +494,7 @@ async def connect(self):
proxy=http_proxy_host,
proxy_auth=http_proxy_auth,
ssl=self.sslopts,
heartbeat=DEFAULT_WEBSOCKET_HEARTBEAT_SECONDS,
)
self.connected = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
AUTH_TYPE_SASL_PLAIN = "AUTH_SASL_PLAIN"
AUTH_TYPE_CBS = "AUTH_CBS"

DEFAULT_WEBSOCKET_HEARTBEAT_SECONDS = 10


class ConnectionState(Enum):
#: In this state a Connection exists, but nothing has been sent or received. This is the state an
Expand Down