ClickHouse/tools/clickhouse-diagnostics/internal/platform/manager.go

49 lines
1.2 KiB
Go
Raw Normal View History

2022-04-27 12:22:20 +00:00
package platform
import (
"errors"
"github.com/ClickHouse/clickhouse-diagnostics/internal/platform/data"
"github.com/ClickHouse/clickhouse-diagnostics/internal/platform/database"
"sync"
)
var once sync.Once
var dbInit sync.Once
// manages all resources that collectors and outputs may wish to ensure inc. db connections
type DBClient interface {
ReadTableNamesForDatabase(databaseName string) ([]string, error)
ReadTable(databaseName string, tableName string, excludeColumns []string, orderBy data.OrderBy, limit int64) (data.Frame, error)
ExecuteStatement(id string, statement string) (data.Frame, error)
Version() (string, error)
}
var manager *ResourceManager
type ResourceManager struct {
DbClient DBClient
}
func GetResourceManager() *ResourceManager {
once.Do(func() {
manager = &ResourceManager{}
})
return manager
}
func (m *ResourceManager) Connect(host string, port uint16, username string, password string) error {
var err error
var clientInstance DBClient
init := false
dbInit.Do(func() {
clientInstance, err = database.NewNativeClient(host, port, username, password)
manager.DbClient = clientInstance
init = true
})
if !init {
return errors.New("connect can only be called once")
}
return err
}