Skip to content

Commit

Permalink
solving WebSocket performance issues through select
Browse files Browse the repository at this point in the history
  • Loading branch information
usedev committed Oct 19, 2024
1 parent 8cdf5a8 commit dc1122e
Showing 1 changed file with 12 additions and 7 deletions.
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)
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

0 comments on commit dc1122e

Please sign in to comment.