oldmoe-neverblock 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_record/connection_adapters/neverblock_postgresql_adapter.rb +9 -17
- data/lib/never_block/db/fibered_db_connection.rb +13 -5
- data/lib/never_block/db/fibered_mysql_connection.rb +1 -1
- data/lib/never_block/pool/fiber_pool.rb +1 -0
- data/lib/never_block/pool/fibered_connection_pool.rb +11 -6
- data/neverblock.gemspec +1 -1
- metadata +1 -1
@@ -9,28 +9,18 @@ class ActiveRecord::ConnectionAdapters::NeverBlockPostgreSQLAdapter < ActiveReco
|
|
9
9
|
'NeverBlockPostgreSQL'
|
10
10
|
end
|
11
11
|
|
12
|
-
def begin_db_transaction
|
13
|
-
@connection.begin_db_transaction
|
14
|
-
end
|
15
|
-
|
16
|
-
def commit_db_transaction
|
17
|
-
@connection.commit_db_transaction
|
18
|
-
end
|
19
|
-
|
20
|
-
def rollback_db_transaction
|
21
|
-
@connection.rollback_db_transaction
|
22
|
-
end
|
23
12
|
# Executes an INSERT query and returns the new record's ID, this wont
|
24
13
|
# work on earlier versions of PostgreSQL but they don't suppor the async
|
25
14
|
# interface anyway
|
26
|
-
def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
|
27
|
-
@connection.exec(sql << " returning id ")
|
28
|
-
end
|
15
|
+
# def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
|
16
|
+
# @connection.exec(sql << " returning id ")
|
17
|
+
# end
|
29
18
|
|
30
19
|
def connect
|
31
|
-
@connection = ::NB::DB::
|
32
|
-
conn =
|
33
|
-
|
20
|
+
@connection = ::NB::DB::PooledDBConnection.new(@connection_parameters[0]) do
|
21
|
+
conn = ::NB::DB::FiberedPostgresConnection.connect(*@connection_parameters[1..(@connection_parameters.length-1)])
|
22
|
+
=begin
|
23
|
+
::NB::DB::FiberedPostgresConnection.translate_results = false if ::NB::DB::FiberedPostgresConnection.respond_to?(:translate_results=)
|
34
24
|
# Ignore async_exec and async_query when using postgres-pr.
|
35
25
|
@async = @config[:allow_concurrency] && @connection.respond_to?(:async_exec)
|
36
26
|
# Use escape string syntax if available. We cannot do this lazily when encountering
|
@@ -59,6 +49,8 @@ class ActiveRecord::ConnectionAdapters::NeverBlockPostgreSQLAdapter < ActiveReco
|
|
59
49
|
end_eval
|
60
50
|
#configure_connection
|
61
51
|
end
|
52
|
+
conn
|
53
|
+
=end
|
62
54
|
end
|
63
55
|
end
|
64
56
|
|
@@ -11,9 +11,11 @@ module NeverBlock
|
|
11
11
|
@fiber = Fiber.current
|
12
12
|
#puts ">>>>>register_with_event_loop fiber #{@fiber.inspect}"
|
13
13
|
# When there's no previous em_connection
|
14
|
-
|
15
|
-
|
14
|
+
key = em_connection_with_pool_key
|
15
|
+
unless @fiber[key]
|
16
|
+
@fiber[key] = EM::attach(socket,EMConnectionHandler,self)
|
16
17
|
@fiber[:callbacks] << self.method(:unregister_from_event_loop)
|
18
|
+
@fiber[:em_keys] << key
|
17
19
|
end
|
18
20
|
else
|
19
21
|
raise ::NB::NBError.new("FiberedDBConnection: EventMachine reactor not running")
|
@@ -23,9 +25,10 @@ module NeverBlock
|
|
23
25
|
# Unattaches the connection socket from the event loop
|
24
26
|
def unregister_from_event_loop
|
25
27
|
#puts ">>>>>unregister_from_event_loop #{self.inspect} #{@fiber.inspect}"
|
26
|
-
|
28
|
+
key = @fiber[:em_keys].pop
|
29
|
+
if em_c = @fiber[key]
|
27
30
|
em_c.detach
|
28
|
-
@fiber[
|
31
|
+
@fiber[key] = nil
|
29
32
|
true
|
30
33
|
else
|
31
34
|
false
|
@@ -41,7 +44,8 @@ module NeverBlock
|
|
41
44
|
|
42
45
|
# Closes the connection using event loop
|
43
46
|
def event_loop_connection_close
|
44
|
-
|
47
|
+
key = em_connection_with_pool_key
|
48
|
+
@fiber[key].close_connection if @fiber[key]
|
45
49
|
end
|
46
50
|
|
47
51
|
# The callback, this is called whenever
|
@@ -50,6 +54,10 @@ module NeverBlock
|
|
50
54
|
@fiber.resume if @fiber
|
51
55
|
end
|
52
56
|
|
57
|
+
private
|
58
|
+
def em_connection_with_pool_key
|
59
|
+
"em_#{@fiber[:current_pool_key]}".intern
|
60
|
+
end
|
53
61
|
end
|
54
62
|
|
55
63
|
module EMConnectionHandler
|
@@ -32,7 +32,7 @@ module NeverBlock
|
|
32
32
|
# the execution in a blocking way.
|
33
33
|
def query(sql)
|
34
34
|
if NB.event_loop_available? && NB.neverblocking?
|
35
|
-
raise ::NB::NBError.new("FiberedMysqlConnection: The running fiber is attached to a connection other than the current one") if (c = Fiber.current[:
|
35
|
+
raise ::NB::NBError.new("FiberedMysqlConnection: The running fiber is attached to a connection other than the current one") if (c = Fiber.current[Fiber.current[:current_pool_key]]) && c != self
|
36
36
|
begin
|
37
37
|
send_query sql
|
38
38
|
Fiber.yield register_with_event_loop
|
@@ -53,7 +53,7 @@ module NeverBlock
|
|
53
53
|
fiber = Fiber.current
|
54
54
|
conn = @connection_proc.call
|
55
55
|
@busy_connections[fiber] = conn
|
56
|
-
fiber[
|
56
|
+
fiber[connection_pool_key] = conn
|
57
57
|
end
|
58
58
|
|
59
59
|
# If a connection is available, pass it to the block, otherwise pass
|
@@ -79,7 +79,8 @@ module NeverBlock
|
|
79
79
|
# queries in environment.rb (Root Fiber)
|
80
80
|
return @connections.first unless fiber[:callbacks]
|
81
81
|
|
82
|
-
|
82
|
+
fiber[:current_pool_key] = connection_pool_key
|
83
|
+
return fiber[connection_pool_key] if fiber[connection_pool_key]
|
83
84
|
conn = if !@connections.empty?
|
84
85
|
@connections.shift
|
85
86
|
elsif (@connections.length + @busy_connections.length) < @size
|
@@ -93,16 +94,16 @@ module NeverBlock
|
|
93
94
|
fiber[:callbacks] << self.method(:release)
|
94
95
|
|
95
96
|
@busy_connections[fiber] = conn
|
96
|
-
fiber[
|
97
|
+
fiber[connection_pool_key] = conn
|
97
98
|
end
|
98
99
|
|
99
100
|
# Give the fiber's connection back to the pool
|
100
101
|
def release()
|
101
102
|
fiber = Fiber.current
|
102
|
-
if fiber[
|
103
|
+
if fiber[connection_pool_key]
|
103
104
|
@busy_connections.delete(fiber)
|
104
|
-
@connections << fiber[
|
105
|
-
fiber[
|
105
|
+
@connections << fiber[connection_pool_key]
|
106
|
+
fiber[connection_pool_key] = nil
|
106
107
|
end
|
107
108
|
end
|
108
109
|
|
@@ -118,6 +119,10 @@ module NeverBlock
|
|
118
119
|
end
|
119
120
|
end
|
120
121
|
|
122
|
+
def connection_pool_key
|
123
|
+
@connection_pool_key ||= "connection_pool_#{object_id}".intern
|
124
|
+
end
|
125
|
+
|
121
126
|
end #FiberedConnectionPool
|
122
127
|
|
123
128
|
end #Pool
|
data/neverblock.gemspec
CHANGED