Skip to content

Commit

Permalink
implement connection pool in gem
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsulc committed Feb 27, 2011
1 parent 3c0c22c commit d09696d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
5 changes: 5 additions & 0 deletions lib/sugarcrm/connection/connection.rb
Expand Up @@ -43,6 +43,11 @@ def login!
@sugar_session_id = login["id"]
raise SugarCRM::LoginError, "Invalid Login" unless logged_in?
end

def logout
logout
@sugar_session_id = nil
end

# Check to see if we are connected
def connected?
Expand Down
26 changes: 20 additions & 6 deletions lib/sugarcrm/connection_pool.rb
@@ -1,22 +1,22 @@
require 'monitor'

module SugarCRM; class ConnectionPool
def initialize(params)
def initialize(session)
@session = session

# The cache of reserved connections mapped to threads
@reserved_connections = {}

# The mutex used to synchronize pool access
@connection_mutex = Monitor.new
@queue = @connection_mutex.new_cond
@timeout = params[:wait_timeout] || 5
@timeout = @session.config[:wait_timeout] || 5

# default max pool size to 5
@size = (params[:pool] && params[:pool].to_i) || 5
@size = (@session.config[:pool] && @session.config[:pool].to_i) || 5

@connections = []
@checked_out = []

@credentials = params[:credentials]
end

# If a connection already exists yield it to the block. If no connection
Expand Down Expand Up @@ -84,6 +84,18 @@ def checkin(conn)
end
end

# Disconnects all connections in the pool, and clears the pool.
def disconnect!
@reserved_connections.each_value do |conn|
checkin conn
end
@reserved_connections = {}
@connections.each do |conn|
conn.logout
end
@connections = []
end

# Return any checked-out connections back to the pool by threads that
# are no longer alive.
def clear_stale_cached_connections!
Expand All @@ -98,7 +110,9 @@ def clear_stale_cached_connections!

private
def new_connection
Connection.new(@credentials[:base_url], @credentials[:username], @credentials[:password])
c = Connection.new(@session.config[:base_url], @session.config[:username], @session.config[:password], @session.config[:options])
c.session = @session
c
end

def checkout_new_connection
Expand Down
14 changes: 9 additions & 5 deletions lib/sugarcrm/session.rb
@@ -1,7 +1,7 @@
# This class hold an individual connection to a SugarCRM server.
# There can be several such simultaneous connections
module SugarCRM; class Session
attr_reader :config, :connection, :extensions_path, :namespace, :namespace_const
attr_reader :config, :extensions_path, :namespace, :namespace_const
attr_accessor :modules
def initialize(url, user, pass, opts={})
options = {
Expand Down Expand Up @@ -61,8 +61,7 @@ def connect(url=nil, user=nil, pass=nil, opts={})
}

SugarCRM::Module.deregister_all(self)
@connection = SugarCRM::Connection.new(@config[:base_url], @config[:username], @config[:password], options) if connection_info_loaded?
@connection.session = self
@connection_pool = SugarCRM::ConnectionPool.new(self)
SugarCRM::Module.register_all(self)
load_extensions
true
Expand All @@ -72,9 +71,14 @@ def connect(url=nil, user=nil, pass=nil, opts={})
alias :reconnect! :connect
alias :reload! :connect

# Returns a connection from the connection pool, if available
def connection
@connection_pool.connection
end

# log out from SugarCRM and cleanup (deregister modules, remove session, etc.)
def disconnect
@connection.logout
@connection_pool.disconnect!
SugarCRM::Module.deregister_all(self)
namespace = @namespace
SugarCRM.instance_eval{ remove_const namespace } # remove NamespaceX from SugarCRM
Expand Down Expand Up @@ -105,7 +109,7 @@ def update_config(params)

# lazy load the SugarCRM version we're connecting to
def sugar_version
@version ||= @connection.get_server_info["version"]
@version ||= connection.get_server_info["version"]
end

private
Expand Down

0 comments on commit d09696d

Please sign in to comment.