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

bugfix: solving webSocket performance issues through select, close #346 #413

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Changes from 1 commit
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
19 changes: 12 additions & 7 deletions curl_cffi/requests/websockets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import asyncio
import select
import struct
from enum import IntEnum
from typing import Callable, Optional, Tuple

from ..const import CurlECode, CurlWsFlag
from .._wrapper import ffi, lib
from ..const import CurlECode, CurlWsFlag, CurlInfo
from ..curl import CurlError

ON_MESSAGE_T = Callable[["WebSocket", bytes], None]
Expand Down Expand Up @@ -64,14 +66,17 @@ def recv(self) -> Tuple[bytes, int]:
"""
chunks = []
flags = 0
# TODO use select here
sock_fd = ffi.new("long*")
lib.curl_easy_getinfo(self.curl._curl, CurlInfo.ACTIVESOCKET, sock_fd)
lexiforest marked this conversation as resolved.
Show resolved Hide resolved
while True:
try:
chunk, frame = self.curl.ws_recv()
flags = frame.flags
chunks.append(chunk)
if frame.bytesleft == 0 and flags & CurlWsFlag.CONT == 0:
break
rlist, _, _ = select.select([sock_fd[0]], [], [], 5.0)
if rlist:
chunk, frame = self.curl.ws_recv()
flags = frame.flags
chunks.append(chunk)
if frame.bytesleft == 0 and flags & CurlWsFlag.CONT == 0:
break
except CurlError as e:
if e.code == CurlECode.AGAIN:
pass
Expand Down
Loading