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

[Feature]: It is necessary to kick out connections that have not had data interaction for a long time. How to delete them all at once? #635

Closed maxbad closed 2 months ago

maxbad commented 2 months ago

Description of new feature

没有找到获取当前全部连接的接口, 怎么实现踢掉长时间没有数据交互的连接呢, 是需要上线存到列表, 然后遍历自己的列表吗

Scenarios for new feature

TCP长连接

Breaking changes or not?

No

Code snippets (optional)

No response

Alternatives for new feature

None.

Additional context (optional)

None.

panjf2000 commented 2 months ago

设置一个小顶堆来存所有空闲连接,以给每一个连接设置一个过期时间,这个时间堆就用这个过期时间来排序,在 OnTick() 中每次取出一个,用过期时间减去当前时间得到一个差值 d 并作为返回值,OnTick() 就会在 d 时间之后再次调用,取出堆顶的空闲连接并销毁。

gh-translator commented 2 months ago

🤖 Non-English text detected, translating...


Set up a small top heap to store all idle connections, and set an expiration time for each connection. This time heap is sorted by this expiration time. Take out one at a time in OnTick(), and use the expiration time minus the current The time gets a difference value d as the return value. OnTick() will be called again after d time, taking out the idle connection on the top of the heap and destroying it.