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.
@@ -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::PooledFiberedPostgresConnection.new(@connection_options[0]) do
32
- conn = PGconn.connect(*@connection_options[1..(@connection_options.length-1)])
33
- PGconn.translate_results = false if PGconn.respond_to?(:translate_results=)
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
- unless @fiber[:em_connection]
15
- @fiber[:em_connection] = EM::attach(socket,EMConnectionHandler,self)
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
- if em_c = @fiber[:em_connection]
28
+ key = @fiber[:em_keys].pop
29
+ if em_c = @fiber[key]
27
30
  em_c.detach
28
- @fiber[:em_connection] = nil
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
- @fiber[:em_connection].close_connection if @fiber[:em_connection]
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[:connection]) && c != self
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
@@ -48,6 +48,7 @@ module NeverBlock
48
48
  end
49
49
  end
50
50
  fiber[:callbacks] = []
51
+ fiber[:em_keys] = []
51
52
  fiber[:neverblock] = true
52
53
  @fibers << fiber
53
54
  end
@@ -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[:connection] = conn
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
- return fiber[:connection] if fiber[:connection]
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[:connection] = conn
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[:connection]
103
+ if fiber[connection_pool_key]
103
104
  @busy_connections.delete(fiber)
104
- @connections << fiber[:connection]
105
- fiber[:connection] = nil
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "neverblock"
3
- s.version = "0.1.5"
3
+ s.version = "0.1.6"
4
4
  s.date = "2008-11-06"
5
5
  s.summary = "Utilities for non-blocking stack components"
6
6
  s.email = "oldmoe@gmail.com"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oldmoe-neverblock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muhammad A. Ali