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]: client blocks when dialing new connections #629

Closed zhao890516 closed 4 months ago

zhao890516 commented 4 months ago

Actions I've taken before I'm here

Questions with details

我是用一个client对象连接了两个不同的服务器,在连接第二个时在如下位置被阻塞住,请问这个openChan调用时机是什么?阻塞在这里可能原因是什么? image

Code snippets (optional)

No response

panjf2000 commented 4 months ago

请提供能够复现这个问题的代码。

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


Please provide code that reproduces this issue.

zhao890516 commented 4 months ago

image image image image

panjf2000 commented 4 months ago

请提供操作系统和版本、gnet 版本、Go 版本。

下次这种问题不要用这个 issue 模板,这个是用来问一些通用的问题的,很容易缺失很多必要的信息,如果使用过程中遇到问题用这个模板

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


Please provide operating system and version, gnet version, Go version.

Don’t use this issue template next time for this kind of question. This is used to ask some general questions. It is easy to miss a lot of necessary information. If you encounter problems during use, use [this template](https://github.com /panjf2000/gnet/issues/new?assignees=panjf2000&labels=bug&projects=&template=bug-report.yaml&title=%5BBug%5D%3A+)

zhao890516 commented 4 months ago

好的。 系统:Linux ubuntu 5.15.0-113-generic #123~20.04.1-Ubuntu SMP Wed Jun 12 17:33:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux gnet版本:github.com/panjf2000/gnet/v2 v2.5.5 go version:go1.22.3 linux/amd64

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


OK System: Linux ubuntu 5.15.0-113-generic #123~20.04.1-Ubuntu SMP Wed Jun 12 17:33:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux gnet version:github.com/panjf2000/gnet/v2 v2.5.5 go version: go1.22.3 linux/amd64

panjf2000 commented 4 months ago

你那个 ConnectToCouplerByGnet() 是在哪里调用的?要注意不能再 event loop 所在的 goroutine 里调用,否则就会阻塞。因为 gnet client 是单 event loop 的实现。

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


Where did you call ConnectToCouplerByGnet()? Be careful not to call it in the goroutine where the event loop is located, otherwise it will block. Because gnet client is an implementation of a single event loop.

zhao890516 commented 4 months ago

ConnectToCouplerByGnet() 这个是另一个线程里边

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


ConnectToCouplerByGnet() This is in another thread

zhao890516 commented 4 months ago

现在的情况是,我尝试连接的对端是OPEN-SSH服务器没问题,当连接的对端是HTTP服务器时,这里会在 image 这段完成之前先收到一条715字节的数据,然后连接就被阻塞了

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


The current situation is that the peer I am trying to connect to is an OPEN-SSH server, no problem. When the peer to connect is an HTTP server, here will be image Before this section is completed, a 715-byte data is received, and then the connection is blocked.

panjf2000 commented 4 months ago

是不是其他回调函数里有阻塞的逻辑?比如 OnOpen 或者 OnClose,或者 AsyncWrite 的回调函数,这些回调里如果阻塞了也会导致 Dial 阻塞。

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


Is there blocking logic in other callback functions? For example, OnOpen or OnClose, or AsyncWrite's callback function, if blocked in these callbacks, it will also cause Dial to block.

zhao890516 commented 4 months ago

Desktop.zip 没有阻塞的逻辑。 这里是完整的代码,里边有个测试方法 image 写一个简单的服务端 image 这样子稳定复现 下边是日志,两次连接,只有一次成功 image

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


Desktop.zip There is no blocking logic. Here is the complete code, with a test method inside image Write a simple server image This is a stable recurrence Below is the log. Two connections were made, only one was successful. image

zhao890516 commented 4 months ago

我解决了 ,犯了个低级错误,datachan忘记设容量,导致OnTraffic阻塞了,多谢你的帮助!

gh-translator commented 4 months ago

🤖 Non-English text detected, translating...


I solved it. I made a simple mistake. Datachan forgot to set the capacity, which caused OnTraffic to be blocked. Thank you for your help!