mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-12 03:09:42 +08:00
🐛 fix(clickhouse): 修正 8132 端口连接失败
- 将 8132 纳入 ClickHouse HTTP 端口识别范围 - 同步修正协议切换日志与错误提示中的端口说明 - 补充连接协议识别回归测试并更新 backlog 记录 Fixes #338
This commit is contained in:
@@ -26,6 +26,7 @@ const (
|
||||
defaultClickHouseUser = "default"
|
||||
defaultClickHouseDatabase = "default"
|
||||
minClickHouseReadTimeout = 5 * time.Minute
|
||||
clickHouseHTTPPortHint = "8123/8132/8443"
|
||||
)
|
||||
|
||||
type ClickHouseDB struct {
|
||||
@@ -133,12 +134,21 @@ func detectClickHouseProtocol(config connection.ConnectionConfig) clickhouse.Pro
|
||||
if strings.HasPrefix(uriText, "http://") || strings.HasPrefix(uriText, "https://") {
|
||||
return clickhouse.HTTP
|
||||
}
|
||||
if config.Port == 8123 || config.Port == 8443 {
|
||||
if isClickHouseHTTPPort(config.Port) {
|
||||
return clickhouse.HTTP
|
||||
}
|
||||
return clickhouse.Native
|
||||
}
|
||||
|
||||
func isClickHouseHTTPPort(port int) bool {
|
||||
switch port {
|
||||
case 8123, 8132, 8443:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isClickHouseProtocolMismatch(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
@@ -246,14 +256,14 @@ func (c *ClickHouseDB) Connect(config connection.ConnectionConfig) error {
|
||||
logger.Warnf("ClickHouse SSL 优先连接失败,已回退至明文连接")
|
||||
}
|
||||
if pIdx > 0 {
|
||||
logger.Warnf("ClickHouse 已自动切换连接协议为 %s(常见于 8123/8443 HTTP 端口)", protocol.String())
|
||||
logger.Warnf("ClickHouse 已自动切换连接协议为 %s(常见于 %s HTTP 端口)", protocol.String(), clickHouseHTTPPortHint)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
_ = c.Close()
|
||||
return fmt.Errorf("连接建立后验证失败(可检查 ClickHouse 端口与协议是否匹配:Native=9000/9440,HTTP=8123/8443):%s", strings.Join(failures, ";"))
|
||||
return fmt.Errorf("连接建立后验证失败(可检查 ClickHouse 端口与协议是否匹配:Native=9000/9440,HTTP=%s):%s", clickHouseHTTPPortHint, strings.Join(failures, ";"))
|
||||
}
|
||||
|
||||
func (c *ClickHouseDB) Close() error {
|
||||
|
||||
@@ -12,6 +12,10 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"GoNavi-Wails/internal/connection"
|
||||
|
||||
clickhouse "github.com/ClickHouse/clickhouse-go/v2"
|
||||
)
|
||||
|
||||
const fakeClickHouseDriverName = "gonavi-fake-clickhouse"
|
||||
@@ -129,6 +133,65 @@ func TestClickHouseGetDatabasesFallsBackToCurrentDatabase(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectClickHouseProtocolTreatsHTTPPortsAsHTTP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config connection.ConnectionConfig
|
||||
expected clickhouse.Protocol
|
||||
}{
|
||||
{
|
||||
name: "http uri",
|
||||
config: connection.ConnectionConfig{
|
||||
URI: "http://127.0.0.1:8132/default",
|
||||
},
|
||||
expected: clickhouse.HTTP,
|
||||
},
|
||||
{
|
||||
name: "default http port",
|
||||
config: connection.ConnectionConfig{
|
||||
Port: 8123,
|
||||
},
|
||||
expected: clickhouse.HTTP,
|
||||
},
|
||||
{
|
||||
name: "alternate http port 8132",
|
||||
config: connection.ConnectionConfig{
|
||||
Port: 8132,
|
||||
},
|
||||
expected: clickhouse.HTTP,
|
||||
},
|
||||
{
|
||||
name: "https port",
|
||||
config: connection.ConnectionConfig{
|
||||
Port: 8443,
|
||||
},
|
||||
expected: clickhouse.HTTP,
|
||||
},
|
||||
{
|
||||
name: "native port",
|
||||
config: connection.ConnectionConfig{
|
||||
Port: 9000,
|
||||
},
|
||||
expected: clickhouse.Native,
|
||||
},
|
||||
{
|
||||
name: "native tls port",
|
||||
config: connection.ConnectionConfig{
|
||||
Port: 9440,
|
||||
},
|
||||
expected: clickhouse.Native,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if protocol := detectClickHouseProtocol(tt.config); protocol != tt.expected {
|
||||
t.Fatalf("expected protocol %s, got %s", tt.expected.String(), protocol.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type fakeClickHouseDriver struct{}
|
||||
|
||||
func (fakeClickHouseDriver) Open(name string) (driver.Conn, error) {
|
||||
|
||||
Reference in New Issue
Block a user