diff --git a/lib/sequella/plugin.rb b/lib/sequella/plugin.rb index 8cdab24..1f795c2 100644 --- a/lib/sequella/plugin.rb +++ b/lib/sequella/plugin.rb @@ -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 diff --git a/lib/sequella/service.rb b/lib/sequella/service.rb index a4b2047..331d013 100644 --- a/lib/sequella/service.rb +++ b/lib/sequella/service.rb @@ -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) require_models(*params.delete(:model_paths)) # Provide Sequel a handle on the Adhearsion logger @@ -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 end ## diff --git a/spec/sequella/service_spec.rb b/spec/sequella/service_spec.rb index 8ebd39e..3578596 100644 --- a/spec/sequella/service_spec.rb +++ b/spec/sequella/service_spec.rb @@ -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) + subject.should_receive(:require_models) + + expect { subject.start connection_params }.to_not raise_error + + end end describe '#connection_string' do