From e49a611f62db1debb787c5c3a1d00e43a3a98e6a Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 21 May 2024 11:11:41 +0200 Subject: [PATCH] Introduce `GetAddr()` for `database#DB` and `redis#Client` --- cmd/icingadb/main.go | 4 ++-- pkg/database/db.go | 34 +++++++++++++++++++++------------- pkg/redis/client.go | 5 +++++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/cmd/icingadb/main.go b/cmd/icingadb/main.go index d41ae6b7a..33ad17048 100644 --- a/cmd/icingadb/main.go +++ b/cmd/icingadb/main.go @@ -60,7 +60,7 @@ func run() int { } defer db.Close() { - logger.Infof("Connecting to database at '%s'", utils.JoinHostPort(cmd.Config.Database.Host, cmd.Config.Database.Port)) + logger.Infof("Connecting to database at '%s'", db.GetAddr()) err := db.Ping() if err != nil { logger.Fatalf("%+v", errors.Wrap(err, "can't connect to database")) @@ -76,7 +76,7 @@ func run() int { logger.Fatalf("%+v", errors.Wrap(err, "can't create Redis client from config")) } { - logger.Infof("Connecting to Redis at '%s'", utils.JoinHostPort(cmd.Config.Redis.Host, cmd.Config.Redis.Port)) + logger.Infof("Connecting to Redis at '%s'", rc.GetAddr()) _, err := rc.Ping(context.Background()).Result() if err != nil { logger.Fatalf("%+v", errors.Wrap(err, "can't connect to Redis")) diff --git a/pkg/database/db.go b/pkg/database/db.go index 9ae8f0b90..f707a4625 100644 --- a/pkg/database/db.go +++ b/pkg/database/db.go @@ -36,6 +36,7 @@ type DB struct { Options *Options + addr string logger *logging.Logger tableSemaphores map[string]*semaphore.Weighted tableSemaphoresMu sync.Mutex @@ -90,18 +91,9 @@ func (o *Options) Validate() error { return nil } -// NewDb returns a new DB wrapper for a pre-existing sqlx.DB. -func NewDb(db *sqlx.DB, logger *logging.Logger, options *Options) *DB { - return &DB{ - DB: db, - logger: logger, - Options: options, - tableSemaphores: make(map[string]*semaphore.Weighted), - } -} - // NewDbFromConfig returns a new DB from Config. func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks RetryConnectorCallbacks) (*DB, error) { + var addr string var db *sqlx.DB switch c.Type { @@ -151,6 +143,7 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry return setGaleraOpts(ctx, conn, int64(c.Options.WsrepSyncWait)) } + addr = config.Addr db = sqlx.NewDb(sql.OpenDB(NewConnector(connector, logger, connectorCallbacks)), MySQL) case "pgsql": uri := &url.URL{ @@ -168,9 +161,12 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry // string. See also https://github.com/lib/pq/issues/796 "host": {c.Host}, } - if c.Port != 0 { - query["port"] = []string{strconv.FormatInt(int64(c.Port), 10)} + + port := c.Port + if port == 0 { + port = 5432 } + query["port"] = []string{strconv.FormatInt(int64(port), 10)} if _, err := c.TlsOptions.MakeConfig(c.Host); err != nil { return nil, err @@ -205,6 +201,7 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry return nil, errors.Wrap(err, "can't open pgsql database") } + addr = utils.JoinHostPort(c.Host, port) db = sqlx.NewDb(sql.OpenDB(NewConnector(connector, logger, connectorCallbacks)), PostgreSQL) default: return nil, unknownDbType(c.Type) @@ -215,7 +212,18 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry db.Mapper = reflectx.NewMapperFunc("db", strcase.Snake) - return NewDb(db, logger, &c.Options), nil + return &DB{ + DB: db, + Options: &c.Options, + addr: addr, + logger: logger, + tableSemaphores: make(map[string]*semaphore.Weighted), + }, nil +} + +// GetAddr returns the database host:port or Unix socket address. +func (db *DB) GetAddr() string { + return db.addr } // BuildColumns returns all columns of the given struct. diff --git a/pkg/redis/client.go b/pkg/redis/client.go index de6f81398..75657440f 100644 --- a/pkg/redis/client.go +++ b/pkg/redis/client.go @@ -78,6 +78,11 @@ func NewClientFromConfig(c *Config, logger *logging.Logger) (*Client, error) { return NewClient(redis.NewClient(options), logger, &c.Options), nil } +// GetAddr returns the Redis host:port or Unix socket address. +func (c *Client) GetAddr() string { + return c.Client.Options().Addr +} + // HPair defines Redis hashes field-value pairs. type HPair struct { Field string