2019-05-28 14:31:35 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-11-29 19:46:52 +00:00
|
|
|
"errors"
|
2019-05-28 14:31:35 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-29 19:46:52 +00:00
|
|
|
"strings"
|
2021-06-14 09:15:30 +00:00
|
|
|
"time"
|
2019-05-28 14:31:35 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The maximum size of a single line returned by ReadLinesBlock.
|
|
|
|
const maxLineSize = 256 * 1024
|
|
|
|
|
|
|
|
// Default size in bytes of a single block returned by ReadLinesBlock.
|
|
|
|
const defaultBlockSize = 64 * 1024
|
|
|
|
|
|
|
|
// ReadLinesBlock reads a block of lines delimited by '\n' from tailBuf and r into dstBuf.
|
|
|
|
//
|
|
|
|
// Trailing chars after the last newline are put into tailBuf.
|
|
|
|
//
|
|
|
|
// Returns (dstBuf, tailBuf).
|
2020-08-14 17:13:15 +00:00
|
|
|
//
|
|
|
|
// It is expected that read timeout on r exceeds 1 second.
|
2019-05-28 14:31:35 +00:00
|
|
|
func ReadLinesBlock(r io.Reader, dstBuf, tailBuf []byte) ([]byte, []byte, error) {
|
2019-12-09 18:58:19 +00:00
|
|
|
return ReadLinesBlockExt(r, dstBuf, tailBuf, maxLineSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadLinesBlockExt reads a block of lines delimited by '\n' from tailBuf and r into dstBuf.
|
|
|
|
//
|
|
|
|
// Trailing chars after the last newline are put into tailBuf.
|
|
|
|
//
|
|
|
|
// Returns (dstBuf, tailBuf).
|
|
|
|
//
|
|
|
|
// maxLineLen limits the maximum length of a single line.
|
2020-08-14 17:13:15 +00:00
|
|
|
//
|
|
|
|
// It is expected that read timeout on r exceeds 1 second.
|
2019-12-09 18:58:19 +00:00
|
|
|
func ReadLinesBlockExt(r io.Reader, dstBuf, tailBuf []byte, maxLineLen int) ([]byte, []byte, error) {
|
2021-06-14 09:25:43 +00:00
|
|
|
startTime := time.Now()
|
2019-05-28 14:31:35 +00:00
|
|
|
if cap(dstBuf) < defaultBlockSize {
|
2022-01-31 22:18:39 +00:00
|
|
|
dstBuf = bytesutil.ResizeNoCopyNoOverallocate(dstBuf, defaultBlockSize)
|
2019-05-28 14:31:35 +00:00
|
|
|
}
|
|
|
|
dstBuf = append(dstBuf[:0], tailBuf...)
|
2019-06-25 23:51:54 +00:00
|
|
|
tailBuf = tailBuf[:0]
|
2019-05-28 14:31:35 +00:00
|
|
|
again:
|
|
|
|
n, err := r.Read(dstBuf[len(dstBuf):cap(dstBuf)])
|
|
|
|
// Check for error only if zero bytes read from r, i.e. no forward progress made.
|
|
|
|
// Otherwise process the read data.
|
|
|
|
if n == 0 {
|
|
|
|
if err == nil {
|
|
|
|
return dstBuf, tailBuf, fmt.Errorf("no forward progress made")
|
|
|
|
}
|
2021-11-29 19:46:52 +00:00
|
|
|
isEOF := isEOFLikeError(err)
|
|
|
|
if isEOF && len(dstBuf) > 0 {
|
2019-06-07 20:17:57 +00:00
|
|
|
// Missing newline in the end of stream. This is OK,
|
2019-06-25 23:51:54 +00:00
|
|
|
// so suppress io.EOF for now. It will be returned during the next
|
2019-06-07 20:17:57 +00:00
|
|
|
// call to ReadLinesBlock.
|
|
|
|
// This fixes https://github.com/VictoriaMetrics/VictoriaMetrics/issues/60 .
|
|
|
|
return dstBuf, tailBuf, nil
|
|
|
|
}
|
2021-11-29 19:46:52 +00:00
|
|
|
if !isEOF {
|
2021-06-14 09:25:43 +00:00
|
|
|
err = fmt.Errorf("cannot read a block of data in %.3fs: %w", time.Since(startTime).Seconds(), err)
|
2021-11-29 19:46:52 +00:00
|
|
|
} else {
|
|
|
|
err = io.EOF
|
2021-06-14 09:15:30 +00:00
|
|
|
}
|
2019-05-28 14:31:35 +00:00
|
|
|
return dstBuf, tailBuf, err
|
|
|
|
}
|
|
|
|
dstBuf = dstBuf[:len(dstBuf)+n]
|
|
|
|
|
|
|
|
// Search for the last newline in dstBuf and put the rest into tailBuf.
|
|
|
|
nn := bytes.LastIndexByte(dstBuf[len(dstBuf)-n:], '\n')
|
|
|
|
if nn < 0 {
|
2023-11-24 10:53:04 +00:00
|
|
|
// Didn't find at least a single line.
|
2019-12-09 18:58:19 +00:00
|
|
|
if len(dstBuf) > maxLineLen {
|
|
|
|
return dstBuf, tailBuf, fmt.Errorf("too long line: more than %d bytes", maxLineLen)
|
2019-05-28 14:31:35 +00:00
|
|
|
}
|
|
|
|
if cap(dstBuf) < 2*len(dstBuf) {
|
|
|
|
// Increase dsbBuf capacity, so more data could be read into it.
|
|
|
|
dstBufLen := len(dstBuf)
|
2022-01-31 22:18:39 +00:00
|
|
|
dstBuf = bytesutil.ResizeWithCopyNoOverallocate(dstBuf, 2*cap(dstBuf))
|
2019-05-28 14:31:35 +00:00
|
|
|
dstBuf = dstBuf[:dstBufLen]
|
|
|
|
}
|
|
|
|
goto again
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found at least a single line. Return it.
|
|
|
|
nn += len(dstBuf) - n
|
|
|
|
tailBuf = append(tailBuf[:0], dstBuf[nn+1:]...)
|
|
|
|
dstBuf = dstBuf[:nn]
|
|
|
|
return dstBuf, tailBuf, nil
|
|
|
|
}
|
2021-11-29 19:46:52 +00:00
|
|
|
|
|
|
|
func isEOFLikeError(err error) bool {
|
2023-08-18 06:55:42 +00:00
|
|
|
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
|
2021-11-29 19:46:52 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
s := err.Error()
|
|
|
|
return strings.Contains(s, "reset by peer")
|
|
|
|
}
|