• haoyanbin's avatar
    1 · 9bf9e037
    haoyanbin authored
    9bf9e037
clickhouse.go 736 Bytes
package utils

import (
	"fmt"
	"gin-vue-admin/global"
	"github.com/ClickHouse/clickhouse-go/v2"
	"time"
)

func GetCh() error {
	if global.GVA_CH == nil {
		chConf := global.GVA_CONFIG.Clickhouse
		conn, err := clickhouse.Open(&clickhouse.Options{
			Addr: []string{chConf.Host},
			Auth: clickhouse.Auth{
				Database: chConf.Database,
				Username: chConf.Username,
				Password: chConf.Password,
			},
			Debug:           chConf.Debug,
			DialTimeout:     2 * time.Second,
			MaxOpenConns:    10,
			MaxIdleConns:    5,
			ConnMaxLifetime: time.Hour,
			Compression:     &clickhouse.Compression{Method: clickhouse.CompressionLZ4},
		})
		if err != nil {
			fmt.Println(err)
			return err
		}
		global.GVA_CH = conn
	}
	return nil
}