tidwall / evio

Fast event-loop networking for Go
MIT License
5.9k stars 493 forks source link

I think writing to the output buffer wrong in examples (http server/ redis server) #58

Open recoilme opened 5 years ago

recoilme commented 5 years ago

Hello. I start from simple example how to work with evio:

    events.Data = func(ec evio.Conn, in []byte) (out []byte, action evio.Action) {
        if in == nil {
            fmt.Printf("wake from %s\n", ec.RemoteAddr())
            return nil, evio.Close
        }
        c := ec.Context().(*conn)
        data := c.is.Begin(in)
        for {
            leftover, response, err := mcproto(data, b52)
            if err != nil {
                if err != ErrClose {
                    // bad thing happened
                    println(err.Error())
                }
                action = evio.Close
                break
            } else if len(leftover) == len(data) {
                // request not ready, yet
                break
            }
            // handle the request
            out = response//next response may overwrite previous buffer
            data = leftover
        }
        c.is.End(data)
        return
    }

But out buffer will be overrided in case of piplined requests. I think we must write to output - like this:

                responses := make([]byte, 0)
        for {
            leftover, response, err := mcproto(data, b52)

            // handle the response
            responses = append(responses, response...)
            //out = response// don't do like this
            data = leftover
        }
        out = responses// do like this
        c.is.End(data)

You may find test for that case - here: https://github.com/recoilme/b52/blob/master/main_test.go#L19

I am not sure what's going on internally - but spent two weeks, for finding that problem) May be you have this kind of problem in rediconn+evio+pipelined, too

recoilme commented 5 years ago

And special thanks for the great library! On the standard library, I could not even get close to the 2ms latency in my database https://github.com/recoilme/b52