em-postgresql-sequel 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ ["mutex", "fibered_connection_pool", "pgconn", "watcher"].each { |f| require "em-postgresql-sequel/#{f}" }
@@ -0,0 +1,65 @@
1
+ module EM
2
+ module Sequel
3
+
4
+ class FiberedConnectionPool < ::Sequel::ConnectionPool
5
+ def initialize(opts={}, &block)
6
+ super
7
+ @available = []
8
+ @waiting = []
9
+
10
+ Integer(opts[:max_connections] || 4).times do
11
+ @available << make_new(DEFAULT_SERVER)
12
+ end
13
+
14
+ @mutex = Mutex.new
15
+ end
16
+
17
+ def disconnect(opts={}, &block)
18
+ @mutex.synchronize do
19
+ block ||= @disconnection_proc
20
+ if block
21
+ m = Mutex.new
22
+ @available.each do |conn|
23
+ m.synchronize do
24
+ block.call(conn)
25
+ end
26
+ end
27
+ end
28
+ @available.clear
29
+ end
30
+ end
31
+
32
+ def size
33
+ @available.length
34
+ end
35
+
36
+ def hold(server=nil, &blk)
37
+ if @available.empty?
38
+ @waiting << Fiber.current
39
+ Fiber.yield
40
+ end
41
+
42
+ @waiting.delete Fiber.current
43
+ conn = @available.pop
44
+
45
+ begin
46
+ blk.call(conn)
47
+ rescue ::Sequel::DatabaseDisconnectError
48
+ _conn = conn
49
+ conn = nil
50
+ @mutex.synchronize do
51
+ @disconnection_proc.call(_conn) if @disconnection_proc && _conn
52
+ @available << make_new(DEFAULT_SERVER)
53
+ end
54
+ raise
55
+ ensure
56
+ @available << conn if conn
57
+ if waiting = @waiting.shift
58
+ waiting.resume
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,32 @@
1
+ module EM
2
+ module Sequel
3
+ class Mutex
4
+ def initialize
5
+ @waiting = []
6
+ @current = nil
7
+ end
8
+
9
+ def synchronize
10
+ if @current
11
+ if @current == Fiber.current
12
+ raise "already in synchronize"
13
+ else
14
+ @waiting << Fiber.current
15
+ Fiber.yield
16
+ end
17
+ end
18
+
19
+ @current = Fiber.current
20
+
21
+ begin
22
+ yield if block_given?
23
+ ensure
24
+ @current = nil
25
+ if waiting = @waiting.shift
26
+ waiting.resume
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ module Sequel
2
+ module Postgres
3
+ class ::PGconn
4
+ def async_exec(sql, args=nil)
5
+ send_query(sql, args)
6
+
7
+ deferrable = ::EM::DefaultDeferrable.new
8
+ ::EM.watch(self.socket, EM::Sequel::Postgres::Watcher, self, deferrable).notify_readable = true
9
+
10
+ f = Fiber.current
11
+
12
+ deferrable.callback do |res|
13
+ # puts "!!! callback: #{res}"
14
+
15
+ # check for alive?, otherwise we probably resume a dead fiber, because someone has killed our session e.g. "select pg_terminate_backend('procpid');"
16
+ f.resume(res) if f.alive?
17
+ end
18
+
19
+ deferrable.errback do |err|
20
+ # puts "!!! errback: #{err}"
21
+
22
+ # check for alive?, otherwise we probably resume a dead fiber, because someone has killed our session e.g. "select pg_terminate_backend('procpid');"
23
+ f.resume(err) if f.alive?
24
+ end
25
+
26
+ Fiber.yield.tap do |result|
27
+ raise result if result.is_a?(Exception)
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ module EM
2
+ module Sequel
3
+ module Postgres
4
+ module Watcher
5
+ def initialize(client, deferrable)
6
+ @client = client
7
+ @deferrable = deferrable
8
+ end
9
+
10
+ def notify_readable
11
+ detach
12
+ begin
13
+ @client.block
14
+ @deferrable.succeed(@client.get_last_result)
15
+ rescue Exception => e
16
+ @deferrable.fail(e)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-postgresql-sequel
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Zimmek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-22 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: glue together eventmachine, postgresql and sequel
15
+ email: jan.zimmek@web.de
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/em-postgresql-sequel/fibered_connection_pool.rb
21
+ - lib/em-postgresql-sequel/mutex.rb
22
+ - lib/em-postgresql-sequel/pgconn.rb
23
+ - lib/em-postgresql-sequel/watcher.rb
24
+ - lib/em-postgresql-sequel.rb
25
+ homepage:
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.6
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: glue together eventmachine, postgresql and sequel
49
+ test_files: []