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

Special treatment for the first package #601

Closed skyw4code closed 6 months ago

skyw4code commented 6 months ago

遇到了不少场景(如:支持PROXY Protocol协议),都需要在连接建立后对首个包进行特殊处理,当然放在协议解析器里面去处理是可以的,但很明显这样每次数据包来的时候都得去判断一下,很不合理,解析器应该只关心业务相关内容才对!!!

翻了一下Issues,发现之前已经有提过这个了, https://github.com/panjf2000/gnet/issues/231#issuecomment-883857897 https://github.com/panjf2000/gnet/issues/231#issuecomment-883990539 pan大佬也说了解决的思路:应该有一个专门的 handler,收到第一个包的时候调用一下就行。。。 这么久过去了,有计划支持吗?

panjf2000 commented 6 months ago

这个问题主要是 gnet v1 里出现的,因为 v1 有编码器和解码器的功能,导致解码器在识别第一个特殊包的场景中不是很灵活,但是 gnet v2 移除了编码器和解码器,现在整个数据解析逻辑都要放在 OnTraffic() 里了,更加灵活,就没必要再设置一个所谓的 handler 了,用户自己判断一下第一个包然后处理就行了。

gh-translator commented 6 months ago

🤖 Non-English text detected, translating...


This problem mainly occurs in gnet v1, because v1 has the functions of encoder and decoder, resulting in the decoder not being very flexible in the scenario of identifying the first special packet, but gnet v2 removes the encoder and decoder. Now the entire data parsing logic will be placed in OnTraffic(), which is more flexible. There is no need to set up a so-called handler. The user can just judge the first package and process it.

skyw4code commented 6 months ago

这个问题主要是 gnet v1 里出现的,因为 v1 有编码器和解码器的功能,导致解码器在识别第一个特殊包的场景中不是很灵活,但是 gnet v2 移除了编码器和解码器,现在整个数据解析逻辑都要放在 OnTraffic() 里了,更加灵活,就没必要再设置一个所谓的 handler 了,用户自己判断一下第一个包然后处理就行了。

是~~~ 其实纠结的是后面每次数据包来的时候也得都去判断一下,这个很难受...

gh-translator commented 6 months ago

🤖 Non-English text detected, translating...


This problem mainly occurs in gnet v1, because v1 has the functions of encoder and decoder, resulting in the decoder not being very flexible in the scenario of identifying the first special packet, but gnet v2 removes the encoder and decoder , now the entire data parsing logic must be placed in OnTraffic(), which is more flexible. There is no need to set up a so-called handler. The user can just judge the first package and then process it.

Yes~~~ In fact, the struggle is that every time a data packet comes, it has to be judged. This is very uncomfortable...

panjf2000 commented 6 months ago

不需要每次都判断,你用 sync.Once 或者更简单地在 c.Context 里设置一个 bool,第一个包解析完之后就设个标志,下次判断一下这个标志,跳过就行了。

gh-translator commented 6 months ago

🤖 Non-English text detected, translating...


You don’t need to judge every time. You can use sync.Once or simply set a bool in c.Context. After the first package is parsed, set a flag. Next time, judge this flag and skip it. That's it.

skyw4code commented 6 months ago

下次判断一下这个标志,跳过就行了。

emmm... 我说的就是这个判断,这也是判断啊...

gh-translator commented 6 months ago

🤖 Non-English text detected, translating...


Judge this sign next time and just skip it.