I want a connection pool for mysql objects that I would use with the mysql C adapter and mysqlplus. But I was also looking to make it as flexible as possible so that it can be a pool of object for more than just mysql connections. I'm looking for advice on how to implement that, I currently have 2 implementations above. I read the sequel connection_pool.rb file but I don't understand everything so stripping the Sequel part is kind of a problem for me. Do you guys have any advice/opinion on how I could achieve this?
Created
October 28, 2009 21:48
-
-
Save vroy/220885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
require 'thread' | |
require 'timeout' | |
class Stored | |
def initialize | |
@iid = object_id | |
puts "#{@iid}: Created." | |
end | |
def use | |
sleep(1) | |
puts "#{@iid}: Used." | |
end | |
end | |
class Pool | |
def initialize(size, proc) | |
@objects, @proc = [], proc | |
@mutex = Mutex.new | |
1.upto(size) do |i| | |
@objects << proc.call | |
end | |
end | |
def get | |
o = nil | |
until !o.nil? | |
@mutex.synchronize { | |
o = @objects.pop | |
} | |
sleep(0.1) if o.nil? | |
end | |
yield o | |
@mutex.synchronize { | |
@objects.push(o) | |
} | |
#TODO: Handle exceptions and create a new object if needed. | |
end | |
end | |
pool = Pool.new(3, lambda{ Stored.new() } ) | |
a = Time.now | |
threads = [] | |
1.upto(10) do |j| | |
threads << Thread.new do | |
pool.get do |i| | |
i.use | |
end | |
end | |
end | |
threads.each{|t| t.join } | |
puts (Time.now-a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
require 'thread' | |
require 'timeout' | |
class Stored | |
def initialize | |
@iid = object_id | |
puts "#{@iid}: Created." | |
end | |
def use | |
sleep(1) | |
puts "#{@iid}: Used." | |
end | |
end | |
class Pool | |
def initialize(max_size, proc) | |
@max_size, @proc = max_size, proc | |
@objects, @created = [], 0 | |
@mutex = Mutex.new | |
end | |
def get | |
until o = acquire | |
sleep 0.1 | |
end | |
yield o | |
release(o) | |
#TODO: rescue exceptions and re-create an object if needed. | |
end | |
private | |
# Tries to get an object of the @objects array or create one. | |
def acquire | |
o = nil | |
@mutex.synchronize do | |
o = @objects.pop | |
if o.nil? and @created < @max_size | |
@objects << @proc.call | |
@created += 1 | |
end | |
end | |
o | |
end | |
def release(o) | |
@mutex.synchronize{ @objects.push(o) } | |
end | |
end | |
pool = Pool.new(3, lambda{ Stored.new() } ) | |
a = Time.new | |
threads = [] | |
1.upto(10) do |j| | |
threads << Thread.new do | |
pool.get do |i| | |
i.use | |
end | |
end | |
end | |
threads.each{|t| t.join } | |
puts (Time.now-a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment