Friday, December 6, 2024
HomeGolangStudying from web.Conn hangs or breaks early - Code Assessment

Studying from web.Conn hangs or breaks early – Code Assessment


Hello. I’m kinda new to Go and caught with a difficulty of hanging web connection or not studying full message from web connection. I’m attempting to do a easy redis by way of tcp connection.
Right here’s an instance how I attempt to learn information from a web connection:

const connChunkSize int = 1024

func readConnectionMessage(conn web.Conn) string {
	buffer := bytes.NewBuffer(nil)
	for {
		chunk := make([]byte, connChunkSize)
		learn, err := conn.Learn(chunk)

		if learn > 0 {
			buffer.Write(chunk[:read])
		}

		if learn == 0 || errors.Is(err, io.EOF) {
			break
		} else if err != nil {
			return err.Error()
		}
	}

	return buffer.String()
}

I obtain a “$1n$4nPING” on the primary loop and on the second loop conn.Learn will dangle.
If I do break if assertion when learn bytes are < 1024 like:

if learn < 1024 || errors.Is(err, io.EOF) {
    break
}

This can work for redis-cli PING. However after I do, echo -e "PINGnPING" | redis-cli, it’ll learn solely the primary PING command as within the first take a look at case, performance will reply to 1 command and shut the connection.

So the query is, the way to correctly learn all the information from connection and keep away from hanging? I’ve tried other ways of implementing studying from connection, however all have the identical end result.

My finest guess could be that redis-cli is ready to your reply after the primary PING command. In accordance with the protocol spec you’re on the level the place you obtained the primary command in full: array of size 1, first bulk string of size 4 after which the 4 bytes. It’s best to now write "+PONGn" again to the socket.

I feel you must evaluate err and nil at first ,not learn >0

If you will course of protocol packets, you must use for stream connections io. ReadFull, to learn the total packet from the stream, as an alternative of you giving a buff to learn it, and also you don’t should take care of whether or not to attend for the total packet, then you could learn the truncated information.
From web. Conn reads information, usually information is interactive, when a blockage happens, it signifies that the opposite celebration might not be sending information, it could be ready to your response, and will probably be launched when the connection error happens, I feel you must first perceive the instance of golang tcp, after which write a selected software.

That is sensible, thanks.

I feel you must evaluate err and nil at first ,not learn >0

Probably not, if the case when studying reached EOF and nonetheless learn some bytes.

It’s worthwhile to cross a buffer to io.ReadFull and also you describe a basic case dealing with for a tcp connection, which I’m doing right here.

For protocol parsing, it’s usually vital to acquire information of a specified size. When information of a size that doesn’t adjust to the protocol specification seems, I feel it isn’t essential to course of it. This buff is mostly given a regular size, and when io.readfull is handed in , use buff[:4] to learn 4 bytes of knowledge as an alternative of instantly passing in buff
That is simply an instance, the small print nonetheless rely upon the protocol specs.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments