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

refactor eventsubws subscription error handling to not error on reconnect #439

Merged
merged 3 commits into from
Mar 15, 2024
Merged
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
24 changes: 16 additions & 8 deletions twitchio/ext/eventsub/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from twitchio import PartialUser, Unauthorized, HTTPException

if TYPE_CHECKING:
from typing_extensions import Literal
from twitchio import Client

logger = logging.getLogger("twitchio.ext.eventsub.ws")
Expand All @@ -32,7 +33,7 @@ def __init__(self, event_type: Tuple[str, int, Type[models.EventData]], conditio
self.token = token
self.subscription_id: Optional[str] = None
self.cost: Optional[int] = None
self.created: asyncio.Future[Tuple[bool, int]] | None = asyncio.Future()
self.created: asyncio.Future[Tuple[Literal[False], int] | Tuple[Literal[True], None]] | None = asyncio.Future()
AbstractUmbra marked this conversation as resolved.
Show resolved Hide resolved


_T = TypeVar("_T")
Expand Down Expand Up @@ -117,18 +118,25 @@ async def _subscribe(self, obj: _Subscription) -> dict | None:
try:
resp = await self._http.create_websocket_subscription(obj.event, obj.condition, self._session_id, obj.token)
except HTTPException as e:
assert obj.created
obj.created.set_result((False, e.status)) # type: ignore
if obj.created:
obj.created.set_result((False, e.status))

else:
logger.error(
"An error (%s %s) occurred while attempting to resubscribe to an event on reconnect: %s",
e.status,
e.reason,
e.message,
)

return None

else:
assert obj.created
obj.created.set_result((True, None)) # type: ignore
if obj.created:
obj.created.set_result((True, None))

data = resp["data"][0]
cost = data["cost"]
self.remaining_slots = resp["max_total_cost"] - resp["total_cost"]
obj.cost = cost
obj.cost = data["cost"]

return data

Expand Down
Loading