Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: When using io.CopyBuffer(), do you need to call Flush to send the data? #603

Closed
3 tasks done
KM-SF opened this issue May 22, 2024 · 2 comments
Closed
3 tasks done
Labels
help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested

Comments

@KM-SF
Copy link

KM-SF commented May 22, 2024

Actions I've taken before I'm here

  • I've thoroughly read the documentations about this problem but still have no answer.
  • I've searched the Github Issues/Discussions but didn't find any similar problems that have been solved.
  • I've searched the internet for this problem but didn't find anything helpful.

Questions with details

  • 使用的v2.5.2的版本
  • 服务端的使用默认的配置启动
  • 场景:在简单的kv系统中,服务端接收到请求-> 处理完请求 -> 将结果发送回去这一个流程。
  • 分析:我使用的是io.CopyBuffer(gnet.conn, RespReader)这个方式,将结果拷贝到gnet.conn中,但是这个方式好像不会发送数据到客户端,客户端用io.ReadFull会一直卡住,需要手动调用一次Flush才会发送数据。跟了一下代码,好像只是简单的把数据拷贝到buffer中。如果我直接调用Write的话,会发送数据到客户端
  • 问题:如果使用io.CopyBuffer这个情况,是需要自己收到调用Flush是吗?

Code snippets (optional)

// Server:

func (s *Service) OnTraffic(c gnet.Conn) (action gnet.Action) {
    args = c.Context().(KVGetRequest)
    args.Decocde(c)

    go func(){
        respReader := KVGet(args)
        io.CopyBuffer(c,respReader,buff) // c.Write(resp) 这个方式不会卡住
        // c.Flush()  如果不手动flush,客户端会一直卡住
    }
}

// Client

func (c *Client) KVGet(req KVGetRequest) {
    c := net.Dial("tcp",p.Addr)
    c.Write(req.Bytes())
    io.ReadFull(conn,respBuf)
}
@KM-SF KM-SF added help wanted Extra attention is needed question Further information is requested labels May 22, 2024
@gh-translator gh-translator changed the title [Question]: 使用io.CopyBuffer(),后需要调用Flush才会将数据发送出去? [Question]: When using io.CopyBuffer(), do you need to call Flush to send the data? May 22, 2024
@panjf2000
Copy link
Owner

gnet.Conn 大部分的方法都不是并发安全的,不能放到新的 goroutine 里执行,仔细看文档:https://pkg.go.dev/github.com/panjf2000/gnet/v2#Conn

@gh-translator
Copy link
Collaborator

🤖 Non-English text detected, translating...


Most methods of gnet.Conn are not concurrency-safe and cannot be executed in new goroutines. Read the documentation carefully: https://pkg.go.dev/github.com/panjf2000/gnet/v2#Conn

@panjf2000 panjf2000 added the invalid This doesn't seem right label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants