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.69k stars 1.04k forks source link

[Question]: Is OnTick always triggered by the same goroutine? #634

Closed ckcfcc closed 3 months ago

ckcfcc commented 3 months ago

Actions I've taken before I'm here

Questions with details

潘少你好,最近在我在用gnet接kcp,kcp是使用的udp通讯,我现在是在Traffic里创建kcp连接对象然后把收到的数据复制一份扔到kcp中,因为kcp需要外部时钟驱动,所以我用了OnTick作为外部时钟。

我想请教的问题有2个: 1、OnTick是不是一直被一同一个goroutine触发? 2、我在OnTraffic里创建kcp对象并收包是否算阻塞操作,是否需要用ants来开多个goroutine处理?

创建kcp对象不是所有收包都要进行,如果是同一个来源的udp包,只会在首次收到包时创建这个kcp,后面就只是往这个kcp里扔数据了。
OnTick中还有一个功能就是负责检查和处理kcp连接的超时销毁工作

以上就是目前的遇到的问题,因为能力有限,阅读gnet源码后任然不知道结果,望指点

Code snippets (optional)

No response

panjf2000 commented 3 months ago
  1. 在 gnet 中涉及的阻塞操作通常是指可能会让内核将当前线程调离 CPU 并挂起休眠的 I/O 操作,通常是网络 I/O 或者磁盘 I/O,或者是其他可能会被内核阻塞挂起的操作,如果你只是做一些封包/解包的操作,而且期间只会访问内存,那就不是阻塞操作。
gh-translator commented 3 months ago

🤖 Non-English text detected, translating...


  1. Yes
  2. The blocking operations involved in gnet usually refer to I/O operations that may cause the kernel to transfer the current thread away from the CPU and suspend sleep, usually network I/O or disk I/O, or other operations that may be The kernel blocks pending operations. If you just do some packet/unpack operations and only access memory during the process, it is not a blocking operation.
ckcfcc commented 3 months ago

感谢回复,gnet认知+1.

gh-translator commented 3 months ago

🤖 Non-English text detected, translating...


Thanks for the reply, gnet awareness +1.