Calculate netspeed points, too
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
74
chdb/db.go
74
chdb/db.go
@@ -1,8 +1,78 @@
|
||||
package chdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/ClickHouse/clickhouse-go/v2"
|
||||
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
|
||||
"go.ntppool.org/common/version"
|
||||
"golang.org/x/exp/slog"
|
||||
)
|
||||
|
||||
type ClickHouse struct {
|
||||
conn clickhouse.Conn
|
||||
}
|
||||
|
||||
func New(dbConfigPath string) (*ClickHouse, error) {
|
||||
return &ClickHouse{}, nil
|
||||
func New(ctx context.Context, dbConfigPath string) (*ClickHouse, error) {
|
||||
conn, err := setupClickhouse(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ClickHouse{conn: conn}, nil
|
||||
}
|
||||
|
||||
func setupClickhouse(ctx context.Context) (driver.Conn, error) {
|
||||
|
||||
conn, err := clickhouse.Open(&clickhouse.Options{
|
||||
Addr: []string{"10.43.207.123:9000"},
|
||||
Auth: clickhouse.Auth{
|
||||
Database: "geodns3",
|
||||
Username: "default",
|
||||
Password: "",
|
||||
},
|
||||
// Debug: true,
|
||||
// Debugf: func(format string, v ...interface{}) {
|
||||
// slog.Info("debug format", "format", format)
|
||||
// fmt.Printf(format+"\n", v)
|
||||
// },
|
||||
Settings: clickhouse.Settings{
|
||||
"max_execution_time": 60,
|
||||
},
|
||||
Compression: &clickhouse.Compression{
|
||||
Method: clickhouse.CompressionLZ4,
|
||||
},
|
||||
DialTimeout: time.Second * 5,
|
||||
MaxOpenConns: 5,
|
||||
MaxIdleConns: 5,
|
||||
ConnMaxLifetime: time.Duration(10) * time.Minute,
|
||||
ConnOpenStrategy: clickhouse.ConnOpenInOrder,
|
||||
BlockBufferSize: 10,
|
||||
MaxCompressionBuffer: 10240,
|
||||
ClientInfo: clickhouse.ClientInfo{
|
||||
Products: []struct {
|
||||
Name string
|
||||
Version string
|
||||
}{
|
||||
{Name: "data-api", Version: version.Version()},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v, err := conn.ServerVersion()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
slog.Info("clickhouse connection", "version", v)
|
||||
|
||||
err = conn.Ping(ctx)
|
||||
if err != nil {
|
||||
slog.Error("clickhouse ping", "err", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user