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 pool_timeout and max_connections parameters #11

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lib/sequella/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Plugin < Adhearsion::Plugin
host 'localhost' , :desc => 'host where the database is running'
port 3306 , :desc => 'port where the database is listening'
model_paths ['app/models'] , :desc => 'paths to model files to load', :transform => Proc.new {|v| Array(v)}
max_connections 4 , :desc => 'The maximum number of connections the connection pool will open'
pool_timeout 5 , :desc => ' The amount of seconds to wait to acquire a connection before raising a PoolTimeoutError'
end

run :sequella do
Expand Down
9 changes: 6 additions & 3 deletions lib/sequella/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ class << self
# Start the Sequel connection with the configured database
def start(config)
params = config.to_hash.select { |k, v| !v.nil? }
# using sequel default values.
max_connections = params[:max_connections]||4
pool_timeout = params[:pool_timeout]||5

@@connection = establish_connection connection_string(params)
@@connection = establish_connection(connection_string(params),max_connections,pool_timeout)
Copy link
Owner

Choose a reason for hiding this comment

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

Tiny nit, but would you please add spaces after the commas?

Copy link
Owner

Choose a reason for hiding this comment

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

Also, it occurs to me that it might be better to pass this as a hash to #establish_connection, in case we get more params in the future. The calling code would then look something like

@@connection = establish_connection connection_string(params), max_connections: max_connections, pool_timeout: pool_timeout

require_models(*params.delete(:model_paths))

# Provide Sequel a handle on the Adhearsion logger
Expand Down Expand Up @@ -52,9 +55,9 @@ def qualify_path(path)
# Start the Sequel connection with the configured database
#
# @param connection_uri [String] Connection URI for connecting to the database
def establish_connection(connection_uri)
def establish_connection(connection_uri, max_connections, pool_timeout)
logger.info "Sequella connecting: #{connection_uri}"
::Sequel.connect connection_uri
::Sequel.connect connection_uri, :max_connections => max_connections, :pool_timeout => pool_timeout
Copy link
Owner

Choose a reason for hiding this comment

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

Please use Ruby 1.9 hash syntax (eg. pool_timeout: pool_timeout)

end

##
Expand Down
20 changes: 19 additions & 1 deletion spec/sequella/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,29 @@
describe '#start' do
it 'should not raise an error if start attempted with a uri specified' do
config = OpenStruct.new uri: 'postgres://user:password@localhost/blog'
subject.should_receive(:establish_connection).with(config.uri)
subject.should_receive(:establish_connection).with(config.uri,4,5)
subject.should_receive(:require_models)

expect { subject.start config.marshal_dump }.to_not raise_error
end
it 'should pass configured pool value' do
connection_params = {
adapter: 'postgres',
host: 'localhost',
port: 5432,
database: 'test',
username: 'test-user',
password: 'password',
max_connections: 20,
pool_timeout: 10
}

subject.should_receive(:establish_connection).with('postgres://test-user:password@localhost:5432/test',20,10)
Copy link
Owner

Choose a reason for hiding this comment

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

It would be better to place the expectation on the ::Sequel.connect method, since that's what we actually care about

subject.should_receive(:require_models)

expect { subject.start connection_params }.to_not raise_error

end
end

describe '#connection_string' do
Expand Down