Comments (11)
两个问题:
- 你确定对端已经把消息成功发送出去了?
- 你确定
OnTraffic
没有正常被调用?
你在 OnTraffic
里打印一下日志看看?
from gnet.
这个很确定,抓包看了,另外在 server 端做了点测试,发送 ack包 以后通过远程断点 hang 住后面流程不发 close 信号, client 这边就一切正常。。。另外还做了一个测试在 OnClose 的时候从新 reconnect 链接,基本上重试两三次可以成功,但是这么做在 clinet 数量多的时候怕重试太多对 sever 端造成影响。。
from gnet.
按照 TCP 协议,这种情况理论上不应该发生的。你能弄一个简单的、可以复现这个问题的 demo 代码 (客户端和服务端)然后贴在这里吗?我本地调试看看是哪里的问题,这种问题以前也没人遇到过。
from gnet.
对是的,按照 TCP 协议来说不应该有这个问题。代码您稍等一下,我看看怎么撸一个轻量点的,晚些时候放出来
from gnet.
我大概写了一个逻辑,server 端回写包后立刻 close,OnTraffic 是没有打印响应请求的。实际情况是我的 server 端是 java 的 netty 写的,这两个操作我不太清楚是不是等效的
package jmtpclient
import (
"bufio"
"fmt"
"github.com/panjf2000/gnet/v2"
"net"
"testing"
"time"
)
func TestTcpClient(t *testing.T) {
listen, err := net.Listen("tcp", "127.0.0.1:9999")
if err != nil {
fmt.Println("Listen() failed, err: ", err)
return
}
go func() {
for {
conn, err := listen.Accept() // 监听客户端的连接请求
if err != nil {
fmt.Println("Accept() failed, err: ", err)
continue
}
go process(conn) // 启动一个goroutine来处理客户端的连接请求
}
}()
cli, err := gnet.NewClient(&GNetClient{}, gnet.WithMulticore(true))
defer cli.Stop()
if err != nil {
t.Error(err)
}
cli.Start()
_, err = cli.Dial("tcp", "127.0.0.1:9999")
if err != nil {
t.Error(err)
}
time.Sleep(10 * time.Second)
}
func process(conn net.Conn) {
defer conn.Close() // 关闭连接
for {
reader := bufio.NewReader(conn)
var buf [128]byte
n, err := reader.Read(buf[:]) // 读取数据
if err != nil {
fmt.Println("read from client failed, err: ", err)
break
}
recvStr := string(buf[:n])
fmt.Println("收到Client端发来的数据:", recvStr)
conn.Write([]byte(recvStr)) // 发送数据
conn.Close()
}
}
type GNetClient struct {
gnet.BuiltinEventEngine
}
func (G GNetClient) OnBoot(eng gnet.Engine) (action gnet.Action) {
return gnet.None
}
func (G GNetClient) OnOpen(c gnet.Conn) (out []byte, action gnet.Action) {
return []byte("test"), gnet.None
}
func (G GNetClient) OnClose(c gnet.Conn, err error) (action gnet.Action) {
return gnet.Close
}
func (G GNetClient) OnTraffic(c gnet.Conn) (action gnet.Action) {
buf, err := c.Next(4)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("======================" + string(buf))
}
return gnet.None
}
from gnet.
不小心关错了。。。
from gnet.
好的,谢谢提供 demo。我等会儿 debug 一下看看。
from gnet.
我自己测试了一下,在 macOS 上确实有这个问题,但是在 Linux 是没问题的,你的客户端是部署在 mac 上的?
@xspren
from gnet.
对,是在 mac 上测的。。。单测有问题没上生产,我找个 linux 环境试一下
from gnet.
我已经定位到原因了,等这个 PR #531 合入后应该就能修复在 BSD 上的这个 bug 了。
Thanks for the bug report!
from gnet.
好的,多谢
from gnet.
Related Issues (20)
- [Question]: What is the maximum number of cores tested by anyone? I just tested 192 cores and it's better than evio etc but... HOT 7
- [Question]: Will the OnTraffic function be called concurrently? HOT 1
- [Question]: Validate the approach of implementing persistent TCP connections. HOT 1
- [Bug]: Calling Dial() in version 2.4 of Windows 10 will block HOT 4
- [Question]: I would like to verify whether this problem in the screenshot will cause the memory of the service to slowly increase. HOT 1
- [Feature]: support edge-triggered I/O
- [Question]: Is the netpoll v1 version defaulting to horizontal triggering, and the V2 version defaulting to edge triggering? HOT 15
- [Bug]: fatal error: concurrent map read and map write HOT 1
- Inconsistent behaviors with SO_REUSEPORT on Linux and *BSD HOT 7
- Don't use SO_REUSEPORT on Unix domain sockets HOT 1
- Don't disable SO_REUSEPORT on DragonFlyBSD HOT 1
- [Question]: gnet client works not as expected even tho i did it according to the api ref HOT 1
- [Bug]: GNET client not working as expected HOT 4
- [Bug]: example gnet for echo tcp uses too much memory HOT 7
- [Bug]: Read returns io.ErrShortBuffer on closed connection HOT 7
- Special treatment for the first package HOT 8
- [Question]: How to shut down the started tcp server and turn off port listening? HOT 2
- [Question]: When using io.CopyBuffer(), do you need to call Flush to send the data? HOT 2
- [Bug]: Discard() panic HOT 17
- [Feature]: now tls is possible, when can we have io_uring?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gnet.