evio icon indicating copy to clipboard operation
evio copied to clipboard

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

Open recoilme opened this issue 4 years ago • 1 comments

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 avatar Oct 18 '19 12:10 recoilme

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

recoilme avatar Nov 12 '19 15:11 recoilme