From 4ae50037631c6160c9e4881345c6ec761a46d9a4 Mon Sep 17 00:00:00 2001 From: Dylan Scott Date: Mon, 3 May 2021 23:42:18 +0100 Subject: [PATCH] PR #1001: Postgres Connection Pooling Control (#1015) --- internal/cmd/base/servers.go | 11 ++++++++--- internal/cmd/commands/server/server.go | 2 ++ internal/cmd/config/config.go | 5 +++-- website/content/docs/configuration/controller.mdx | 8 ++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/internal/cmd/base/servers.go b/internal/cmd/base/servers.go index 6cf5c12ee..6c4edad04 100644 --- a/internal/cmd/base/servers.go +++ b/internal/cmd/base/servers.go @@ -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 } @@ -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) diff --git a/internal/cmd/commands/server/server.go b/internal/cmd/commands/server/server.go index 372f90abd..501d9c6ab 100644 --- a/internal/cmd/commands/server/server.go +++ b/internal/cmd/commands/server/server.go @@ -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 diff --git a/internal/cmd/config/config.go b/internal/cmd/config/config.go index 85d6f6c18..4107a070a 100644 --- a/internal/cmd/config/config.go +++ b/internal/cmd/config/config.go @@ -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 diff --git a/website/content/docs/configuration/controller.mdx b/website/content/docs/configuration/controller.mdx index d4a5d43ce..bce24d765 100644 --- a/website/content/docs/configuration/controller.mdx +++ b/website/content/docs/configuration/controller.mdx @@ -15,6 +15,7 @@ controller { description = "An example controller" database { url = "postgresql://:@10.0.0.1:5432/" + max_open_connections = 2 } } ``` @@ -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://).