-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
db.DB/redis.Client: Details in Address for GetAddr #53
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
"github.com/lib/pq" | ||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"golang.org/x/sync/errgroup" | ||
"golang.org/x/sync/semaphore" | ||
"net" | ||
|
@@ -36,7 +37,7 @@ type DB struct { | |
|
||
Options *Options | ||
|
||
addr string | ||
addrDescription string | ||
columnMap ColumnMap | ||
logger *logging.Logger | ||
tableSemaphores map[string]*semaphore.Weighted | ||
|
@@ -94,8 +95,8 @@ func (o *Options) Validate() error { | |
|
||
// 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 | ||
var typeAddr string | ||
|
||
switch c.Type { | ||
case "mysql": | ||
|
@@ -144,8 +145,8 @@ 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) | ||
typeAddr = config.Addr | ||
case "pgsql": | ||
uri := &url.URL{ | ||
Scheme: "postgres", | ||
|
@@ -202,12 +203,18 @@ 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) | ||
typeAddr = utils.JoinHostPort(c.Host, port) | ||
default: | ||
return nil, unknownDbType(c.Type) | ||
} | ||
|
||
addrDescription := c.Type | ||
if c.TlsOptions.Enable { | ||
addrDescription += "+tls" | ||
} | ||
addrDescription += fmt.Sprintf("://%s@%s/%s", c.User, typeAddr, c.Database) | ||
|
||
db.SetMaxIdleConns(c.Options.MaxConnections / 3) | ||
db.SetMaxOpenConns(c.Options.MaxConnections) | ||
|
||
|
@@ -217,15 +224,21 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry | |
DB: db, | ||
Options: &c.Options, | ||
columnMap: NewColumnMap(db.Mapper), | ||
addr: addr, | ||
addrDescription: addrDescription, | ||
logger: logger, | ||
tableSemaphores: make(map[string]*semaphore.Weighted), | ||
}, nil | ||
} | ||
|
||
// GetAddr returns the database host:port or Unix socket address. | ||
// GetAddr returns the Redis connection address in a technical form. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
GetAddr returns the URI-like database connection string with the following syntax |
||
func (db *DB) GetAddr() string { | ||
return db.addr | ||
return db.addrDescription | ||
} | ||
|
||
// MarshalLogObject implements zapcore.ObjectMarshaler, adding the database address to each log message. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would link to |
||
func (db *DB) MarshalLogObject(encoder zapcore.ObjectEncoder) error { | ||
encoder.AddString("database_address", db.addrDescription) | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, please add a newline before return as it improves readability. I know that there is no specific convention or rule in Go regarding the use of newlines before return statements. Even the official Go style guide does not explicitly mention anything in this regard. But we do this in PHP as well and the majority of the code in Icinga DB and Icinga Go library already does this, so I would make this a convention for us. |
||
} | ||
|
||
// BuildColumns returns all columns of the given struct. | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,103 @@ | ||||||||||
package database | ||||||||||
|
||||||||||
import ( | ||||||||||
"github.com/icinga/icinga-go-library/config" | ||||||||||
"github.com/icinga/icinga-go-library/logging" | ||||||||||
"github.com/stretchr/testify/require" | ||||||||||
"go.uber.org/zap/zaptest" | ||||||||||
"testing" | ||||||||||
) | ||||||||||
|
||||||||||
func TestNewDbFromConfig_GetAddr(t *testing.T) { | ||||||||||
tests := []struct { | ||||||||||
name string | ||||||||||
conf *Config | ||||||||||
addr string | ||||||||||
}{ | ||||||||||
{ | ||||||||||
name: "mysql-simple", | ||||||||||
conf: &Config{ | ||||||||||
Type: "mysql", | ||||||||||
Host: "example.com", | ||||||||||
Database: "db", | ||||||||||
User: "user", | ||||||||||
}, | ||||||||||
addr: "mysql://[email protected]:3306/db", | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "mysql-custom-port", | ||||||||||
conf: &Config{ | ||||||||||
Type: "mysql", | ||||||||||
Host: "example.com", | ||||||||||
Port: 1234, | ||||||||||
Database: "db", | ||||||||||
User: "user", | ||||||||||
}, | ||||||||||
addr: "mysql://[email protected]:1234/db", | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "mysql-tls", | ||||||||||
conf: &Config{ | ||||||||||
Type: "mysql", | ||||||||||
Host: "example.com", | ||||||||||
Database: "db", | ||||||||||
User: "user", | ||||||||||
TlsOptions: config.TLS{Enable: true}, | ||||||||||
}, | ||||||||||
addr: "mysql+tls://[email protected]:3306/db", | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "mysql-unix-domain-socket", | ||||||||||
conf: &Config{ | ||||||||||
Type: "mysql", | ||||||||||
Host: "/var/empty/mysql.sock", | ||||||||||
Database: "db", | ||||||||||
User: "user", | ||||||||||
}, | ||||||||||
addr: "mysql://user@/var/empty/mysql.sock/db", | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This demonstrates one limitation of the approach: A nicer looking alternative might be to use And a somewhat strange-looking alternative might be to take some inspiration from icinga-go-library/database/db.go Lines 160 to 163 in 54b737a
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This was inspired by the comment above, #53 (review). I don't know what exactly would be seen as a beautiful solution. Maybe make it more strange by prefixing a filesystem path with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With my comment I didn't mean to imply that it is absolutely necessary to create URI-like strings, but |
||||||||||
}, | ||||||||||
{ | ||||||||||
name: "pgsql-simple", | ||||||||||
conf: &Config{ | ||||||||||
Type: "pgsql", | ||||||||||
Host: "example.com", | ||||||||||
Database: "db", | ||||||||||
User: "user", | ||||||||||
}, | ||||||||||
addr: "pgsql://[email protected]:5432/db", | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "pgsql-custom-port", | ||||||||||
conf: &Config{ | ||||||||||
Type: "pgsql", | ||||||||||
Host: "example.com", | ||||||||||
Port: 1234, | ||||||||||
Database: "db", | ||||||||||
User: "user", | ||||||||||
}, | ||||||||||
addr: "pgsql://[email protected]:1234/db", | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "pgsql-tls", | ||||||||||
conf: &Config{ | ||||||||||
Type: "pgsql", | ||||||||||
Host: "example.com", | ||||||||||
Database: "db", | ||||||||||
User: "user", | ||||||||||
TlsOptions: config.TLS{Enable: true}, | ||||||||||
}, | ||||||||||
addr: "pgsql+tls://[email protected]:5432/db", | ||||||||||
}, | ||||||||||
} | ||||||||||
|
||||||||||
for _, test := range tests { | ||||||||||
t.Run(test.name, func(t *testing.T) { | ||||||||||
db, err := NewDbFromConfig( | ||||||||||
test.conf, | ||||||||||
logging.NewLogger(zaptest.NewLogger(t).Sugar(), 0), | ||||||||||
RetryConnectorCallbacks{}) | ||||||||||
require.NoError(t, err) | ||||||||||
require.Equal(t, test.addr, db.GetAddr()) | ||||||||||
}) | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package redis | ||
|
||
import ( | ||
"github.com/icinga/icinga-go-library/config" | ||
"github.com/icinga/icinga-go-library/logging" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zaptest" | ||
"testing" | ||
) | ||
|
||
func TestNewClientFromConfig_GetAddr(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conf *Config | ||
addr string | ||
}{ | ||
{ | ||
name: "redis-simple", | ||
conf: &Config{ | ||
Host: "example.com", | ||
}, | ||
addr: "redis://example.com:6379", | ||
}, | ||
{ | ||
name: "redis-custom-port", | ||
conf: &Config{ | ||
Host: "example.com", | ||
Port: 6380, | ||
}, | ||
addr: "redis://example.com:6380", | ||
}, | ||
{ | ||
name: "redis-acl", | ||
conf: &Config{ | ||
Host: "example.com", | ||
Username: "user", | ||
Password: "pass", | ||
}, | ||
addr: "redis://[email protected]:6379", | ||
}, | ||
{ | ||
name: "redis-custom-database", | ||
conf: &Config{ | ||
Host: "example.com", | ||
Database: 23, | ||
}, | ||
addr: "redis://example.com:6379/23", | ||
}, | ||
{ | ||
name: "redis-tls", | ||
conf: &Config{ | ||
Host: "example.com", | ||
TlsOptions: config.TLS{Enable: true}, | ||
}, | ||
addr: "redis+tls://example.com:6379", | ||
}, | ||
{ | ||
name: "redis-with-everything", | ||
conf: &Config{ | ||
Host: "example.com", | ||
Port: 6380, | ||
Username: "user", | ||
Password: "pass", | ||
Database: 23, | ||
TlsOptions: config.TLS{Enable: true}, | ||
}, | ||
addr: "redis+tls://[email protected]:6380/23", | ||
}, | ||
{ | ||
name: "redis-unix-domain-socket", | ||
conf: &Config{ | ||
Host: "/var/empty/redis.sock", | ||
}, | ||
addr: "redis:///var/empty/redis.sock", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
redis, err := NewClientFromConfig( | ||
test.conf, | ||
logging.NewLogger(zaptest.NewLogger(t).Sugar(), 0)) | ||
require.NoError(t, err) | ||
require.Equal(t, test.addr, redis.GetAddr()) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a benefit renaming
addr
toaddrDescription
and introducingtypeAddr
. I would revert that and do the following here: