Class: Async::MySQL::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/async/mysql/pool.rb

Overview

This pool doesn’t impose a maximum number of open resources, but it WILL block if there are no available resources and trying to allocate another one fails.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Pool

Returns a new instance of Pool.



65
66
67
68
69
70
# File 'lib/async/mysql/pool.rb', line 65

def initialize(&block)
	@available = []
	@waiting = []
	
	@constructor = block
end

Instance Method Details

#acquireObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/async/mysql/pool.rb', line 72

def acquire
	resource = wait_for_next_available
	
	begin
		yield resource
	ensure
		@available << resource
		
		if task = @waiting.pop
			task.resume
		end
	end
end

#closeObject



86
87
88
89
# File 'lib/async/mysql/pool.rb', line 86

def close
	@available.each(&:close)
	@available.clear
end

#next_availableObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/async/mysql/pool.rb', line 100

def next_available
	if @available.empty?
		return @constructor.call # This might fail, which is okay :)
	else
		return @available.pop
	end
rescue StandardError
	$stderr.puts $!.inspect
	return nil
end

#wait_for_next_availableObject



91
92
93
94
95
96
97
98
# File 'lib/async/mysql/pool.rb', line 91

def wait_for_next_available
	until resource = next_available
		@waiting << Fiber.current
		Task.yield
	end
	
	return resource
end