Skip to content

Commit

Permalink
em-synchrony'fy swift
Browse files Browse the repository at this point in the history
  • Loading branch information
deepfryed committed Apr 17, 2012
1 parent 518c8de commit f55e60f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 23 deletions.
56 changes: 34 additions & 22 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A rational rudimentary object relational mapper.
## Dependencies ## Dependencies


* ruby >= 1.9.1 * ruby >= 1.9.1
* [dbic++](http://github.com/deepfryed/dbicpp) >= 0.6.0 * [dbic++](http://github.com/deepfryed/dbicpp) >= 0.6.1
* mysql >= 5.0.17, postgresql >= 8.4 or sqlite3 >= 3.7 * mysql >= 5.0.17, postgresql >= 8.4 or sqlite3 >= 3.7


## Features ## Features
Expand Down Expand Up @@ -245,31 +245,43 @@ which implicitly uses `rb_thread_wait_fd`
Thread.list.reject {|thread| Thread.current == thread}.each(&:join) Thread.list.reject {|thread| Thread.current == thread}.each(&:join)
``` ```
or use the `swift/eventmachine` api.
```ruby ```ruby
require 'swift' require 'swift/eventmachine'
require 'eventmachine'
EM.run do
pool = 3.times.map.with_index {|n| Swift.setup n, Swift::DB::Postgres, db: 'swift'} pool = 3.times.map { Swift.setup(:default, Swift::DB::Postgres, db: "swift") }
module Handler 3.times.each do |n|
attr_reader :result defer = pool[n].execute("select pg_sleep(3 - #{n}), #{n + 1} as qid")
def initialize result defer.callback do |res|
@result = result p res.first
end end
def notify_readable defer.errback do |e|
result.retrieve p 'error', e
result.each {|row| p row } end
unbind
end end
end end
```
or use the `em-synchrony` api for `swift`
```ruby
require 'swift/synchrony'
EM.run do EM.run do
EM.watch(pool[0].fileno, Handler, pool[0].async_execute('select pg_sleep(3), 1 as qid')){|c| c.notify_readable = true} 3.times.each do |n|
EM.watch(pool[1].fileno, Handler, pool[1].async_execute('select pg_sleep(2), 2 as qid')){|c| c.notify_readable = true} EM.synchrony do
EM.watch(pool[2].fileno, Handler, pool[2].async_execute('select pg_sleep(1), 3 as qid')){|c| c.notify_readable = true} db = Swift.setup(:default, Swift::DB::Postgres, db: "swift")
EM.add_timer(4) { EM.stop } result = db.execute("select pg_sleep(3 - #{n}), #{n + 1} as qid")
p result.first
EM.stop if n == 0
end
end
end end
``` ```
Expand Down
2 changes: 1 addition & 1 deletion ext/extconf.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ def assert_dbicpp_version ver
exit 1 unless library_installed? 'uuid', apt_install_hint('uuid-dev') exit 1 unless library_installed? 'uuid', apt_install_hint('uuid-dev')
exit 1 unless library_installed? 'dbic++', apt_install_hint('dbic++-dev') exit 1 unless library_installed? 'dbic++', apt_install_hint('dbic++-dev')


assert_dbicpp_version '0.6.0' assert_dbicpp_version '0.6.1'
create_makefile 'swift' create_makefile 'swift'
33 changes: 33 additions & 0 deletions lib/swift/eventmachine.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'eventmachine'
require 'swift'

module Swift
class Adapter
alias :blocking_execute :execute

class EMHandler < EM::Connection
attr_reader :result, :defer
def initialize result, defer
@result = result
@defer = defer
end

def notify_readable
detach
begin
result.retrieve
rescue Exception => e
defer.fail(e)
else
defer.succeed(result)
end
end
end

def execute *args
EM::DefaultDeferrable.new.tap do |defer|
EM.watch(fileno, EMHandler, async_execute(*args), defer) {|c| c.notify_readable = true }
end
end
end
end
11 changes: 11 additions & 0 deletions lib/swift/synchrony.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'em-synchrony'
require 'swift/eventmachine'

module Swift
class Adapter
alias :aexecute :execute
def execute *args
EM::Synchrony.sync aexecute(*args)
end
end
end

0 comments on commit f55e60f

Please sign in to comment.