sync-em-pg 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +10 -0
- data/lib/sync-em/pg.rb +28 -0
- data/lib/sync-em/pg/sequel.rb +92 -0
- data/lib/sync-em/version.rb +7 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/sync-em-pg/pg/sequel_spec.rb +81 -0
- data/spec/sync-em-pg/pg_spec.rb +13 -0
- data/sync-em-pg.gemspec +23 -0
- metadata +108 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Petr Yanovich
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
sync-em-pg
|
2
|
+
===========
|
3
|
+
|
4
|
+
Simple wrapper around [em-pg](https://github.com/prepor/em-pg) for [em-synchrony](https://github.com/igrigorik/em-synchrony). And [Sequel](http://sequel.rubyforge.org/) adapter.
|
5
|
+
|
6
|
+
You can also look on [green-em-pg](https://github.com/prepor/green-em-pg) for [Green](https://github.com/prepor/green).
|
7
|
+
|
8
|
+
Usage
|
9
|
+
-----
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "sync-em-pg"
|
13
|
+
```
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require "sync-em/pg"
|
17
|
+
EM.synchrony do
|
18
|
+
db = Sync::EM::PG.new host: "localhost",
|
19
|
+
port: 5432,
|
20
|
+
dbname: "test",
|
21
|
+
user: "postgres",
|
22
|
+
password: "postgres"
|
23
|
+
|
24
|
+
res = db.send_qeury "select * from test"
|
25
|
+
|
26
|
+
puts res.inspect
|
27
|
+
|
28
|
+
EM.stop
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
Sequel
|
33
|
+
------
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require "sync-em/pg/sequel"
|
37
|
+
EM.synchrony do
|
38
|
+
url = "postgres://postgres:postgres@localhost:5432/test"
|
39
|
+
db = Sequel.connect(url, pool_class: Sync::EM::PG::Sequel::ConnectionPool)
|
40
|
+
|
41
|
+
puts db[:test].all.inspect
|
42
|
+
|
43
|
+
EM.stop
|
44
|
+
end
|
45
|
+
```
|
data/Rakefile
ADDED
data/lib/sync-em/pg.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'em/pg'
|
2
|
+
require 'em-synchrony'
|
3
|
+
|
4
|
+
module Sync
|
5
|
+
module EM
|
6
|
+
class PG < ::EM::PG
|
7
|
+
|
8
|
+
def self._sync
|
9
|
+
f = Fiber.current
|
10
|
+
resp = yield
|
11
|
+
resp.callback{ |r| f.resume(r) }
|
12
|
+
resp.errback{ |r| f.resume(r) }
|
13
|
+
Fiber.yield
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(*args)
|
17
|
+
super
|
18
|
+
PG._sync{ self }
|
19
|
+
end
|
20
|
+
|
21
|
+
[:send_query, :send_prepare, :send_query_prepared, :send_describe_prepared, :send_describe_portal].each do |m|
|
22
|
+
define_method(m) do |*args|
|
23
|
+
PG._sync{ super(*args) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'pg'
|
2
|
+
require 'forwardable'
|
3
|
+
require 'sync-em/pg'
|
4
|
+
require 'sequel'
|
5
|
+
require 'em-synchrony/connection_pool'
|
6
|
+
|
7
|
+
module Sync
|
8
|
+
module EM
|
9
|
+
class PG
|
10
|
+
|
11
|
+
class ReConnectionPool < EventMachine::Synchrony::ConnectionPool
|
12
|
+
def initialize(opts, &block)
|
13
|
+
@magick = block
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def acquire(fiber)
|
18
|
+
if conn = @available.pop
|
19
|
+
conn = @magick.call if conn.state != :connected
|
20
|
+
@reserved[fiber.object_id] = conn
|
21
|
+
conn
|
22
|
+
else
|
23
|
+
Fiber.yield @pending.push fiber
|
24
|
+
acquire(fiber)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Sequel
|
30
|
+
class ConnectionPool < ::Sequel::ConnectionPool
|
31
|
+
DEFAULT_SIZE = 4
|
32
|
+
attr_accessor :pool
|
33
|
+
def initialize(db, opts = {})
|
34
|
+
super
|
35
|
+
size = opts[:max_connection] || DEFAULT_SIZE
|
36
|
+
@pool = ReConnectionPool.new(size: size) do
|
37
|
+
make_new(DEFAULT_SERVER)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def size
|
42
|
+
@pool.available.size
|
43
|
+
end
|
44
|
+
|
45
|
+
def hold(server = nil, &blk)
|
46
|
+
@pool.execute(false, &blk)
|
47
|
+
end
|
48
|
+
|
49
|
+
def disconnect(server = nil)
|
50
|
+
@pool.available.each{ |conn| db.disconnect_connection(conn) }
|
51
|
+
@pool.available.clear
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class PGconn < Sync::EM::PG
|
57
|
+
extend Forwardable
|
58
|
+
|
59
|
+
def_delegators :@pg, :status, :escape_string, :escape_bytea
|
60
|
+
|
61
|
+
class << self
|
62
|
+
alias :connect :new
|
63
|
+
end
|
64
|
+
|
65
|
+
{ async_exec: :send_query,
|
66
|
+
prepare: :send_prepare,
|
67
|
+
exec_prepared: :send_query_prepared,
|
68
|
+
finish: :close }.each do |from, to|
|
69
|
+
define_method from do |*args|
|
70
|
+
send(to, *args)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
# alias :async_exec :send_query
|
74
|
+
# alias :prepare :send_prepare
|
75
|
+
# alias :exec_prepared :send_query_prepared
|
76
|
+
# alias :finish :close
|
77
|
+
|
78
|
+
[:get_copy_data, :put_copy_data, :put_copy_end, :get_result, :wait_for_notify].each do |m|
|
79
|
+
define_method(m) do |*args|
|
80
|
+
raise "Unimplemented method #{m} in green postgres adapter"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
PGconn = Sync::EM::PG::PGconn
|
89
|
+
|
90
|
+
require 'sequel/adapters/postgres'
|
91
|
+
|
92
|
+
Sequel::Postgres::CONVERTED_EXCEPTIONS << ::EM::PG::Error
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'sync-em/pg'
|
5
|
+
|
6
|
+
require 'logger'
|
7
|
+
require 'minitest/spec'
|
8
|
+
require 'minitest/autorun'
|
9
|
+
require 'minitest/reporters'
|
10
|
+
|
11
|
+
logger = Logger.new nil
|
12
|
+
|
13
|
+
DB_CONFIG = {
|
14
|
+
host: "localhost",
|
15
|
+
port: 5432,
|
16
|
+
dbname: "petr",
|
17
|
+
user: "petr",
|
18
|
+
password: "petr_passwd",
|
19
|
+
}
|
20
|
+
|
21
|
+
EM::PG.logger = logger
|
22
|
+
|
23
|
+
DB_URL = "postgres://%s:%s@%s:%d/%s" % [DB_CONFIG[:user], DB_CONFIG[:password], DB_CONFIG[:host], DB_CONFIG[:port], DB_CONFIG[:dbname]]
|
24
|
+
|
25
|
+
MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sync-em/pg/sequel'
|
3
|
+
require 'em-synchrony/fiber_iterator'
|
4
|
+
# require 'green/group'
|
5
|
+
|
6
|
+
describe Sync::EM::PG do
|
7
|
+
DELAY = 1
|
8
|
+
QUERY = "select pg_sleep(#{DELAY})"
|
9
|
+
|
10
|
+
|
11
|
+
let(:url) { DB_URL }
|
12
|
+
let(:size) { 1 }
|
13
|
+
let(:db) { Sequel.connect(url, max_connection: size, pool_class: Sync::EM::PG::Sequel::ConnectionPool, db_logger: Logger.new(nil)) }
|
14
|
+
let(:test) { db[:test] }
|
15
|
+
|
16
|
+
before do
|
17
|
+
EM.synchrony do
|
18
|
+
db.create_table!(:test) do
|
19
|
+
text :name
|
20
|
+
integer :value, index: true
|
21
|
+
end
|
22
|
+
|
23
|
+
EM.stop
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
after do
|
28
|
+
EM.synchrony do
|
29
|
+
db.drop_table?(:test)
|
30
|
+
|
31
|
+
EM.stop
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should connect and execute query" do
|
36
|
+
EM.synchrony do
|
37
|
+
test.insert name: "andrew", value: 42
|
38
|
+
test.where(name: "andrew").first[:value].must_equal 42
|
39
|
+
|
40
|
+
EM.stop
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
describe "pool size is exceeded" do
|
46
|
+
let(:size) { 1 }
|
47
|
+
it "should queue requests" do
|
48
|
+
EM.synchrony do
|
49
|
+
start = Time.now.to_f
|
50
|
+
|
51
|
+
res = []
|
52
|
+
EM::Synchrony::FiberIterator.new([1,2], 2).each do |t|
|
53
|
+
res << db[QUERY].all
|
54
|
+
end
|
55
|
+
(Time.now.to_f - start.to_f).must_be_within_delta DELAY * 2, DELAY * 2 * 0.15
|
56
|
+
res.size.must_equal 2
|
57
|
+
|
58
|
+
EM.stop
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "pool size is enough" do
|
64
|
+
let(:size) { 2 }
|
65
|
+
it "should parallel requests" do
|
66
|
+
EM.synchrony do
|
67
|
+
start = Time.now.to_f
|
68
|
+
|
69
|
+
res = []
|
70
|
+
EM::Synchrony::FiberIterator.new([1,2], 2).each do |t|
|
71
|
+
res << db[QUERY].all
|
72
|
+
end
|
73
|
+
|
74
|
+
(Time.now.to_f - start.to_f).must_be_within_delta DELAY, DELAY * 0.30
|
75
|
+
res.size.must_equal 2
|
76
|
+
|
77
|
+
EM.stop
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/sync-em-pg.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sync-em/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "sync-em-pg"
|
8
|
+
gem.version = Sync::Em::Pg::VERSION
|
9
|
+
gem.authors = ["Petr Yanovich"]
|
10
|
+
gem.email = ["fl00r@yandex.ru"]
|
11
|
+
gem.description = %q{em-pg on fibers}
|
12
|
+
gem.summary = %q{em-pg on fibers}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "em-synchrony"
|
21
|
+
gem.add_dependency "pg"
|
22
|
+
gem.add_dependency "em-pg"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sync-em-pg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Petr Yanovich
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: em-synchrony
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: pg
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: em-pg
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: em-pg on fibers
|
63
|
+
email:
|
64
|
+
- fl00r@yandex.ru
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/sync-em/pg.rb
|
75
|
+
- lib/sync-em/pg/sequel.rb
|
76
|
+
- lib/sync-em/version.rb
|
77
|
+
- spec/spec_helper.rb
|
78
|
+
- spec/sync-em-pg/pg/sequel_spec.rb
|
79
|
+
- spec/sync-em-pg/pg_spec.rb
|
80
|
+
- sync-em-pg.gemspec
|
81
|
+
homepage: ''
|
82
|
+
licenses: []
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.24
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: em-pg on fibers
|
105
|
+
test_files:
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
- spec/sync-em-pg/pg/sequel_spec.rb
|
108
|
+
- spec/sync-em-pg/pg_spec.rb
|