panjf2000 / gnet

🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.
https://gnet.host
Apache License 2.0
9.7k stars 1.04k forks source link

[Question]: Does udp buffer support on-demand allocation? #628

Closed Flash-LHR closed 4 months ago

Flash-LHR commented 4 months ago

Actions I've taken before I'm here

Questions with details

服务端启动一个udp服务,每次请求需要60s。 客户端以 5000qps 向服务端请求,服务端需要为每个请求分配 64KB 大小的缓冲区,但是99%的客户端请求大小只有1KB,剩下1%的请求大小是64KB。 基于以上场景,服务端需要分配的内存大小是 $60 \times 5000 \times 64$ KB,大约 1TB!

请问udp buffer是否支持按需分配,或者上述场景该如何解决?

Code snippets (optional)

No response

panjf2000 commented 4 months ago

因为 UDP 本身是数据报的形式而非像 TCP 那样的数据流,所以 gnet 的 UDP 实现没有内部 buffer,每次 OnTraffic() 都必须把数据消费完,不然数据会被自动丢弃。至于你的场景,我不明白你为什么要为一个请求分配 64KB 的内存,这明显是一种浪费,如果只是为了读取请求而需要分配内存,更合理的做法是多个请求共享一块内存,只要保证多个请求的数据不要混在一起就行了。

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


Because UDP itself is in the form of a datagram rather than a data stream like TCP, gnet's UDP implementation has no internal buffer, and the data must be consumed every time OnTraffic() is called, otherwise the data will be automatically discarded. As for your scenario, I don't understand why you want to allocate 64KB of memory for a request. This is obviously a waste. If you only need to allocate memory for read requests, a more reasonable approach is to share a piece of memory with multiple requests, as long as Just make sure that the data from multiple requests are not mixed together.