thin_async 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +12 -7
- data/example/headers.ru +23 -0
- data/lib/thin/async.rb +17 -8
- data/thin_async.gemspec +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -11,14 +11,19 @@ will prevent the EventMachine event loop from running and block all other reques
|
|
11
11
|
## Usage
|
12
12
|
Inside your Rack app #call(env):
|
13
13
|
|
14
|
-
Thin::AsyncResponse.new(env)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
response = Thin::AsyncResponse.new(env)
|
15
|
+
response.status = 201
|
16
|
+
response.headers["X-Muffin-Mode"] = "ACTIVATED!"
|
17
|
+
|
18
|
+
response << "this is ... "
|
19
|
+
|
20
|
+
EM.add_timer(1) do
|
21
|
+
# This will be sent to the client 1 sec later without blocking other requests.
|
22
|
+
response << "async!"
|
23
|
+
response.done
|
21
24
|
end
|
25
|
+
|
26
|
+
response.finish
|
22
27
|
|
23
28
|
See example/ dir for more.
|
24
29
|
|
data/example/headers.ru
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../lib/thin/async"
|
2
|
+
|
3
|
+
class Headers
|
4
|
+
def call(env)
|
5
|
+
response = Thin::AsyncResponse.new(env)
|
6
|
+
|
7
|
+
EM.add_timer(1) do
|
8
|
+
response.status = 201
|
9
|
+
response.headers["X-Muffin-Mode"] = "ACTIVATED!"
|
10
|
+
|
11
|
+
# Headers are sent automatically the first time you call response#<<
|
12
|
+
# and can't be modified afterwards.
|
13
|
+
response.send_headers
|
14
|
+
|
15
|
+
response << "done"
|
16
|
+
response.done
|
17
|
+
end
|
18
|
+
|
19
|
+
response.finish
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
run Headers.new
|
data/lib/thin/async.rb
CHANGED
@@ -38,14 +38,15 @@ module Thin
|
|
38
38
|
|
39
39
|
Marker = [-1, {}, []].freeze
|
40
40
|
|
41
|
-
attr_reader :headers
|
41
|
+
attr_reader :headers, :callback
|
42
42
|
attr_accessor :status
|
43
43
|
|
44
|
-
def initialize(env)
|
44
|
+
def initialize(env, status=200, headers={})
|
45
45
|
@callback = env['async.callback']
|
46
46
|
@body = DeferrableBody.new
|
47
|
-
@status =
|
48
|
-
@headers =
|
47
|
+
@status = status
|
48
|
+
@headers = headers
|
49
|
+
@headers_sent = false
|
49
50
|
|
50
51
|
if block_given?
|
51
52
|
yield self
|
@@ -53,19 +54,27 @@ module Thin
|
|
53
54
|
end
|
54
55
|
end
|
55
56
|
|
57
|
+
def send_headers
|
58
|
+
return if @headers_sent
|
59
|
+
@callback.call [@status, @headers, @body]
|
60
|
+
@headers_sent = true
|
61
|
+
end
|
62
|
+
|
56
63
|
def write(body)
|
64
|
+
send_headers
|
57
65
|
@body.call(body.respond_to?(:each) ? body : [body])
|
58
66
|
end
|
59
67
|
alias :<< :write
|
60
68
|
|
69
|
+
# Tell Thin the response is complete and the connection can be closed.
|
61
70
|
def done
|
62
|
-
|
63
|
-
|
64
|
-
end
|
71
|
+
send_headers
|
72
|
+
EM.next_tick { @body.succeed }
|
65
73
|
end
|
66
74
|
|
75
|
+
# Tell Thin the response is gonna be sent asynchronously.
|
76
|
+
# The status code of -1 is the magic trick here.
|
67
77
|
def finish
|
68
|
-
@callback.call [@status, @headers, @body]
|
69
78
|
Marker
|
70
79
|
end
|
71
80
|
end
|
data/thin_async.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thin_async
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marc-Andre Cournoyer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-03-08 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -31,10 +31,10 @@ extensions: []
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
|
33
33
|
files:
|
34
|
+
- example/headers.ru
|
34
35
|
- example/simple.ru
|
35
36
|
- lib/thin/async.rb
|
36
37
|
- README.md
|
37
|
-
- thin_async-0.0.1.gem
|
38
38
|
- thin_async.gemspec
|
39
39
|
has_rdoc: true
|
40
40
|
homepage: http://github.com/macournoyer/thin_async
|