mirror of
https://github.com/skywind3000/kcp.git
synced 2025-01-13 20:10:12 +08:00
Updated EN_KCP Best Practice (markdown)
parent
505b896a04
commit
772940e354
@ -1,16 +1,17 @@
|
||||
# 最佳实践
|
||||
#### 内存分配器
|
||||
# BestPractice
|
||||
|
||||
默认KCP协议使用 malloc/free进行内存分配释放,如果应用层接管了内存分配,可以用ikcp_allocator来设置新的内存分配器,注意要在一开始设置:
|
||||
#### Memory Management
|
||||
|
||||
KCP use `malloc/free` for memory management, user can use `ikcp_allocator` to use other solution. Remember to setup it at the first line of `main()`:
|
||||
|
||||
> ikcp_allocator(my_new_malloc, my_new_free);
|
||||
|
||||
|
||||
#### 前向纠错注意
|
||||
#### Reduce Redundancy for FEC
|
||||
|
||||
为了进一步提高传输速度,下层协议也许会使用前向纠错技术。需要注意,前向纠错会根据冗余信息解出原始数据包。相同的原始数据包不要两次input到KCP,否则将会导致 kcp以为对方重发了,这样会产生更多的ack占用额外带宽。
|
||||
As concrete transport is specified by application, which integrate KCP(For more information, read [Usage](EN_KCP-Basic-Usage)). When FEC is used by application-level, it may contains some redundancy packets. User should feed KCP(by `ikcp_input`) by the packets without redundancy, that is, if received packets `p0,p1,p0,p2`, user should feed KCP `p0,p1,p2`, rather than `p0,p1,p0,p2`, the duplicated `p0` should never feed to KCP, or there maybe more ACK packets.
|
||||
|
||||
比如下层协议使用最简单的冗余包:单个数据包除了自己外,还会重复存储一次上一个数据包,以及上上一个数据包的内容:
|
||||
For example, the FEC in application use triple redundancy:
|
||||
|
||||
```cpp
|
||||
Fn = (Pn, Pn-1, Pn-2)
|
||||
@ -21,11 +22,10 @@ P2 = (2, 1, 0)
|
||||
P3 = (3, 2, 1)
|
||||
```
|
||||
|
||||
这样几个包发送出去,接收方对于单个原始包都可能被解出3次来(后面两个包任然会重复该包内容),那么这里需要记录一下,一个下层数据包只会input给kcp一次,避免过多重复ack带来的浪费。
|
||||
The recever can recover previous two packet from one packet. User should reduce the duplicated packets then feed to KCP.
|
||||
|
||||
#### 管理大规模连接
|
||||
#### Efficient Update
|
||||
|
||||
如果需要同时管理大规模的 KCP连接(比如大于3000个),比如你正在实现一套类 epoll的机制,那么为了避免每秒钟对每个连接调用大量的调用 ikcp_update,我们可以使用 ikcp_check 来大大减少 ikcp_update调用的次数。 ikcp_check返回值会告诉你需要在什么时间点再次调用 ikcp_update(如果中途没有 ikcp_send, ikcp_input的话,否则中途调用了 ikcp_send, ikcp_input的话,需要在下一次interval时调用 update)
|
||||
For server-side application, for example linux application server, `select` or `epool` is often used in `non-blocking` `asnc` architecture, to serve over `10k` clients. For these use scenarios, there are usually huge connections(>3k) to serve, it may cause performance issue when use `ikcp_update` for each connection to upddate, user should better use `ikcp_check` to replace `ikcp_update`. Unlike `ikcp_update` to update KCP every N ms, `ikcp_check` tells us the exactly time to update the KCP(Only need to update when called `ikcp_send` or `ikcp_input`).
|
||||
|
||||
标准顺序是每次调用了 ikcp_update后,使用 ikcp_check决定下次什么时间点再次调用 ikcp_update,而如果中途发生了 ikcp_send, ikcp_input 的话,在下一轮 interval 立马调用 ikcp_update和 ikcp_check。 使用该方法,原来在处理2000个 kcp连接且每
|
||||
个连接每10ms调用一次update,改为 check机制后,cpu从 60%降低到 15%。
|
||||
For example, user can use `ikcp_udpate` at the first time, then use `ikcp_check` to get the next time to call `ikcp_update`. It's reported that the CPU usage descreate form 60% to 15% when use `ikcp_check`.
|
||||
|
Loading…
Reference in New Issue
Block a user