Source File
io.go
Belonging Package
github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/util
package utilimport (pool)type DelimitedReader struct {r io.Readerbuf []byte}// The gogo protobuf NewDelimitedReader is buffered, which may eat up stream data.// So we need to implement a compatible delimited reader that reads unbuffered.// There is a slowdown from unbuffered reading: when reading the message// it can take multiple single byte Reads to read the length and another Read// to read the message payload.// However, this is not critical performance degradation as// - the reader is utilized to read one (dialer, stop) or two messages (hop) during// the handshake, so it's a drop in the water for the connection lifetime.// - messages are small (max 4k) and the length fits in a couple of bytes,// so overall we have at most three reads per message.func ( io.Reader, int) *DelimitedReader {return &DelimitedReader{r: , buf: pool.Get()}}func ( *DelimitedReader) () {if .buf != nil {pool.Put(.buf).buf = nil}}func ( *DelimitedReader) () (byte, error) {:= .buf[:1], := .r.Read()return [0],}func ( *DelimitedReader) ( proto.Message) error {, := varint.ReadUvarint()if != nil {return}if uint64(len(.buf)) < {return errors.New("message too large")}:= .buf[:]_, = io.ReadFull(.r, )if != nil {return}return proto.Unmarshal(, )}func ( io.Writer) pbio.WriteCloser {return pbio.NewDelimitedWriter()}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |