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.69k stars 1.04k forks source link

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

Closed KM-SF closed 6 months ago

KM-SF commented 6 months ago

Actions I've taken before I'm here

Questions with details

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)
}
panjf2000 commented 6 months ago

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

gh-translator commented 6 months ago

🤖 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