initialize.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package olap
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net"
  7. "time"
  8. extConfig "go-admin/config"
  9. "github.com/ClickHouse/clickhouse-go/v2"
  10. "github.com/go-admin-team/go-admin-core/sdk/config"
  11. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  12. "github.com/uptrace/opentelemetry-go-extra/otelgorm"
  13. gormclickhouse "gorm.io/driver/clickhouse"
  14. "gorm.io/gorm"
  15. "gorm.io/gorm/schema"
  16. )
  17. func Setup() {
  18. if extConfig.ExtConfig.OLAPDB.Driver == "clickhouse" {
  19. setClickhouse()
  20. setClickhouseOrm()
  21. } else {
  22. log.Fatal("Have no other olap driver but clickhouse only.")
  23. }
  24. }
  25. func setClickhouse() {
  26. dialCount := 0
  27. debug := false
  28. if config.ApplicationConfig.Mode == "dev" {
  29. debug = true
  30. }
  31. OlapConn, err := clickhouse.Open(&clickhouse.Options{
  32. Addr: []string{extConfig.ExtConfig.OLAPDB.Addr},
  33. Auth: clickhouse.Auth{
  34. Database: extConfig.ExtConfig.OLAPDB.Database,
  35. Username: extConfig.ExtConfig.OLAPDB.Username,
  36. Password: extConfig.ExtConfig.OLAPDB.Password,
  37. },
  38. DialContext: func(ctx context.Context, addr string) (net.Conn, error) {
  39. dialCount++
  40. var d net.Dialer
  41. return d.DialContext(ctx, "tcp", addr)
  42. },
  43. Debug: debug,
  44. Debugf: func(format string, v ...interface{}) {
  45. fmt.Println(format, v)
  46. },
  47. Settings: clickhouse.Settings{
  48. "max_execution_time": 60,
  49. },
  50. Compression: &clickhouse.Compression{
  51. Method: clickhouse.CompressionLZ4,
  52. },
  53. DialTimeout: time.Duration(10) * time.Second,
  54. MaxOpenConns: extConfig.ExtConfig.OLAPDB.MaxOpenConns,
  55. MaxIdleConns: extConfig.ExtConfig.OLAPDB.MaxIdleConns,
  56. ConnMaxLifetime: time.Duration(10) * time.Minute,
  57. ConnOpenStrategy: clickhouse.ConnOpenInOrder,
  58. BlockBufferSize: 10,
  59. })
  60. if err != nil {
  61. log.Fatal(pkg.Red(extConfig.ExtConfig.OLAPDB.Driver+" connect error (conn):"), err)
  62. }
  63. defaultOlapConn = OlapConn
  64. log.Println(pkg.Green("clickhouse connect success (conn) !"))
  65. }
  66. func setClickhouseOrm() {
  67. dsn := fmt.Sprintf("clickhouse://%s:%s@%s/%s?dial_timeout=%ss&max_execution_time=%s",
  68. extConfig.ExtConfig.OLAPDB.Username,
  69. extConfig.ExtConfig.OLAPDB.Password,
  70. extConfig.ExtConfig.OLAPDB.Addr,
  71. extConfig.ExtConfig.OLAPDB.Database,
  72. "10",
  73. "60",
  74. )
  75. db, err := gorm.Open(gormclickhouse.Open(dsn), &gorm.Config{
  76. NamingStrategy: ChNamingStrategy{
  77. schema.NamingStrategy{TablePrefix: "otel_", SingularTable: false},
  78. },
  79. })
  80. if err != nil {
  81. log.Fatal(pkg.Red(extConfig.ExtConfig.OLAPDB.Driver+" connect error (orm) :"), err)
  82. }
  83. chdb, err := db.DB()
  84. if err != nil {
  85. log.Fatal(pkg.Red(extConfig.ExtConfig.OLAPDB.Driver+" get *sql.DB error :"), err)
  86. }
  87. chdb.SetMaxIdleConns(extConfig.ExtConfig.OLAPDB.MaxIdleConns)
  88. chdb.SetMaxOpenConns(extConfig.ExtConfig.OLAPDB.MaxOpenConns)
  89. chdb.SetConnMaxLifetime(time.Duration(10) * time.Minute)
  90. db.Use(otelgorm.NewPlugin())
  91. log.Println(pkg.Green(extConfig.ExtConfig.OLAPDB.Driver + " connect success (orm) !"))
  92. gormClickhouse = db
  93. }