lib/handshake: log read/write operation duration on connection errors

This improve debuggability of network errors
This commit is contained in:
Aliaksandr Valialkin 2021-03-02 21:18:32 +02:00
parent c8dde1fd6b
commit c67a07b469
2 changed files with 15 additions and 3 deletions

View file

@ -311,7 +311,7 @@ func (s *Server) processVMInsertConn(bc *handshake.BufferedConn) error {
}
reqBuf = bytesutil.Resize(reqBuf, int(packetSize))
if n, err := io.ReadFull(bc, reqBuf); err != nil {
return fmt.Errorf("cannot read packet with size %d: %w; read only %d bytes", packetSize, err, n)
return fmt.Errorf("cannot read packet with size %d bytes: %w; read only %d bytes", packetSize, err, n)
}
// Send `ack` to vminsert that the packet has been received.
deadline := time.Now().Add(5 * time.Second)

View file

@ -2,8 +2,10 @@ package handshake
import (
"bufio"
"fmt"
"io"
"net"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding/zstd"
)
@ -43,14 +45,24 @@ func newBufferedConn(c net.Conn, compressionLevel int, isReadCompressed bool) *B
// Read reads up to len(p) from bc to p.
func (bc *BufferedConn) Read(p []byte) (int, error) {
return bc.br.Read(p)
startTime := time.Now()
n, err := bc.br.Read(p)
if err != nil {
err = fmt.Errorf("cannot read data in %.3f seconds: %w", time.Since(startTime).Seconds(), err)
}
return n, err
}
// Write writes p to bc.
//
// Do not forget to call Flush if needed.
func (bc *BufferedConn) Write(p []byte) (int, error) {
return bc.bw.Write(p)
startTime := time.Now()
n, err := bc.bw.Write(p)
if err != nil {
err = fmt.Errorf("cannot write data in %.3f seconds: %w", time.Since(startTime).Seconds(), err)
}
return n, err
}
// Close closes bc.