tidwall / redcon

Redis compatible server framework for Go
MIT License
2.19k stars 158 forks source link

Provide Flush() function from the Conn interface #66

Closed digitive closed 1 year ago

digitive commented 1 year ago

We need a Flush() function on the Conn interface so that the server can flush the response when multiple line of response accumulated. For example

func (r *RedisHandler) Ping(conn redcon.Conn, cmd redcon.Command) {
    conn.WriteString("PONG1")
    conn.WriteString("PONG2")
    conn.WriteString("PONG3")
}

When client sends ping command, only PONG1 returned. Then if client sends something like get something, PONG2 returned.

We need ability to flush the underlying buffer from server side, e.g

func (r *RedisHandler) Ping(conn redcon.Conn, cmd redcon.Command) {
    conn.WriteString("PONG1")
    conn.WriteString("PONG2")
    conn.WriteString("PONG3")
    conn.Flush() // <- we need this here
}

The underlying conn.wr.Flush() has been implemented. We just need to expose it on the Conn interface.

This feature is required when implementing the transaction feature like MULTI/EXEC, which will queue commands and execute at one submit.

Thank you!

tidwall commented 1 year ago

Redcon takes care of the flushing for you. It automatically flushes after handling all commands contained in the client's pipeline packet. In most cases this will be one command per packet.

For every incoming command there will be one handler call, and you should be sending exactly one response. A String, Bulk String, Integer, Error, or Array.

In your case, it appears that you are sending three PONGs for one PING.

The MUTLI/EXEC can work by queuing each command after the MULTI, and sending a "queued" response. Then once EXEC is handled, all the queued commands will be processed at once and all responses are sent together.

digitive commented 1 year ago

Hi @tidwall thanks for prompt response. I was using in the wrong way. For MULTI/EXEC I should send back an array instead of multiple call to conn.WriteString(). All good. Thanks again.