Skip to content

Commit

Permalink
Allow expiring of DB connections
Browse files Browse the repository at this point in the history
Add some config parameters which (only when provided) enable the
connection_expiration extension for Sequel. This allows connections to
be closed and recreated after a certain time period (with some random
delay to prevent the entire pool being recycled at once).

This should help DBs that are suffering from performance issues related
to extremely long running connections as the recycling should allow
resources associated with those connections to be freed
  • Loading branch information
andy-paine committed Jul 5, 2021
1 parent 18399f6 commit eba6b1e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/cloud_controller/config_schemas/base/api_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class ApiSchema < VCAP::Config
log_level: String, # debug, info, etc.
log_db_queries: bool,
connection_validation_timeout: Integer,
optional(:connection_expiration_timeout) => Integer,
optional(:connection_expiration_random_delay) => Integer,
optional(:ssl_verify_hostname) => bool,
optional(:ca_cert_path) => String,
},
Expand Down
11 changes: 11 additions & 0 deletions lib/cloud_controller/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def self.connect(opts, logger)
db.sql_log_level = opts[:log_level]
end
db.default_collate = 'utf8_bin' if db.database_type == :mysql
add_connection_expiration_extension(db, opts)
add_connection_validator_extension(db, opts)
db
end
Expand All @@ -48,6 +49,16 @@ def self.add_connection_validator_extension(db, opts)
db.pool.connection_validation_timeout = opts[:connection_validation_timeout] if opts[:connection_validation_timeout]
end

def self.add_connection_expiration_extension(db, opts)
if opts[:connection_expiration_timeout]
db.extension(:connection_expiration)
db.pool.connection_expiration_timeout = opts[:connection_expiration_timeout] if opts[:connection_expiration_timeout]
db.pool.connection_expiration_random_delay = opts[:connection_expiration_random_delay] if opts[:connection_expiration_random_delay]
# So that there are no existing connections without an expiration timestamp
db.disconnect
end
end

def self.load_models(db_config, logger)
db = connect(db_config, logger)
DBMigrator.new(db).wait_for_migrations!
Expand Down

0 comments on commit eba6b1e

Please sign in to comment.