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

PR #1001: Postgres Connection Pooling Control #1015

Merged
merged 3 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 8 additions & 3 deletions internal/cmd/base/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ type Server struct {
DevTargetSessionMaxSeconds int
DevTargetSessionConnectionLimit int

DatabaseUrl string
DevDatabaseCleanupFunc func() error
DatabaseUrl string
DatabaseMaxOpenConnections int
DevDatabaseCleanupFunc func() error

Database *gorm.DB
}
Expand Down Expand Up @@ -453,7 +454,11 @@ func (b *Server) ConnectToDatabase(dialect string) error {
if err != nil {
return fmt.Errorf("unable to create db object with dialect %s: %w", dialect, err)
}

if b.DatabaseMaxOpenConnections == 1 {
return fmt.Errorf("unable to create db object with dialect %s: %s", dialect, "max_open_connections must be unlimited by setting 0 or at least 2")
} else {
dbase.DB().SetMaxOpenConns(b.DatabaseMaxOpenConnections)
}
b.Database = dbase
if os.Getenv("BOUNDARY_DISABLE_GORM_FORMATTER") == "" {
gorm.LogFormatter = db.GetGormLogFormatter(b.Logger)
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/commands/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ func (c *Command) Run(args []string) int {
c.UI.Error(fmt.Errorf("Error parsing database url: %w", err).Error())
return base.CommandUserError
}
c.DatabaseMaxOpenConnections = c.Config.Controller.Database.MaxOpenConnections

if err := c.ConnectToDatabase("postgres"); err != nil {
c.UI.Error(fmt.Errorf("Error connecting to database: %w", err).Error())
return base.CommandCliError
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ type Worker struct {
}

type Database struct {
Url string `hcl:"url"`
MigrationUrl string `hcl:"migration_url"`
Url string `hcl:"url"`
MigrationUrl string `hcl:"migration_url"`
MaxOpenConnections int `hcl:"max_open_connections"`
}

// DevWorker is a Config that is used for dev mode of Boundary
Expand Down
8 changes: 8 additions & 0 deletions website/content/docs/configuration/controller.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ controller {
description = "An example controller"
database {
url = "postgresql://<username>:<password>@10.0.0.1:5432/<database_name>"
max_open_connections = 2
}
}
```
Expand All @@ -28,6 +29,13 @@ controller {
- `url` - Configures the URL for connecting to Postgres
- `migration_url` - Can be used to specify a different URL for migrations, as that
usually requires higher privileges.
- `max_open_connections` - Can be used to control the maximum number of
connections that can be opened by the controller. The minimum number of
connections required is 2. Every controller requires 1 non-pooled
shared lock connection (which is held for the lifetime of the controller) and
a minimum of 1 pooled connection which is used for all other database
requests. Setting this value to 0 will allow the controller to open as many
connections as needed.

Either can refer to a file on disk (file://) from which a URL will be read; an env
var (env://) from which the URL will be read; or a direct database URL (postgres://).
Expand Down