This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check connection liveness in beginTx
This commit fixes for ClickHouse#213. If the connection closes from the server side for some reason, the database driver returns driver.ErrBadConn to database/sql. Usually, database/sql retries a request, assuming that the error occurs in a function that could be called first after retrieving a connection from the pool. But beginTx in clickhouse-go doesn't perform any network interaction and driver.ErrBadConn is returned later in the transaction. database/sql doesn't retry it because assumes that connection is alive - beginTx didn't return the error. This commit adds a method to check the connection liveness and performs that check in beginTx function. The check is taken from go-sql-driver/mysql#934. There is no way to check the liveness of a secure connection, as long as there is no access to raw TCP net.Conn in clickhouse-go.
- Loading branch information
1 parent
e95986c
commit f40a48a
Showing
6 changed files
with
205 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos | ||
|
||
package clickhouse | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
var errUnexpectedRead = errors.New("unexpected read from socket") | ||
|
||
func (conn *connect) connCheck() error { | ||
var sysErr error | ||
|
||
sysConn, ok := conn.Conn.(syscall.Conn) | ||
if !ok { | ||
return nil | ||
} | ||
rawConn, err := sysConn.SyscallConn() | ||
if err != nil { | ||
return err | ||
} | ||
// If this connection has a ReadTimeout which we've been setting on | ||
// reads, reset it to zero value before we attempt a non-blocking | ||
// read, otherwise we may get os.ErrDeadlineExceeded for the cached | ||
// connection from the pool with an expired timeout. | ||
if conn.readTimeout != 0 { | ||
err = conn.SetReadDeadline(time.Time{}) | ||
if err != nil { | ||
return fmt.Errorf("set read deadline: %w", err) | ||
} | ||
conn.lastReadDeadlineTime = time.Time{} | ||
} | ||
err = rawConn.Read(func(fd uintptr) bool { | ||
var buf [1]byte | ||
n, err := syscall.Read(int(fd), buf[:]) | ||
switch { | ||
case n == 0 && err == nil: | ||
sysErr = io.EOF | ||
case n > 0: | ||
sysErr = errUnexpectedRead | ||
case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK: | ||
sysErr = nil | ||
default: | ||
sysErr = err | ||
} | ||
return true | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return sysErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// +build !linux,!darwin,!dragonfly,!freebsd,!netbsd,!openbsd,!solaris,!illumos | ||
|
||
package clickhouse | ||
|
||
import "net" | ||
|
||
func connCheck(conn net.Conn) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package clickhouse | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"database/sql/driver" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ConnCheck(t *testing.T) { | ||
const ( | ||
ddl = ` | ||
CREATE TABLE clickhouse_test_conncheck ( | ||
Value String | ||
) Engine = Memory | ||
` | ||
dml = ` | ||
INSERT INTO clickhouse_test_conncheck | ||
VALUES (?) | ||
` | ||
) | ||
|
||
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=false"); assert.NoError(t, err) { | ||
// We could change settings only at session level. | ||
// If we have only 1 connection, we guarantee that we change settings for them. | ||
connect.SetMaxOpenConns(1) | ||
if _, err := connect.Exec("DROP TABLE IF EXISTS clickhouse_test_conncheck"); assert.NoError(t, err) { | ||
if _, err := connect.Exec(ddl); assert.NoError(t, err) { | ||
_, err = connect.Exec("set idle_connection_timeout=1") | ||
assert.NoError(t, err) | ||
|
||
_, err = connect.Exec("set tcp_keep_alive_timeout=0") | ||
assert.NoError(t, err) | ||
|
||
time.Sleep(1100 * time.Millisecond) | ||
ctx := context.Background() | ||
tx, err := connect.BeginTx(ctx, nil) | ||
assert.NoError(t, err) | ||
|
||
_, err = tx.PrepareContext(ctx, dml) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func Test_ConnCheckNegative(t *testing.T) { | ||
const ( | ||
ddl = ` | ||
CREATE TABLE clickhouse_test_conncheck_negative ( | ||
Value String | ||
) Engine = Memory | ||
` | ||
dml = ` | ||
INSERT INTO clickhouse_test_conncheck_negative | ||
VALUES (?) | ||
` | ||
) | ||
|
||
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true&check_connection_liveness=false"); assert.NoError(t, err) { | ||
// We can only change the settings at the connection level. | ||
// If we have only one connection, we change the settings specifically for that connection. | ||
connect.SetMaxOpenConns(1) | ||
if _, err := connect.Exec("DROP TABLE IF EXISTS clickhouse_test_conncheck_negative"); assert.NoError(t, err) { | ||
if _, err := connect.Exec(ddl); assert.NoError(t, err) { | ||
_, err = connect.Exec("set idle_connection_timeout=1") | ||
assert.NoError(t, err) | ||
|
||
_, err = connect.Exec("set tcp_keep_alive_timeout=0") | ||
assert.NoError(t, err) | ||
|
||
time.Sleep(1100 * time.Millisecond) | ||
ctx := context.Background() | ||
tx, err := connect.BeginTx(ctx, nil) | ||
assert.NoError(t, err) | ||
|
||
_, err = tx.PrepareContext(ctx, dml) | ||
assert.Equal(t, driver.ErrBadConn, err) | ||
} | ||
} | ||
} | ||
} |