Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use net.Buffers to reduce memory inuse bytes.. #421

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -94,6 +93,7 @@ func (c *Client) Close() error {
return errors.Wrap(err, "conn")
}

c.buf.Reset() // avoid memory leak.
return nil
}

Expand Down Expand Up @@ -269,15 +269,13 @@ func (c *Client) flushBuf(ctx context.Context, b *proto.Buffer) error {
// Reset deadline.
defer func() { _ = c.conn.SetWriteDeadline(time.Time{}) }()
}
n, err := c.conn.Write(b.Buf)
b.Buffers = append(b.Buffers, b.Buf)
n, err := b.Buffers.WriteTo(c.conn)
if err != nil {
return errors.Wrap(err, "write")
}
if n != len(b.Buf) {
return errors.Wrap(io.ErrShortWrite, "wrote less than expected")
}
if ce := c.lg.Check(zap.DebugLevel, "Flush"); ce != nil {
ce.Write(zap.Int("bytes", n))
ce.Write(zap.Int("bytes", int(n)))
}
b.Reset()
return nil
Expand Down
3 changes: 3 additions & 0 deletions proto/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"encoding/binary"
"io"
"math"
"net"
)

// Buffer implements ClickHouse binary protocol encoding.
type Buffer struct {
Buf []byte
net.Buffers
}

// Reader returns new *Reader from *Buffer.
Expand Down Expand Up @@ -45,6 +47,7 @@
// Reset buffer to zero length.
func (b *Buffer) Reset() {
b.Buf = b.Buf[:0]
b.Buffers = nil
}

// Read implements io.Reader.
Expand Down Expand Up @@ -122,7 +125,7 @@
}

func (b *Buffer) PutInt8(v int8) {
b.PutUInt8(uint8(v))

Check failure on line 128 in proto/buffer.go

View workflow job for this annotation

GitHub Actions / lint / run

G115: integer overflow conversion int8 -> uint8 (gosec)
}

func (b *Buffer) PutInt16(v int16) {
Expand Down
11 changes: 6 additions & 5 deletions proto/col_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ColStr struct {

// Append string to column.
func (c *ColStr) Append(v string) {
c.Buf = binary.AppendUvarint(c.Buf, uint64(len(v)))
start := len(c.Buf)
c.Buf = append(c.Buf, v...)
end := len(c.Buf)
Expand All @@ -29,6 +30,7 @@ func (c *ColStr) Append(v string) {

// AppendBytes append byte slice as string to column.
func (c *ColStr) AppendBytes(v []byte) {
c.Buf = binary.AppendUvarint(c.Buf, uint64(len(v)))
start := len(c.Buf)
c.Buf = append(c.Buf, v...)
end := len(c.Buf)
Expand Down Expand Up @@ -68,12 +70,11 @@ func (c *ColStr) Reset() {

// EncodeColumn encodes String rows to *Buffer.
func (c ColStr) EncodeColumn(b *Buffer) {
buf := make([]byte, binary.MaxVarintLen64)
for _, p := range c.Pos {
n := binary.PutUvarint(buf, uint64(p.End-p.Start))
b.Buf = append(b.Buf, buf[:n]...)
b.Buf = append(b.Buf, c.Buf[p.Start:p.End]...)
if b.Buf != nil {
b.Buffers = append(b.Buffers, b.Buf)
b.Buf = nil
}
b.Buffers = append(b.Buffers, c.Buf)
}

// ForEach calls f on each string from column.
Expand Down
1 change: 1 addition & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@
// Note: only blocks are compressed.
// See "Compressible" method of server or client code for reference.
if c.compression == proto.CompressionEnabled {
// TODO SUPPORT COMPRESS
zdyj3170101136 marked this conversation as resolved.
Show resolved Hide resolved
data := c.buf.Buf[start:]
if err := c.compressor.Compress(c.compressionMethod, data); err != nil {
return errors.Wrap(err, "compress")
Expand Down Expand Up @@ -448,7 +449,7 @@
if err != nil {
return errors.Wrap(err, "progress")
}
c.metricsInc(ctx, queryMetrics{Rows: int(p.Rows), Bytes: int(p.Bytes)})

Check failure on line 452 in query.go

View workflow job for this annotation

GitHub Actions / lint / run

G115: integer overflow conversion uint64 -> int (gosec)
if ce := c.lg.Check(zap.DebugLevel, "Progress"); ce != nil {
ce.Write(
zap.Uint64("rows", p.Rows),
Expand Down
Loading