libhv icon indicating copy to clipboard operation
libhv copied to clipboard

当发送数据过多堆积,应该如何处理

Open ghost opened this issue 4 months ago • 4 comments

Image

ghost avatar Aug 29 '25 02:08 ghost

发送大数据时应该做流控,通过onWriteComplete监听写完成事件,在可写时再发送下一帧数据。

ithewei avatar Sep 01 '25 03:09 ithewei

onWriteComplete没找到~ 我是用websocket,目前是不走hv的fragment send接口, 自己加延时控制,勉强能用,有点丑陋

// 逻辑拆包 const char* ptr = msg.data.data(); size_t remain = msg.data.size(); { int snd = GetSendBufferSize(); LarkDebug("WSClient begin chunked send total={} binary={} logicChunk={} threshold={} sndbuf={} autosize={} queue_pending={}", remain, msg.binary, _logicalChunkSize, _asyncThreshold, snd, _autoSizing, _queue.size()); } bool firstFrame = true; while (remain > 0) { if (_autoSizing) RecalcSafeSizes(); // 选取帧大小:受 _logicalChunkSize 限制并且 <= 65535 size_t frameSize = remain > _logicalChunkSize ? _logicalChunkSize : remain; if (frameSize > 65535) frameSize = 65535; // 强制上限以避免触发内部再次分片 bool last = frameSize == remain; Debug("WSClient chunk send start total={} remain={} frame={} last={} first={} logicChunk={} threshold={} fd_chunkDelay={}ms", msg.data.size(), remain, frameSize, last, firstFrame, _logicalChunkSize, _asyncThreshold, _chunkDelayMs); ws_opcode opcode = firstFrame ? (msg.binary ? WS_OPCODE_BINARY : WS_OPCODE_TEXT) : WS_OPCODE_CONTINUE; int ret = channel ? channel->send(ptr, (int)frameSize, opcode, last) : -1; if (ret < 0) { Error("Async send failed ret={} frameSize={} remain={} total={}", ret, frameSize, remain, msg.data.size()); // 缩小逻辑块尺寸重试 (退出当前消息,后续消息可能使用缩小后的尺寸) _logicalChunkSize = std::max(_logicalChunkSize / 2, _minLogic); break; } Debug("WSClient chunk send done bytes={} next_remain={}", frameSize, remain - frameSize); ptr += frameSize; remain -= frameSize; firstFrame = false; if (!last && _chunkDelayMs > 0) { std::this_thread::sleep_for(std::chrono::milliseconds(_chunkDelayMs)); } }`

ghost avatar Sep 01 '25 03:09 ghost

websocket服务端吗,可以设置channel->onwrite监听写完成事件

ithewei avatar Sep 01 '25 11:09 ithewei

websocket服务端吗,可以设置channel->onwrite监听写完成事件

websocket 服务端和客户端 ,好的 我先按您说的事实, 是在onwrite结束后 再写下一帧呗

ghost avatar Sep 03 '25 07:09 ghost