Skip to content

Commit

Permalink
feat: allow database pool max connections and timeout to be configure…
Browse files Browse the repository at this point in the history
…d via environment variables
  • Loading branch information
bethesque committed Jul 10, 2020
1 parent 93c9daa commit 2c88f67
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ You can additionally set:
* PACT_BROKER_DATABASE_SSLMODE (optional, possible values: 'disable', 'allow', 'prefer', 'require', 'verify-ca', or 'verify-full' to choose how to treat SSL (only respected if using the postgres database adapter). See https://www.postgresql.org/docs/9.1/libpq-ssl.html for more information.)
* PACT_BROKER_SQL_LOG_LEVEL (optional, defaults to debug. The level at which to log SQL statements.)
* PACT_BROKER_SQL_LOG_WARN_DURATION (optional, defaults to 5 seconds. Log the SQL for queries that take longer than this number of seconds)
* `PACT_BROKER_DATABASE_MAX_CONNECTIONS` - optional, defaults to 4. The maximum size of the connection pool.
* `PACT_BROKER_DATABASE_POOL_TIMEOUT` - optional, 5 seconds by default. The number of seconds to wait if a connection cannot be acquired before raising an error.

## Notes

Expand Down
8 changes: 7 additions & 1 deletion pact_broker/docker_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def database_configuration
encoding: 'utf8',
sslmode: env_or_nil(:database_sslmode),
sql_log_level: (env_or_nil(:sql_log_level) || 'debug').downcase.to_sym,
log_warn_duration: (env_or_nil(:sql_log_warn_duration) || '5').to_f
log_warn_duration: (env_or_nil(:sql_log_warn_duration) || '5').to_f,
max_connections: env_as_integer(:database_max_connections),
pool_timeout: env_as_integer(:database_pool_timeout)
).compact
end

Expand Down Expand Up @@ -79,6 +81,10 @@ def env_or_nil name
env_populated?(name) ? env(name) : nil
end

def env_as_integer name, default = nil
env_populated?(name) ? env(name).to_i : default
end

def default property_name
@default_configuration.send(property_name)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/docker_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@
context "when a port is not supplied" do
its("database_configuration.keys") { are_expected.not_to include :port }
end

context "when the max connections and pool is supplied" do
let(:db_env) do
super().merge(
"PACT_BROKER_DATABASE_MAX_CONNECTIONS" => "2",
"PACT_BROKER_DATABASE_POOL_TIMEOUT" => "7",
)
end

its(:database_configuration) { is_expected.to include max_connections: 2, pool_timeout: 7 }
end
end
end

Expand Down

0 comments on commit 2c88f67

Please sign in to comment.