Skip to content

Commit

Permalink
Merge pull request #22 from snapp-incubator/separate-db-attributes
Browse files Browse the repository at this point in the history
Separate db connection URI values in config
  • Loading branch information
parsaaes authored Jan 18, 2023
2 parents b98e233 + af4a36e commit 68cd9dc
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 53 deletions.
17 changes: 12 additions & 5 deletions .deploy/helm/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ metadata:
data:
config.yaml: |-
database:
conn-str: {{ .Values.database.connStr | quote }}
driver: {{ .Values.database.driver | quote }}
options:
connection-lifetime: {{ .Values.database.options.connectionLifetime | quote }}
max-idle-connections: {{ .Values.database.options.maxIdleConnections }}
max-open-connections: {{ .Values.database.options.maxOpenConnections }}
host: {{ .Values.database.host | quote }}
port: {{ .Values.database.port }}
db_name: {{ .Values.database.dbName | quote }}
username: {{ .Values.database.username | quote }}
password: {{ .Values.database.password | quote }}
timeout: {{ .Values.database.timeout | quote }}
read_timeout: {{ .Values.database.readTimeout | quote }}
write_timeout: {{ .Values.database.writeTimeout | quote }}
connection_lifetime: {{ .Values.database.connectionLifetime | quote }}
max_open_connections: {{ .Values.database.maxIdleConnections }}
max_idle_connections: {{ .Values.database.maxOpenConnections }}
matrix:
display-name: {{ .Values.matrix.displayName | quote }}
token: {{ .Values.matrix.token | quote }}
Expand Down
18 changes: 13 additions & 5 deletions .deploy/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,22 @@ tolerations: []
affinity: {}

database:
connStr: on-call:secret@tcp(localhost:3306)/on-call?readTimeout=3s&timeout=30s&parseTime=True
driver: mysql
options:
connectionLifetime: 10m
maxIdleConnections: 5
maxOpenConnections: 10
host: "localhost"
port: 3306
dbName: "on-call"
username: "on-call"
password: "secret"
timeout: "30s"
readTimeout: "3s"
writeTimeout: "1s"
connectionLifetime: 10m
maxIdleConnections: 5
maxOpenConnections: 10
matrix:
displayName: user
token: secret
url: https://example.com
userID: '@some_bot:example.com'

envs: {}
16 changes: 11 additions & 5 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ matrix:

database:
driver: mysql
conn-str: "on-call:secret@tcp(localhost:33060)/on-call?readTimeout=3s&timeout=30s&parseTime=True"
options:
connection-lifetime: "10m"
max-open-connections: 10
max-idle-connections: 5
host: "localhost"
port: 33060
db_name: "on-call"
username: "on-call"
password: "secret"
timeout: "30s"
read_timeout: "3s"
write_timeout: "1s"
connection_lifetime: "10m"
max_open_connections: 10
max_idle_connections: 5
7 changes: 1 addition & 6 deletions internal/cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ const (
var ErrFlags = errors.New("error parsing flags")

func main(path string, cfg config.Database) error {
oncallDB := database.WithRetry(
database.Create,
cfg.Driver,
cfg.ConnStr,
cfg.Options,
)
oncallDB := database.WithRetry(database.Create, cfg)

sqlDB, err := oncallDB.DB()
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions internal/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ import (
const sigChanSize = 2

func main(cfg config.Config) {
oncallDB := database.WithRetry(
database.Create,
cfg.Database.Driver,
cfg.Database.ConnStr,
cfg.Database.Options,
)
oncallDB := database.WithRetry(database.Create, cfg.Database)

roomRepo := &model.SQLRoomRepo{DB: oncallDB}
shiftRepo := &model.SQLShiftRepo{DB: oncallDB}
Expand Down
31 changes: 26 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"

"github.com/go-playground/validator/v10"

"github.com/snapp-incubator/matrix-on-call-bot/internal/database"
)

const (
Expand All @@ -36,9 +35,18 @@ type (
}

Database struct {
Driver string `mapstructure:"driver"`
ConnStr string `mapstructure:"conn-str"`
Options database.Options `mapstructure:"options"`
Driver string `mapstructure:"driver"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
DBName string `mapstructure:"db_name"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Timeout time.Duration `mapstructure:"timeout"`
ReadTimeout time.Duration `mapstructure:"read_timeout"`
WriteTimeout time.Duration `mapstructure:"write_timeout"`
ConnectionLifetime time.Duration `mapstructure:"connection_lifetime"`
MaxOpenConnections int `mapstructure:"max_open_connections"`
MaxIdleConnections int `mapstructure:"max_idle_connections"`
}
)

Expand All @@ -47,6 +55,19 @@ func (c Config) Validate() error {
return errors.Wrap(validator.New().Struct(c), "config validation failed")
}

// MySQLConnectionURI returns URI for connecting to a MySQL liked database.
func (d Database) MySQLConnectionURI() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?timeout=%s&readTimeout=%s&writeTimeout=%s&parseTime=True",
d.Username,
d.Password,
d.Host,
d.Port,
d.DBName,
d.Timeout.String(),
d.ReadTimeout.String(),
d.WriteTimeout.String())
}

// Init reads and validates application configs.
func Init() Config {
var cfg Config
Expand Down
16 changes: 11 additions & 5 deletions internal/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ matrix:
database:
driver: mysql
conn-str: "on-call:secret@tcp(localhost:33060)/on-call?readTimeout=3s&timeout=30s&parseTime=True"
options:
connection-lifetime: "10m"
max-open-connections: 10
max-idle-connections: 5
host: "localhost"
port: 33060
db_name: "on-call"
username: "on-call"
password: "secret"
timeout: "30s"
read_timeout: "3s"
write_timeout: "1s"
connection_lifetime: "10m"
max_open_connections: 10
max_idle_connections: 5
`
27 changes: 11 additions & 16 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,22 @@ import (
"github.com/sirupsen/logrus"
"gorm.io/driver/mysql"
"gorm.io/gorm"

"github.com/snapp-incubator/matrix-on-call-bot/internal/config"
)

const (
healthCheckInterval = 1
maxAttempts = 60
)

// Options represents a struct for creating database connection configurations.
type Options struct {
ConnectionLifetime time.Duration `mapstructure:"connection-lifetime"`
MaxOpenConnections int `mapstructure:"max-open-connections"`
MaxIdleConnections int `mapstructure:"max-idle-connections"`
}

// Create creates a database connection.
func Create(driver string, connStr string, options Options) (*gorm.DB, error) {
func Create(cfg config.Database) (*gorm.DB, error) {
var dialect gorm.Dialector

switch strings.ToLower(driver) {
switch strings.ToLower(cfg.Driver) {
case "mysql":
dialect = mysql.Open(connStr)
dialect = mysql.Open(cfg.MySQLConnectionURI())
//nolint:godox
// TODO: As our migrations are not Postgresql compatible, we don't support postgres for now.
// case "postgres", "postgresql":
Expand All @@ -47,20 +42,20 @@ func Create(driver string, connStr string, options Options) (*gorm.DB, error) {
return nil, errors.Wrap(err, "error in accessing sql DB instance")
}

sqlDB.SetConnMaxLifetime(options.ConnectionLifetime)
sqlDB.SetMaxOpenConns(options.MaxOpenConnections)
sqlDB.SetMaxIdleConns(options.MaxIdleConnections)
sqlDB.SetConnMaxLifetime(cfg.ConnectionLifetime)
sqlDB.SetMaxOpenConns(cfg.MaxOpenConnections)
sqlDB.SetMaxIdleConns(cfg.MaxIdleConnections)

return database, nil
}

// WithRetry provides functionality for having retry for connecting to database.
func WithRetry(
fn func(driver string, connStr string, options Options,
) (*gorm.DB, error), driver string, connStr string, options Options,
fn func(cfg config.Database) (*gorm.DB, error),
cfg config.Database,
) *gorm.DB {
for i := 0; i < maxAttempts; i++ {
db, err := fn(driver, connStr, options)
db, err := fn(cfg)
if err == nil {
return db
}
Expand Down

0 comments on commit 68cd9dc

Please sign in to comment.