rack-async 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +166 -0
- data/lib/rack/async.rb +128 -0
- metadata +71 -0
data/README.rdoc
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
= Rack::Async
|
2
|
+
|
3
|
+
Rack::Async is a rack middleware that makes available a thin[https://github.com/macournoyer/thin] like async api on most rack compatible web servers.
|
4
|
+
|
5
|
+
It works with mongrel, thin, and all the related mongrel-derived servers I've tried, additionally ebb works fine.
|
6
|
+
|
7
|
+
Although WEBrick won't support sending asynchronous replies, using Rack::Async will successfully send everything you ask it to, just bundled up all together like a normal synchronous reply.
|
8
|
+
|
9
|
+
It won't work with MacRuby's ControlTower at all.
|
10
|
+
|
11
|
+
== Installation
|
12
|
+
|
13
|
+
gem install rack-async
|
14
|
+
|
15
|
+
== Common problems
|
16
|
+
|
17
|
+
=== Rack::Lint::LintError
|
18
|
+
Rack::Lint doesn't know anything about async responses, so can't validate them properly, you'll have to turn it off.
|
19
|
+
|
20
|
+
=== The response doesn't seem asynchronous in my browser
|
21
|
+
Most browsers won't start rendering anything until they have around 1024 bytes of data. One way to get around this is to simply send 1024 bytes of whitespace at the start of your response. If you're just looking to see your response come in chunk by chunk, `curl` doesn't suffer this problem.
|
22
|
+
|
23
|
+
== Async Response with EventMachine
|
24
|
+
|
25
|
+
Here's how to send an async response with EventMachine that will work across thin, mongrel, ebb, etc
|
26
|
+
|
27
|
+
config.ru
|
28
|
+
|
29
|
+
require 'rubygems'
|
30
|
+
require 'rack/async'
|
31
|
+
require 'eventmachine'
|
32
|
+
|
33
|
+
class EMAsyncApp
|
34
|
+
def call(env)
|
35
|
+
event_machine do
|
36
|
+
EM.add_timer(5) do
|
37
|
+
env['async.callback'].call([200, {}, ["Hello world!"]])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# returning this signals to the server we are sending an async response
|
42
|
+
Rack::Async::RESPONSE
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
# make sure EventMachine is running (if we're on thin it'll be up and
|
47
|
+
# running, but this isn't the case on other servers).
|
48
|
+
def event_machine(&block)
|
49
|
+
if EM.reactor_running?
|
50
|
+
block.call
|
51
|
+
else
|
52
|
+
Thread.new {EM.run}
|
53
|
+
EM.next_tick(block)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
use Rack::Async
|
59
|
+
run EMAsyncApp.new
|
60
|
+
|
61
|
+
== Async Response with Threads
|
62
|
+
|
63
|
+
Here's how to send an async response using threads that will work across thin, mongrel, ebb, etc
|
64
|
+
|
65
|
+
(we'll just give you the #call method this time)
|
66
|
+
|
67
|
+
def call(env)
|
68
|
+
Thread.new do
|
69
|
+
sleep 5
|
70
|
+
env['async.callback'].call([200, {}, ["Hello world!"]])
|
71
|
+
end
|
72
|
+
|
73
|
+
Rack::Async::RESPONSE
|
74
|
+
end
|
75
|
+
|
76
|
+
== Async Body with EventMachine
|
77
|
+
|
78
|
+
Sure sending an asynchronous response is neat, but what really cool is streaming out the body to the client, here's how to do that
|
79
|
+
|
80
|
+
config.ru
|
81
|
+
|
82
|
+
require 'rubygems'
|
83
|
+
require 'rack/async'
|
84
|
+
require 'eventmachine'
|
85
|
+
|
86
|
+
class EMAsyncBodyApp
|
87
|
+
def call(env)
|
88
|
+
body = env['async.body']
|
89
|
+
|
90
|
+
event_machine do
|
91
|
+
i = 0
|
92
|
+
timer = EM.add_periodic_timer(1) do
|
93
|
+
body << "Hello world!\r\n"
|
94
|
+
i += 1
|
95
|
+
if i >= 5
|
96
|
+
body.finish
|
97
|
+
timer.cancel
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
[200, {}, body]
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
def event_machine(&block)
|
107
|
+
if EM.reactor_running?
|
108
|
+
block.call
|
109
|
+
else
|
110
|
+
Thread.new {EM.run}
|
111
|
+
EM.next_tick(block)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
use Rack::Async
|
117
|
+
run EMAsyncBodyApp.new
|
118
|
+
|
119
|
+
== Async Everything with Threads
|
120
|
+
|
121
|
+
You can combine the two like so (threaded example this time, but it works just fine with event machine too).
|
122
|
+
|
123
|
+
require 'rubygems'
|
124
|
+
require 'rack/async'
|
125
|
+
require 'thread'
|
126
|
+
|
127
|
+
class ThreadedAsyncApp
|
128
|
+
def call(env)
|
129
|
+
Thread.new do
|
130
|
+
sleep 1
|
131
|
+
body = env['async.body']
|
132
|
+
env['async.callback'].call([200, {}, body])
|
133
|
+
5.times {sleep 1; body << "Hello world!\r\n";}
|
134
|
+
body.finish
|
135
|
+
end
|
136
|
+
|
137
|
+
Rack::Async::RESPONSE
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
use Rack::Async
|
142
|
+
run ThreadedAsyncApp.new
|
143
|
+
|
144
|
+
== Licence
|
145
|
+
|
146
|
+
(The MIT License)
|
147
|
+
|
148
|
+
Copyright (c) 2011 Matthew Sadler
|
149
|
+
|
150
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
151
|
+
of this software and associated documentation files (the "Software"), to deal
|
152
|
+
in the Software without restriction, including without limitation the rights
|
153
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
154
|
+
copies of the Software, and to permit persons to whom the Software is
|
155
|
+
furnished to do so, subject to the following conditions:
|
156
|
+
|
157
|
+
The above copyright notice and this permission notice shall be included in
|
158
|
+
all copies or substantial portions of the Software.
|
159
|
+
|
160
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
161
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
162
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
163
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
164
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
165
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
166
|
+
THE SOFTWARE.
|
data/lib/rack/async.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class Async
|
5
|
+
RESPONSE = [-1, {}, []].freeze
|
6
|
+
CALLBACK = "async.callback".freeze
|
7
|
+
BODY = "async.body".freeze
|
8
|
+
|
9
|
+
def initialize(app)
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
original_callback = env[Async::CALLBACK]
|
15
|
+
if original_callback.respond_to?(:arity)
|
16
|
+
original_callback_arity = original_callback.arity
|
17
|
+
elsif original_callback
|
18
|
+
original_callback_arity = original_callback.method(:call).arity
|
19
|
+
end
|
20
|
+
|
21
|
+
if original_callback # we've got a thin-like async api!
|
22
|
+
if original_callback_arity > 1 # wrap ebb's callback to expand the args
|
23
|
+
env[Async::CALLBACK] = Proc.new do |response|
|
24
|
+
original_callback.call(*response)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
env[Async::BODY] = Async::CallbackBody.new
|
28
|
+
else # no async api, but blocking in the right places we can fake it
|
29
|
+
blocking_callback = Async::BlockingCallback.new
|
30
|
+
env[Async::CALLBACK] = blocking_callback
|
31
|
+
env[Async::BODY] = Async::BlockingBody.new
|
32
|
+
end
|
33
|
+
|
34
|
+
response = @app.call(env)
|
35
|
+
|
36
|
+
if response.first != Async::RESPONSE.first # non-async response
|
37
|
+
response
|
38
|
+
elsif original_callback && original_callback_arity > 1 # ebb-like
|
39
|
+
[0, {}, []]
|
40
|
+
elsif original_callback # thin-like
|
41
|
+
Async::RESPONSE
|
42
|
+
else # mongrel, etc
|
43
|
+
blocking_callback.wait
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class BlockingCallback < Queue
|
48
|
+
alias call push
|
49
|
+
alias wait pop
|
50
|
+
end
|
51
|
+
|
52
|
+
class BlockingBody < Queue
|
53
|
+
def each
|
54
|
+
until @finished && empty?
|
55
|
+
data = pop
|
56
|
+
yield data if data
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def succeed
|
61
|
+
@finished = true
|
62
|
+
self << nil
|
63
|
+
end
|
64
|
+
alias finish succeed
|
65
|
+
alias fail succeed
|
66
|
+
end
|
67
|
+
|
68
|
+
class CallbackBody
|
69
|
+
def initialize(*args)
|
70
|
+
super
|
71
|
+
@buffer = []
|
72
|
+
@lock = Mutex.new
|
73
|
+
end
|
74
|
+
|
75
|
+
def <<(data)
|
76
|
+
@lock.synchronize do
|
77
|
+
if @each_callback
|
78
|
+
@each_callback.call(data)
|
79
|
+
else
|
80
|
+
@buffer << data
|
81
|
+
end
|
82
|
+
end
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
def each(&block)
|
87
|
+
@lock.synchronize do
|
88
|
+
@buffer.each(&block)
|
89
|
+
@buffer.clear
|
90
|
+
@each_callback = block
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def succeed
|
95
|
+
@callback.call if @callback
|
96
|
+
@finished = true
|
97
|
+
end
|
98
|
+
alias finish succeed
|
99
|
+
|
100
|
+
def fail
|
101
|
+
@errback.call if @errback
|
102
|
+
@finished = true
|
103
|
+
end
|
104
|
+
|
105
|
+
def callback(&block)
|
106
|
+
if !@callback && @finished
|
107
|
+
block.call
|
108
|
+
else
|
109
|
+
@callback = block
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def errback(&block)
|
114
|
+
if !@errback && @finished
|
115
|
+
block.call
|
116
|
+
else
|
117
|
+
@errback = block
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def on_eof(&block)
|
122
|
+
callback(&block)
|
123
|
+
errback(&block)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-async
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matthew Sadler
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-19 00:00:00 +00:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Thin's asynchronous API available to any Rack server.
|
23
|
+
email: mat@sourcetagsandcodes.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- lib/rack/async.rb
|
32
|
+
- README.rdoc
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/matsadler/rack-async
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --main
|
40
|
+
- README.rdoc
|
41
|
+
- --charset
|
42
|
+
- utf-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.7
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: An asynchronous Rack API for everyone.
|
70
|
+
test_files: []
|
71
|
+
|