em-synchrony 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +173 -173
- data/em-synchrony.gemspec +1 -1
- data/lib/active_record/connection_adapters/em_mysql2_adapter.rb +47 -47
- data/lib/em-synchrony.rb +1 -1
- data/lib/em-synchrony/activerecord.rb +102 -102
- data/lib/em-synchrony/amqp.rb +180 -180
- data/lib/em-synchrony/em-hiredis.rb +103 -103
- data/lib/em-synchrony/tcpsocket.rb +157 -28
- data/spec/activerecord_spec.rb +108 -108
- data/spec/amqp_spec.rb +146 -146
- data/spec/tcpsocket_spec.rb +401 -18
- data/spec/timer_spec.rb +7 -0
- metadata +10 -5
data/README.md
CHANGED
@@ -1,174 +1,174 @@
|
|
1
|
-
# EM-Synchrony
|
2
|
-
|
3
|
-
Collection of convenience classes and primitives to help untangle evented code, plus a number of patched EM clients to make them Fiber aware. To learn more, please see: [Untangling Evented Code with Ruby Fibers](http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers).
|
4
|
-
|
5
|
-
* Fiber aware ConnectionPool with sync/async query support
|
6
|
-
* Fiber aware Iterator to allow concurrency control & mixing of sync / async
|
7
|
-
* Fiber aware async inline support: turns any async function into sync
|
8
|
-
* Fiber aware Multi-request interface for any callback enabled clients
|
9
|
-
* Fiber aware TCPSocket replacement, powered by EventMachine
|
10
|
-
* Fiber aware Thread, Mutex, ConditionVariable clases
|
11
|
-
* Fiber aware sleep
|
12
|
-
|
13
|
-
Supported clients:
|
14
|
-
|
15
|
-
* [mysql2](http://github.com/igrigorik/em-synchrony/blob/master/spec/mysql2_spec.rb): .query is synchronous, while .aquery is async (see specs)
|
16
|
-
* [activerecord](http://github.com/igrigorik/em-synchrony/blob/master/spec/activerecord_spec.rb): require synchrony/activerecord, set your AR adapter to em_mysql2 and you should be good to go
|
17
|
-
* [em-http-request](http://github.com/igrigorik/em-synchrony/blob/master/spec/http_spec.rb): .get, etc are synchronous, while .aget, etc are async
|
18
|
-
* [em-memcached](http://github.com/igrigorik/em-synchrony/blob/master/spec/memcache_spec.rb) & [remcached](http://github.com/igrigorik/em-synchrony/blob/master/spec/remcached_spec.rb): .get, etc, and .multi_* methods are synchronous
|
19
|
-
* [em-mongo](http://github.com/igrigorik/em-synchrony/blob/master/spec/em-mongo_spec.rb): .find, .first are synchronous
|
20
|
-
* [mongoid](http://github.com/igrigorik/em-synchrony/blob/master/spec/mongo_spec.rb): all functions synchronous, plus Rails compatibility
|
21
|
-
* em-jack: a[method]'s are async, and all regular jack method's are synchronous
|
22
|
-
* [AMQP](http://github.com/ruby-amqp/amqp): most of functions are synchronous (see specs)
|
23
|
-
|
24
|
-
Other clients with native Fiber support:
|
25
|
-
|
26
|
-
* redis: contains [synchrony code](https://github.com/ezmobius/redis-rb/blob/master/test/synchrony_driver.rb) right within the driver
|
27
|
-
* synchrony also supports [em-redis](http://github.com/igrigorik/em-synchrony/blob/master/spec/redis_spec.rb) and em-hiredis (see specs), but unless you specifically need either of those, use the official redis gem
|
28
|
-
|
29
|
-
## Fiber-aware Iterator: mixing sync / async code
|
30
|
-
|
31
|
-
Allows you to perform each, map, inject on a collection of any asynchronous tasks. To advance the iterator, simply call iter.next, or iter.return(result). The iterator will not exit until you advance through the entire collection. Additionally, you can specify the desired concurrency level! Ex: crawling a web-site, but you want to have at most 5 connections open at any one time.
|
32
|
-
|
33
|
-
```ruby
|
34
|
-
require "em-synchrony"
|
35
|
-
require "em-synchrony/em-http"
|
36
|
-
|
37
|
-
EM.synchrony do
|
38
|
-
concurrency = 2
|
39
|
-
urls = ['http://url.1.com', 'http://url2.com']
|
40
|
-
|
41
|
-
# iterator will execute async blocks until completion, .each, .inject also work!
|
42
|
-
results = EM::Synchrony::Iterator.new(urls, concurrency).map do |url, iter|
|
43
|
-
|
44
|
-
# fire async requests, on completion advance the iterator
|
45
|
-
http = EventMachine::HttpRequest.new(url).aget
|
46
|
-
http.callback { iter.return(http) }
|
47
|
-
http.errback { iter.return(http) }
|
48
|
-
end
|
49
|
-
|
50
|
-
p results # all completed requests
|
51
|
-
EventMachine.stop
|
52
|
-
end
|
53
|
-
```
|
54
|
-
|
55
|
-
Or, you can use FiberIterator to hide the async nature of em-http:
|
56
|
-
|
57
|
-
```ruby
|
58
|
-
require "em-synchrony"
|
59
|
-
require "em-synchrony/em-http"
|
60
|
-
require "em-synchrony/fiber_iterator"
|
61
|
-
|
62
|
-
EM.synchrony do
|
63
|
-
concurrency = 2
|
64
|
-
urls = ['http://url.1.com', 'http://url2.com']
|
65
|
-
results = []
|
66
|
-
|
67
|
-
EM::Synchrony::FiberIterator.new(urls, concurrency).each do |url|
|
68
|
-
resp = EventMachine::HttpRequest.new(url).get
|
69
|
-
results.push resp.response
|
70
|
-
end
|
71
|
-
|
72
|
-
p results # all completed requests
|
73
|
-
EventMachine.stop
|
74
|
-
end
|
75
|
-
```
|
76
|
-
|
77
|
-
## Fiber-aware ConnectionPool shared by a fiber:
|
78
|
-
Allows you to create a pool of resources which are then shared by one or more fibers. A good example is a collection of long-lived database connections. The pool will automatically block and wake up the fibers as the connections become available.
|
79
|
-
|
80
|
-
```ruby
|
81
|
-
require "em-synchrony"
|
82
|
-
require "em-synchrony/mysql2"
|
83
|
-
|
84
|
-
EventMachine.synchrony do
|
85
|
-
db = EventMachine::Synchrony::ConnectionPool.new(size: 2) do
|
86
|
-
Mysql2::EM::Client.new
|
87
|
-
end
|
88
|
-
|
89
|
-
multi = EventMachine::Synchrony::Multi.new
|
90
|
-
multi.add :a, db.aquery("select sleep(1)")
|
91
|
-
multi.add :b, db.aquery("select sleep(1)")
|
92
|
-
res = multi.perform
|
93
|
-
|
94
|
-
p "Look ma, no callbacks, and parallel MySQL requests!"
|
95
|
-
p res
|
96
|
-
|
97
|
-
EventMachine.stop
|
98
|
-
end
|
99
|
-
```
|
100
|
-
|
101
|
-
## Fiber-aware Multi interface: parallel HTTP requests
|
102
|
-
Allows you to fire simultaneous requests and wait for all of them to complete (success or error) before advancing. Concurrently fetching many HTTP pages at once is a good example; parallel SQL queries is another. Technically, this functionality can be also achieved by using the Synchrony Iterator shown above.
|
103
|
-
|
104
|
-
```ruby
|
105
|
-
require "em-synchrony"
|
106
|
-
require "em-synchrony/em-http"
|
107
|
-
EventMachine.synchrony do
|
108
|
-
multi = EventMachine::Synchrony::Multi.new
|
109
|
-
multi.add :a, EventMachine::HttpRequest.new("http://www.postrank.com").aget
|
110
|
-
multi.add :b, EventMachine::HttpRequest.new("http://www.postrank.com").apost
|
111
|
-
res = multi.perform
|
112
|
-
|
113
|
-
p "Look ma, no callbacks, and parallel HTTP requests!"
|
114
|
-
p res
|
115
|
-
|
116
|
-
EventMachine.stop
|
117
|
-
end
|
118
|
-
```
|
119
|
-
|
120
|
-
## Fiber-aware & EventMachine backed TCPSocket:
|
121
|
-
This is dangerous territory - you've been warned. You can patch your base TCPSocket class to make any/all libraries depending on TCPSocket be actually powered by EventMachine and Fibers under the hood.
|
122
|
-
|
123
|
-
```ruby
|
124
|
-
require "em-synchrony"
|
125
|
-
require "lib/em-synchrony"
|
126
|
-
require "net/http"
|
127
|
-
|
128
|
-
EM.synchrony do
|
129
|
-
# replace default Socket code to use EventMachine Sockets instead
|
130
|
-
TCPSocket = EventMachine::Synchrony::TCPSocket
|
131
|
-
|
132
|
-
Net::HTTP.get_print 'www.google.com', '/index.html'
|
133
|
-
EM.stop
|
134
|
-
end
|
135
|
-
```
|
136
|
-
|
137
|
-
## Inline synchronization & Fiber sleep:
|
138
|
-
Allows you to inline/synchronize any callback interface to behave as if it was a blocking call. Simply pass any callback object to Synchrony.sync and it will do the right thing: the fiber will be resumed once the callback/errback fires. Likewise, use Synchrony.sleep to avoid blocking the main thread if you need to put one of your workers to sleep.
|
139
|
-
|
140
|
-
```ruby
|
141
|
-
require "em-synchrony"
|
142
|
-
require "em-synchrony/em-http"
|
143
|
-
EM.synchrony do
|
144
|
-
# pass a callback enabled client to sync to automatically resume it when callback fires
|
145
|
-
result = EM::Synchrony.sync EventMachine::HttpRequest.new('http://www.gooogle.com/').aget
|
146
|
-
p result
|
147
|
-
|
148
|
-
# pause execution for 2 seconds
|
149
|
-
EM::Synchrony.sleep(2)
|
150
|
-
|
151
|
-
EM.stop
|
152
|
-
end
|
153
|
-
```
|
154
|
-
|
155
|
-
## Async ActiveRecord:
|
156
|
-
|
157
|
-
Allows you to use async ActiveRecord within Rails and outside of Rails (see [async-rails](https://github.com/igrigorik/async-rails)). If you need to control the connection pool size, use [rack/fiber_pool](https://github.com/mperham/rack-fiber_pool/).
|
158
|
-
|
159
|
-
```ruby
|
160
|
-
require "em-synchrony"
|
161
|
-
require "em-synchrony/mysql2"
|
162
|
-
require "em-synchrony/activerecord"
|
163
|
-
|
164
|
-
ActiveRecord::Base.establish_connection(
|
165
|
-
:adapter => 'em_mysql2',
|
166
|
-
:database => 'widgets'
|
167
|
-
)
|
168
|
-
|
169
|
-
result = Widget.all.to_a
|
170
|
-
```
|
171
|
-
|
172
|
-
# License
|
173
|
-
|
1
|
+
# EM-Synchrony
|
2
|
+
|
3
|
+
Collection of convenience classes and primitives to help untangle evented code, plus a number of patched EM clients to make them Fiber aware. To learn more, please see: [Untangling Evented Code with Ruby Fibers](http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers).
|
4
|
+
|
5
|
+
* Fiber aware ConnectionPool with sync/async query support
|
6
|
+
* Fiber aware Iterator to allow concurrency control & mixing of sync / async
|
7
|
+
* Fiber aware async inline support: turns any async function into sync
|
8
|
+
* Fiber aware Multi-request interface for any callback enabled clients
|
9
|
+
* Fiber aware TCPSocket replacement, powered by EventMachine
|
10
|
+
* Fiber aware Thread, Mutex, ConditionVariable clases
|
11
|
+
* Fiber aware sleep
|
12
|
+
|
13
|
+
Supported clients:
|
14
|
+
|
15
|
+
* [mysql2](http://github.com/igrigorik/em-synchrony/blob/master/spec/mysql2_spec.rb): .query is synchronous, while .aquery is async (see specs)
|
16
|
+
* [activerecord](http://github.com/igrigorik/em-synchrony/blob/master/spec/activerecord_spec.rb): require synchrony/activerecord, set your AR adapter to em_mysql2 and you should be good to go
|
17
|
+
* [em-http-request](http://github.com/igrigorik/em-synchrony/blob/master/spec/http_spec.rb): .get, etc are synchronous, while .aget, etc are async
|
18
|
+
* [em-memcached](http://github.com/igrigorik/em-synchrony/blob/master/spec/memcache_spec.rb) & [remcached](http://github.com/igrigorik/em-synchrony/blob/master/spec/remcached_spec.rb): .get, etc, and .multi_* methods are synchronous
|
19
|
+
* [em-mongo](http://github.com/igrigorik/em-synchrony/blob/master/spec/em-mongo_spec.rb): .find, .first are synchronous
|
20
|
+
* [mongoid](http://github.com/igrigorik/em-synchrony/blob/master/spec/mongo_spec.rb): all functions synchronous, plus Rails compatibility
|
21
|
+
* em-jack: a[method]'s are async, and all regular jack method's are synchronous
|
22
|
+
* [AMQP](http://github.com/ruby-amqp/amqp): most of functions are synchronous (see specs)
|
23
|
+
|
24
|
+
Other clients with native Fiber support:
|
25
|
+
|
26
|
+
* redis: contains [synchrony code](https://github.com/ezmobius/redis-rb/blob/master/test/synchrony_driver.rb) right within the driver
|
27
|
+
* synchrony also supports [em-redis](http://github.com/igrigorik/em-synchrony/blob/master/spec/redis_spec.rb) and em-hiredis (see specs), but unless you specifically need either of those, use the official redis gem
|
28
|
+
|
29
|
+
## Fiber-aware Iterator: mixing sync / async code
|
30
|
+
|
31
|
+
Allows you to perform each, map, inject on a collection of any asynchronous tasks. To advance the iterator, simply call iter.next, or iter.return(result). The iterator will not exit until you advance through the entire collection. Additionally, you can specify the desired concurrency level! Ex: crawling a web-site, but you want to have at most 5 connections open at any one time.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require "em-synchrony"
|
35
|
+
require "em-synchrony/em-http"
|
36
|
+
|
37
|
+
EM.synchrony do
|
38
|
+
concurrency = 2
|
39
|
+
urls = ['http://url.1.com', 'http://url2.com']
|
40
|
+
|
41
|
+
# iterator will execute async blocks until completion, .each, .inject also work!
|
42
|
+
results = EM::Synchrony::Iterator.new(urls, concurrency).map do |url, iter|
|
43
|
+
|
44
|
+
# fire async requests, on completion advance the iterator
|
45
|
+
http = EventMachine::HttpRequest.new(url).aget
|
46
|
+
http.callback { iter.return(http) }
|
47
|
+
http.errback { iter.return(http) }
|
48
|
+
end
|
49
|
+
|
50
|
+
p results # all completed requests
|
51
|
+
EventMachine.stop
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Or, you can use FiberIterator to hide the async nature of em-http:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
require "em-synchrony"
|
59
|
+
require "em-synchrony/em-http"
|
60
|
+
require "em-synchrony/fiber_iterator"
|
61
|
+
|
62
|
+
EM.synchrony do
|
63
|
+
concurrency = 2
|
64
|
+
urls = ['http://url.1.com', 'http://url2.com']
|
65
|
+
results = []
|
66
|
+
|
67
|
+
EM::Synchrony::FiberIterator.new(urls, concurrency).each do |url|
|
68
|
+
resp = EventMachine::HttpRequest.new(url).get
|
69
|
+
results.push resp.response
|
70
|
+
end
|
71
|
+
|
72
|
+
p results # all completed requests
|
73
|
+
EventMachine.stop
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
## Fiber-aware ConnectionPool shared by a fiber:
|
78
|
+
Allows you to create a pool of resources which are then shared by one or more fibers. A good example is a collection of long-lived database connections. The pool will automatically block and wake up the fibers as the connections become available.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
require "em-synchrony"
|
82
|
+
require "em-synchrony/mysql2"
|
83
|
+
|
84
|
+
EventMachine.synchrony do
|
85
|
+
db = EventMachine::Synchrony::ConnectionPool.new(size: 2) do
|
86
|
+
Mysql2::EM::Client.new
|
87
|
+
end
|
88
|
+
|
89
|
+
multi = EventMachine::Synchrony::Multi.new
|
90
|
+
multi.add :a, db.aquery("select sleep(1)")
|
91
|
+
multi.add :b, db.aquery("select sleep(1)")
|
92
|
+
res = multi.perform
|
93
|
+
|
94
|
+
p "Look ma, no callbacks, and parallel MySQL requests!"
|
95
|
+
p res
|
96
|
+
|
97
|
+
EventMachine.stop
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
## Fiber-aware Multi interface: parallel HTTP requests
|
102
|
+
Allows you to fire simultaneous requests and wait for all of them to complete (success or error) before advancing. Concurrently fetching many HTTP pages at once is a good example; parallel SQL queries is another. Technically, this functionality can be also achieved by using the Synchrony Iterator shown above.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
require "em-synchrony"
|
106
|
+
require "em-synchrony/em-http"
|
107
|
+
EventMachine.synchrony do
|
108
|
+
multi = EventMachine::Synchrony::Multi.new
|
109
|
+
multi.add :a, EventMachine::HttpRequest.new("http://www.postrank.com").aget
|
110
|
+
multi.add :b, EventMachine::HttpRequest.new("http://www.postrank.com").apost
|
111
|
+
res = multi.perform
|
112
|
+
|
113
|
+
p "Look ma, no callbacks, and parallel HTTP requests!"
|
114
|
+
p res
|
115
|
+
|
116
|
+
EventMachine.stop
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
## Fiber-aware & EventMachine backed TCPSocket:
|
121
|
+
This is dangerous territory - you've been warned. You can patch your base TCPSocket class to make any/all libraries depending on TCPSocket be actually powered by EventMachine and Fibers under the hood.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
require "em-synchrony"
|
125
|
+
require "lib/em-synchrony"
|
126
|
+
require "net/http"
|
127
|
+
|
128
|
+
EM.synchrony do
|
129
|
+
# replace default Socket code to use EventMachine Sockets instead
|
130
|
+
TCPSocket = EventMachine::Synchrony::TCPSocket
|
131
|
+
|
132
|
+
Net::HTTP.get_print 'www.google.com', '/index.html'
|
133
|
+
EM.stop
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
## Inline synchronization & Fiber sleep:
|
138
|
+
Allows you to inline/synchronize any callback interface to behave as if it was a blocking call. Simply pass any callback object to Synchrony.sync and it will do the right thing: the fiber will be resumed once the callback/errback fires. Likewise, use Synchrony.sleep to avoid blocking the main thread if you need to put one of your workers to sleep.
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
require "em-synchrony"
|
142
|
+
require "em-synchrony/em-http"
|
143
|
+
EM.synchrony do
|
144
|
+
# pass a callback enabled client to sync to automatically resume it when callback fires
|
145
|
+
result = EM::Synchrony.sync EventMachine::HttpRequest.new('http://www.gooogle.com/').aget
|
146
|
+
p result
|
147
|
+
|
148
|
+
# pause execution for 2 seconds
|
149
|
+
EM::Synchrony.sleep(2)
|
150
|
+
|
151
|
+
EM.stop
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
155
|
+
## Async ActiveRecord:
|
156
|
+
|
157
|
+
Allows you to use async ActiveRecord within Rails and outside of Rails (see [async-rails](https://github.com/igrigorik/async-rails)). If you need to control the connection pool size, use [rack/fiber_pool](https://github.com/mperham/rack-fiber_pool/).
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
require "em-synchrony"
|
161
|
+
require "em-synchrony/mysql2"
|
162
|
+
require "em-synchrony/activerecord"
|
163
|
+
|
164
|
+
ActiveRecord::Base.establish_connection(
|
165
|
+
:adapter => 'em_mysql2',
|
166
|
+
:database => 'widgets'
|
167
|
+
)
|
168
|
+
|
169
|
+
result = Widget.all.to_a
|
170
|
+
```
|
171
|
+
|
172
|
+
# License
|
173
|
+
|
174
174
|
The MIT License - Copyright (c) 2011 Ilya Grigorik
|
data/em-synchrony.gemspec
CHANGED
@@ -1,47 +1,47 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# AR adapter for using a fibered mysql2 connection with EM
|
4
|
-
# This adapter should be used within Thin or Unicorn with the rack-fiber_pool middleware.
|
5
|
-
# Just update your database.yml's adapter to be 'em_mysql2'
|
6
|
-
# to real connection pool size.
|
7
|
-
|
8
|
-
require 'em-synchrony/mysql2'
|
9
|
-
require 'em-synchrony/activerecord'
|
10
|
-
require 'active_record/connection_adapters/mysql2_adapter'
|
11
|
-
|
12
|
-
module ActiveRecord
|
13
|
-
class Base
|
14
|
-
def self.em_mysql2_connection(config)
|
15
|
-
client = EM::Synchrony::ActiveRecord::ConnectionPool.new(size: config[:pool]) do
|
16
|
-
conn = ActiveRecord::ConnectionAdapters::EMMysql2Adapter::Client.new(config.symbolize_keys)
|
17
|
-
# From Mysql2Adapter#configure_connection
|
18
|
-
conn.query_options.merge!(:as => :array)
|
19
|
-
|
20
|
-
# By default, MySQL 'where id is null' selects the last inserted id.
|
21
|
-
# Turn this off. http://dev.rubyonrails.org/ticket/6778
|
22
|
-
variable_assignments = ['SQL_AUTO_IS_NULL=0']
|
23
|
-
encoding = config[:encoding]
|
24
|
-
variable_assignments << "NAMES '#{encoding}'" if encoding
|
25
|
-
|
26
|
-
wait_timeout = config[:wait_timeout]
|
27
|
-
wait_timeout = 2592000 unless wait_timeout.is_a?(Fixnum)
|
28
|
-
variable_assignments << "@@wait_timeout = #{wait_timeout}"
|
29
|
-
|
30
|
-
conn.query("SET #{variable_assignments.join(', ')}")
|
31
|
-
conn
|
32
|
-
end
|
33
|
-
options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
|
34
|
-
ActiveRecord::ConnectionAdapters::EMMysql2Adapter.new(client, logger, options, config)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
module ConnectionAdapters
|
39
|
-
class EMMysql2Adapter < ::ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
40
|
-
class Client < Mysql2::EM::Client
|
41
|
-
include EM::Synchrony::ActiveRecord::Client
|
42
|
-
end
|
43
|
-
|
44
|
-
include EM::Synchrony::ActiveRecord::Adapter
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# AR adapter for using a fibered mysql2 connection with EM
|
4
|
+
# This adapter should be used within Thin or Unicorn with the rack-fiber_pool middleware.
|
5
|
+
# Just update your database.yml's adapter to be 'em_mysql2'
|
6
|
+
# to real connection pool size.
|
7
|
+
|
8
|
+
require 'em-synchrony/mysql2'
|
9
|
+
require 'em-synchrony/activerecord'
|
10
|
+
require 'active_record/connection_adapters/mysql2_adapter'
|
11
|
+
|
12
|
+
module ActiveRecord
|
13
|
+
class Base
|
14
|
+
def self.em_mysql2_connection(config)
|
15
|
+
client = EM::Synchrony::ActiveRecord::ConnectionPool.new(size: config[:pool]) do
|
16
|
+
conn = ActiveRecord::ConnectionAdapters::EMMysql2Adapter::Client.new(config.symbolize_keys)
|
17
|
+
# From Mysql2Adapter#configure_connection
|
18
|
+
conn.query_options.merge!(:as => :array)
|
19
|
+
|
20
|
+
# By default, MySQL 'where id is null' selects the last inserted id.
|
21
|
+
# Turn this off. http://dev.rubyonrails.org/ticket/6778
|
22
|
+
variable_assignments = ['SQL_AUTO_IS_NULL=0']
|
23
|
+
encoding = config[:encoding]
|
24
|
+
variable_assignments << "NAMES '#{encoding}'" if encoding
|
25
|
+
|
26
|
+
wait_timeout = config[:wait_timeout]
|
27
|
+
wait_timeout = 2592000 unless wait_timeout.is_a?(Fixnum)
|
28
|
+
variable_assignments << "@@wait_timeout = #{wait_timeout}"
|
29
|
+
|
30
|
+
conn.query("SET #{variable_assignments.join(', ')}")
|
31
|
+
conn
|
32
|
+
end
|
33
|
+
options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
|
34
|
+
ActiveRecord::ConnectionAdapters::EMMysql2Adapter.new(client, logger, options, config)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ConnectionAdapters
|
39
|
+
class EMMysql2Adapter < ::ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
40
|
+
class Client < Mysql2::EM::Client
|
41
|
+
include EM::Synchrony::ActiveRecord::Client
|
42
|
+
end
|
43
|
+
|
44
|
+
include EM::Synchrony::ActiveRecord::Adapter
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|