Skip to content
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

add max open/idle db connection #169

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 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,9 @@ const (
defaultDatabaseSSL = "disable"
defaultDatabaseOption = ""

defaultDatabaseOpenConn = 10
defaultDatabaseMaxIdleConn = 10

// PubSub
defaultPubsubEnable = {{ if .WithPubsub }}true{{ else }}false{{ end }}
defaultPubsubAddress = "pubsub.atlas"
Expand Down Expand Up @@ -113,6 +116,9 @@ 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")


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
11 changes: 10 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 }}
}
setMaxOpenIdleConnection(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,10 @@ func NewGRPCServer(logger *logrus.Logger{{ if or .WithPublishTopic .WithSubscrib

return grpcServer, nil
}

{{ if .WithDatabase }}
// setMaxOpenIdleConnection sets the db connection string
func setMaxOpenIdleConnection(db *gorm.DB) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might call it something like configureDBConns().

It would be nice to set the SetConnMaxIdleTime and SetConnMaxLifetime as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

db.DB().SetMaxOpenConns(viper.GetInt("database.max.open.conns"))
db.DB().SetMaxIdleConns(viper.GetInt("database.max.idle.conns"))
}{{ end }}
Loading