Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #256 from quantmind/ls-ws
Browse files Browse the repository at this point in the history
Websocket fixes
  • Loading branch information
lsbardel authored Jan 3, 2021
2 parents 3155d1d + 1fb1d8c commit 25b65f8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion openapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Minimal OpenAPI asynchronous server application"""

__version__ = "2.1.0"
__version__ = "2.1.1"
11 changes: 11 additions & 0 deletions openapi/ws/errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import asyncio


class CannotSubscribe(RuntimeError):
"""Raised by a :class:`.ServiceManager`.
Expand All @@ -12,3 +15,11 @@ class ChannelCallbackError(RuntimeError):

class CannotPublish(RuntimeError):
"""Raised when not possible to publish event into channels"""


CONNECTION_ERRORS = (
asyncio.CancelledError,
asyncio.TimeoutError,
RuntimeError,
ConnectionResetError,
)
6 changes: 3 additions & 3 deletions openapi/ws/path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import hashlib
import logging
import time
Expand All @@ -12,6 +11,7 @@
from .. import json
from ..data.validate import ValidationErrors, validated_schema
from ..utils import compact
from .errors import CONNECTION_ERRORS
from .manager import SocketsManager, Websocket

logger = logging.getLogger("openapi.ws")
Expand Down Expand Up @@ -66,8 +66,8 @@ async def get(self):
async for msg in response:
if msg.type == web.WSMsgType.TEXT:
await self.on_message(msg)
except (asyncio.CancelledError, asyncio.TimeoutError, RuntimeError):
pass
except CONNECTION_ERRORS:
logger.info("lost connection with websocket %s", self)
finally:
self.sockets.remove(self)
return response
Expand Down
4 changes: 2 additions & 2 deletions openapi/ws/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ..data.validate import ValidationErrors
from ..utils import cached_property
from .channel import logger
from .errors import CannotPublish, ChannelCallbackError
from .errors import CONNECTION_ERRORS, CannotPublish, ChannelCallbackError
from .rpc import ws_rpc

if TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -44,7 +44,7 @@ async def __call__(self, channel: str, match: str, data: Any) -> None:
if hasattr(data, "__call__"):
data = data()
await self.ws.write(dict(channel=channel, event=match, data=data))
except RuntimeError:
except CONNECTION_ERRORS:
logger.info("lost connection with %s", self)
await self.ws.close()
raise ChannelCallbackError
Expand Down

0 comments on commit 25b65f8

Please sign in to comment.