Skip to content

Commit

Permalink
Introduce GetAddr() for database#DB and redis#Client
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed May 21, 2024
1 parent 0e41b32 commit e49a611
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmd/icingadb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down
34 changes: 21 additions & 13 deletions pkg/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type DB struct {

Options *Options

addr string
logger *logging.Logger
tableSemaphores map[string]*semaphore.Weighted
tableSemaphoresMu sync.Mutex
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions pkg/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e49a611

Please sign in to comment.