mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 01:15:22 +00:00
3755a58070
Adds configuration options to use a read-only MySQL replica, and uses it instead of the primary for reads.
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package mysql
|
|
|
|
import (
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
|
"github.com/go-kit/kit/log"
|
|
)
|
|
|
|
const defaultMaxAttempts int = 15
|
|
|
|
// DBOption is used to pass optional arguments to a database connection
|
|
type DBOption func(o *dbOptions) error
|
|
|
|
type dbOptions struct {
|
|
// maxAttempts configures the number of retries to connect to the DB
|
|
maxAttempts int
|
|
logger log.Logger
|
|
replicaConfig *config.MysqlConfig
|
|
}
|
|
|
|
// Logger adds a logger to the datastore
|
|
func Logger(l log.Logger) DBOption {
|
|
return func(o *dbOptions) error {
|
|
o.logger = l
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Replica sets the configuration of the read replica for the datastore.
|
|
func Replica(conf *config.MysqlConfig) DBOption {
|
|
return func(o *dbOptions) error {
|
|
o.replicaConfig = conf
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// LimitAttempts sets a the number of attempts
|
|
// to try establishing a connection to the database backend
|
|
// the default value is 15 attempts
|
|
func LimitAttempts(attempts int) DBOption {
|
|
return func(o *dbOptions) error {
|
|
o.maxAttempts = attempts
|
|
return nil
|
|
}
|
|
}
|