From cdf92d409cbb5c42058249e4be0c9c638cd548e9 Mon Sep 17 00:00:00 2001 From: Cristian Livadaru Date: Wed, 6 May 2015 18:38:30 +0200 Subject: [PATCH 1/2] add pool_timeout and max_connections parameters --- lib/sequella/plugin.rb | 2 ++ lib/sequella/service.rb | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) 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..9e2ded1 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 ## From f15dc73d0e856749874a239910a14724d0c7b111 Mon Sep 17 00:00:00 2001 From: Cristian Livadaru Date: Wed, 6 May 2015 21:04:02 +0200 Subject: [PATCH 2/2] add test --- lib/sequella/service.rb | 2 +- spec/sequella/service_spec.rb | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/sequella/service.rb b/lib/sequella/service.rb index 9e2ded1..331d013 100644 --- a/lib/sequella/service.rb +++ b/lib/sequella/service.rb @@ -11,7 +11,7 @@ def start(config) max_connections = params[:max_connections]||4 pool_timeout = params[:pool_timeout]||5 - @@connection = establish_connection connection_string(params,max_connections,pool_timeout) + @@connection = establish_connection(connection_string(params),max_connections,pool_timeout) require_models(*params.delete(:model_paths)) # Provide Sequel a handle on the Adhearsion logger 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