Skip to content

Commit

Permalink
add db connection configuration parameters (#169)
Browse files Browse the repository at this point in the history
* add max open/idle db connection

* set other config defaults for db
  • Loading branch information
chinmayb authored Nov 14, 2024
1 parent 9b3ab52 commit 121ccc9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions atlas/templates/cmd/server/config.go.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const (
defaultDatabaseSSL = "disable"
defaultDatabaseOption = ""

defaultDatabaseOpenConn = 10
defaultDatabaseMaxIdleConn = 10
defaultDatabaseMaxConnLifetime = 30 * time.Minute
defaultDatabaseMaxConnIdleTime = 10 * time.Minute


// PubSub
defaultPubsubEnable = {{ if .WithPubsub }}true{{ else }}false{{ end }}
defaultPubsubAddress = "pubsub.atlas"
Expand Down Expand Up @@ -113,6 +119,11 @@ var (
flagDatabasePassword = pflag.String("database.password", defaultDatabasePassword, "database password")
flagDatabaseSSL = pflag.String("database.ssl", defaultDatabaseSSL, "database ssl mode")
flagDatabaseOption = pflag.String("database.option", defaultDatabaseOption, "define custom option to db driver")
flagDatabaseMaxOpenConns = pflag.Int("database.max.open.conns", defaultDatabaseOpenConn, "Database max open connections")
flagDatabaseMaxIdleConns = pflag.Int("database.max.idle.conns", defaultDatabaseMaxIdleConn, "Database max idle connections")
flagDatabaseConnLifeTime = pflag.Duration("database.max.conn.lifetime", defaultDatabaseMaxConnLifetime, "Database max connection lifetime")
flagDatabasMaxIdleTime = pflag.Duration("database.max.conn.idle.time", defaultDatabaseMaxConnIdleTime, "Database max connection idle time")
flagDatabaseDebugMode = pflag.Bool("database.log.mode", false, "enable database query log mode")

flagPubsubEnable = pflag.Bool("atlas.pubsub.enable", defaultPubsubEnable, "enable application with pubsub")
flagPubsubAddress = pflag.String("atlas.pubsub.address", defaultPubsubAddress, "address or FQDN of the pubsub service")
Expand Down
14 changes: 13 additions & 1 deletion atlas/templates/cmd/server/grpc.go.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func NewGRPCServer(logger *logrus.Logger{{ if or .WithPublishTopic .WithSubscrib
db, err := gorm.Open("postgres", dbConnectionString)
if err != nil {
return nil, err
}{{ end }}
}
configureDBConns(db)
{{ end }}
// register service implementation with the grpcServer
s, err := svc.NewBasicServer({{ if or .WithPublishTopic .WithSubscribeTopic }}pubsub{{ if .WithDatabase }}, {{ end }}{{ end }}{{ if .WithDatabase }}db{{ end }})
if err != nil {
Expand All @@ -62,3 +64,13 @@ func NewGRPCServer(logger *logrus.Logger{{ if or .WithPublishTopic .WithSubscrib

return grpcServer, nil
}

{{ if .WithDatabase }}
// configureDBConns sets the db connection string
func configureDBConns(db *gorm.DB) {
db.LogMode(viper.GetBool("database.log.mode"))
db.DB().SetMaxOpenConns(viper.GetInt("database.max.open.conns"))
db.DB().SetMaxIdleConns(viper.GetInt("database.max.idle.conns"))
db.DB().SetConnMaxLifetime(viper.GetDuration("database.max.conn.lifetime"))
db.DB().SetConnMaxIdleTime(viper.GetDuration("database.max.conn.idle.time"))
}{{ end }}

0 comments on commit 121ccc9

Please sign in to comment.