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

Close connection on cancel #764

Merged
merged 4 commits into from
Dec 12, 2022
Merged
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
2 changes: 2 additions & 0 deletions clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (ch *clickhouse) Query(ctx context.Context, query string, args ...interface
if err != nil {
return nil, err
}
conn.debugf("[acquired] connection [%d]", conn.id)
return conn.query(ctx, ch.release, query, args...)
}

Expand All @@ -128,6 +129,7 @@ func (ch *clickhouse) QueryRow(ctx context.Context, query string, args ...interf
err: err,
}
}
conn.debugf("[acquired] connection [%d]", conn.id)
return conn.queryRow(ctx, ch.release, query, args...)
}

Expand Down
52 changes: 27 additions & 25 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ func dial(ctx context.Context, addr string, num int, opt *Options) (*connect, er

var (
connect = &connect{
opt: opt,
conn: conn,
debugf: debugf,
buffer: new(chproto.Buffer),
reader: chproto.NewReader(conn),
revision: proto.ClientTCPProtocolVersion,
structMap: &structMap{},
compression: compression,
connectedAt: time.Now(),
compressor: compress.NewWriter(),
readTimeout: opt.ReadTimeout,
id: num,
opt: opt,
conn: conn,
debugf: debugf,
buffer: new(chproto.Buffer),
reader: chproto.NewReader(conn),
revision: proto.ClientTCPProtocolVersion,
structMap: &structMap{},
compression: compression,
connectedAt: time.Now(),
compressor: compress.NewWriter(),
readTimeout: opt.ReadTimeout,
blockBufferSize: opt.BlockBufferSize,
}
)
Expand All @@ -101,20 +102,21 @@ func dial(ctx context.Context, addr string, num int, opt *Options) (*connect, er

// https://github.com/ClickHouse/ClickHouse/blob/master/src/Client/Connection.cpp
type connect struct {
opt *Options
conn net.Conn
debugf func(format string, v ...interface{})
server ServerVersion
closed bool
buffer *chproto.Buffer
reader *chproto.Reader
released bool
revision uint64
structMap *structMap
compression CompressionMethod
connectedAt time.Time
compressor *compress.Writer
readTimeout time.Duration
id int
opt *Options
conn net.Conn
debugf func(format string, v ...interface{})
server ServerVersion
closed bool
buffer *chproto.Buffer
reader *chproto.Reader
released bool
revision uint64
structMap *structMap
compression CompressionMethod
connectedAt time.Time
compressor *compress.Writer
readTimeout time.Duration
blockBufferSize uint8
}

Expand Down
13 changes: 7 additions & 6 deletions conn_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ package clickhouse
import (
"context"
"fmt"
"io"
"time"

"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
"io"
)

type onProcess struct {
Expand Down Expand Up @@ -137,9 +135,12 @@ func (c *connect) handle(packet byte, on *onProcess) error {
}

func (c *connect) cancel() error {
c.conn.SetDeadline(time.Now().Add(2 * time.Second))
c.debugf("[cancel]")
c.closed = true
c.buffer.PutUVarInt(proto.ClientCancel)
return c.flush()
wErr := c.flush()
// don't reuse a cancelled query as we don't drain the connection
if cErr := c.close(); cErr != nil {
return cErr
}
return wErr
}
1 change: 1 addition & 0 deletions tests/std/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,4 @@ func TestMaxExecutionTime(t *testing.T) {
})
}
}