mongrel_esi 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. data/README +10 -9
  2. data/doc/rdoc/classes/ESI/Cache.html +10 -10
  3. data/doc/rdoc/classes/ESI/Cache.src/{M000085.html → M000087.html} +0 -0
  4. data/doc/rdoc/classes/ESI/Cache.src/{M000086.html → M000088.html} +0 -0
  5. data/doc/rdoc/classes/ESI/Config.html +43 -62
  6. data/doc/rdoc/classes/ESI/Config.src/M000049.html +18 -0
  7. data/doc/rdoc/classes/ESI/Config.src/{M000059.html → M000050.html} +5 -5
  8. data/doc/rdoc/classes/ESI/Config.src/M000051.html +35 -0
  9. data/doc/rdoc/classes/ESI/Config.src/M000052.html +27 -5
  10. data/doc/rdoc/classes/ESI/Config.src/M000053.html +10 -5
  11. data/doc/rdoc/classes/ESI/Config.src/M000054.html +5 -22
  12. data/doc/rdoc/classes/ESI/Config.src/M000055.html +7 -25
  13. data/doc/rdoc/classes/ESI/Config.src/M000056.html +11 -10
  14. data/doc/rdoc/classes/ESI/Config/CacheConfig.html +25 -25
  15. data/doc/rdoc/classes/ESI/Config/CacheConfig.src/{M000068.html → M000060.html} +6 -5
  16. data/doc/rdoc/classes/ESI/Config/CacheConfig.src/{M000065.html → M000061.html} +5 -5
  17. data/doc/rdoc/classes/ESI/Config/CacheConfig.src/{M000066.html → M000062.html} +4 -4
  18. data/doc/rdoc/classes/ESI/Config/CacheConfig.src/{M000067.html → M000063.html} +4 -4
  19. data/doc/rdoc/classes/ESI/Config/CacheConfig.src/M000064.html +5 -6
  20. data/doc/rdoc/classes/ESI/Config/ConfigRouter.html +15 -15
  21. data/doc/rdoc/classes/ESI/Config/ConfigRouter.src/{M000061.html → M000057.html} +5 -5
  22. data/doc/rdoc/classes/ESI/Config/ConfigRouter.src/{M000062.html → M000058.html} +7 -7
  23. data/doc/rdoc/classes/ESI/Config/ConfigRouter.src/{M000063.html → M000059.html} +7 -7
  24. data/doc/rdoc/classes/ESI/Dispatcher.html +17 -15
  25. data/doc/rdoc/classes/ESI/Dispatcher.src/{M000087.html → M000092.html} +4 -7
  26. data/doc/rdoc/classes/ESI/Dispatcher.src/M000093.html +27 -0
  27. data/doc/rdoc/classes/ESI/Fragment.html +15 -15
  28. data/doc/rdoc/classes/ESI/Fragment.src/{M000100.html → M000111.html} +0 -0
  29. data/doc/rdoc/classes/ESI/Fragment.src/{M000101.html → M000112.html} +0 -0
  30. data/doc/rdoc/classes/ESI/Fragment.src/{M000102.html → M000113.html} +0 -0
  31. data/doc/rdoc/classes/ESI/MemcachedCache.html +46 -46
  32. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000037.html +24 -0
  33. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000038.html +22 -0
  34. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000039.html +19 -0
  35. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000040.html +10 -11
  36. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000041.html +5 -9
  37. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000042.html +6 -6
  38. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000043.html +5 -10
  39. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000044.html +5 -5
  40. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000045.html +4 -6
  41. data/doc/rdoc/classes/ESI/OutputAdapter.html +204 -0
  42. data/doc/rdoc/classes/ESI/OutputAdapter.src/M000094.html +37 -0
  43. data/doc/rdoc/classes/ESI/OutputAdapter.src/M000095.html +18 -0
  44. data/doc/rdoc/classes/ESI/OutputAdapter.src/M000096.html +18 -0
  45. data/doc/rdoc/classes/ESI/OutputAdapter.src/M000097.html +18 -0
  46. data/doc/rdoc/classes/ESI/Parser.html +15 -15
  47. data/doc/rdoc/classes/ESI/Parser.src/{M000049.html → M000065.html} +0 -0
  48. data/doc/rdoc/classes/ESI/Parser.src/{M000050.html → M000066.html} +0 -0
  49. data/doc/rdoc/classes/ESI/Parser.src/{M000051.html → M000067.html} +0 -0
  50. data/doc/rdoc/classes/ESI/Processor.html +15 -15
  51. data/doc/rdoc/classes/ESI/Processor.src/{M000037.html → M000046.html} +0 -0
  52. data/doc/rdoc/classes/ESI/Processor.src/{M000038.html → M000047.html} +1 -1
  53. data/doc/rdoc/classes/ESI/Processor.src/{M000039.html → M000048.html} +0 -0
  54. data/doc/rdoc/classes/ESI/Proxy.html +48 -48
  55. data/doc/rdoc/classes/ESI/Proxy.src/M000071.html +7 -40
  56. data/doc/rdoc/classes/ESI/Proxy.src/M000072.html +50 -12
  57. data/doc/rdoc/classes/ESI/Proxy.src/M000073.html +49 -6
  58. data/doc/rdoc/classes/ESI/Proxy.src/M000074.html +11 -14
  59. data/doc/rdoc/classes/ESI/Proxy.src/M000075.html +6 -5
  60. data/doc/rdoc/classes/ESI/Proxy.src/M000076.html +9 -15
  61. data/doc/rdoc/classes/ESI/Proxy.src/M000077.html +5 -16
  62. data/doc/rdoc/classes/ESI/Proxy.src/M000078.html +28 -0
  63. data/doc/rdoc/classes/ESI/Proxy.src/M000079.html +29 -0
  64. data/doc/rdoc/classes/ESI/RackAdapter.html +214 -0
  65. data/doc/rdoc/classes/ESI/RackAdapter.src/M000068.html +18 -0
  66. data/doc/rdoc/classes/ESI/RackAdapter.src/M000069.html +34 -0
  67. data/doc/rdoc/classes/ESI/{MemcachedCache.src/M000048.html → RackAdapter.src/M000070.html} +4 -4
  68. data/doc/rdoc/classes/ESI/RackConfig.html +173 -0
  69. data/doc/rdoc/classes/ESI/RackConfig.src/M000089.html +28 -0
  70. data/doc/rdoc/classes/ESI/RackConfig.src/M000090.html +18 -0
  71. data/doc/rdoc/classes/ESI/RackConfig.src/M000091.html +20 -0
  72. data/doc/rdoc/classes/ESI/Request.html +158 -0
  73. data/doc/rdoc/classes/ESI/{MemcachedCache.src/M000046.html → Request.src/M000098.html} +5 -5
  74. data/doc/rdoc/classes/ESI/Request.src/M000099.html +18 -0
  75. data/doc/rdoc/classes/ESI/Response.html +36 -36
  76. data/doc/rdoc/classes/ESI/Response.src/M000080.html +10 -8
  77. data/doc/rdoc/classes/ESI/Response.src/M000081.html +5 -7
  78. data/doc/rdoc/classes/ESI/Response.src/M000082.html +8 -12
  79. data/doc/rdoc/classes/ESI/Response.src/M000083.html +7 -5
  80. data/doc/rdoc/classes/ESI/Response.src/M000084.html +12 -20
  81. data/doc/rdoc/classes/ESI/Response.src/{M000079.html → M000085.html} +5 -5
  82. data/doc/rdoc/classes/ESI/Response.src/M000086.html +33 -0
  83. data/doc/rdoc/classes/ESI/Router.html +10 -10
  84. data/doc/rdoc/classes/ESI/Router.src/{M000098.html → M000109.html} +0 -0
  85. data/doc/rdoc/classes/ESI/Router.src/{M000099.html → M000110.html} +0 -0
  86. data/doc/rdoc/classes/ESI/RubyCache.html +45 -45
  87. data/doc/rdoc/classes/ESI/RubyCache.src/{M000089.html → M000100.html} +6 -6
  88. data/doc/rdoc/classes/ESI/RubyCache.src/{M000090.html → M000101.html} +6 -6
  89. data/doc/rdoc/classes/ESI/RubyCache.src/{M000091.html → M000102.html} +6 -6
  90. data/doc/rdoc/classes/ESI/RubyCache.src/{M000092.html → M000103.html} +8 -8
  91. data/doc/rdoc/classes/ESI/RubyCache.src/{M000093.html → M000104.html} +4 -4
  92. data/doc/rdoc/classes/ESI/RubyCache.src/{M000094.html → M000105.html} +8 -8
  93. data/doc/rdoc/classes/ESI/RubyCache.src/{M000095.html → M000106.html} +4 -4
  94. data/doc/rdoc/classes/ESI/RubyCache.src/{M000096.html → M000107.html} +4 -4
  95. data/doc/rdoc/classes/ESI/RubyCache.src/{M000097.html → M000108.html} +4 -4
  96. data/doc/rdoc/classes/ESI/Tag.html +8 -8
  97. data/doc/rdoc/created.rid +1 -1
  98. data/doc/rdoc/files/COPYING.html +1 -1
  99. data/doc/rdoc/files/LICENSE.html +1 -1
  100. data/doc/rdoc/files/README.html +12 -10
  101. data/doc/rdoc/files/ext/esi/common_rl.html +1 -1
  102. data/doc/rdoc/files/ext/esi/esi_parser_c.html +1 -1
  103. data/doc/rdoc/files/ext/esi/parser_c.html +1 -1
  104. data/doc/rdoc/files/ext/esi/parser_h.html +1 -1
  105. data/doc/rdoc/files/ext/esi/parser_rl.html +122 -82
  106. data/doc/rdoc/files/ext/esi/test/test_c.html +1 -1
  107. data/doc/rdoc/files/lib/esi/cache_rb.html +2 -1
  108. data/doc/rdoc/files/lib/esi/config_rb.html +2 -1
  109. data/doc/rdoc/files/lib/esi/dispatcher_rb.html +3 -1
  110. data/doc/rdoc/files/lib/esi/invalidator_rb.html +1 -1
  111. data/doc/rdoc/files/lib/esi/logger_rb.html +1 -1
  112. data/doc/rdoc/files/lib/esi/parser_rb.html +1 -1
  113. data/doc/rdoc/files/lib/esi/processor_rb.html +1 -1
  114. data/doc/rdoc/files/lib/esi/proxy_rb.html +2 -2
  115. data/doc/rdoc/files/{ext/esi/test/parser_c.html → lib/esi/rack_adapter_rb.html} +21 -5
  116. data/doc/rdoc/files/lib/esi/response_rb.html +1 -1
  117. data/doc/rdoc/files/lib/esi/router_rb.html +1 -1
  118. data/doc/rdoc/files/lib/esi/tag/attempt_rb.html +1 -1
  119. data/doc/rdoc/files/lib/esi/tag/base_rb.html +1 -1
  120. data/doc/rdoc/files/lib/esi/tag/container_rb.html +1 -1
  121. data/doc/rdoc/files/lib/esi/tag/except_rb.html +1 -1
  122. data/doc/rdoc/files/lib/esi/tag/include_rb.html +1 -1
  123. data/doc/rdoc/files/lib/esi/tag/invalidate_rb.html +1 -1
  124. data/doc/rdoc/files/lib/esi/tag/try_rb.html +1 -1
  125. data/doc/rdoc/files/lib/esi/version_rb.html +1 -1
  126. data/doc/rdoc/files/lib/multi_dirhandler_rb.html +1 -1
  127. data/doc/rdoc/fr_class_index.html +4 -0
  128. data/doc/rdoc/fr_file_index.html +1 -5
  129. data/doc/rdoc/fr_method_index.html +87 -76
  130. data/ext/esi/esi_parser.c +2 -2
  131. data/ext/esi/parser.c +133 -107
  132. data/ext/esi/parser.h +9 -4
  133. data/ext/esi/parser.rl +99 -73
  134. data/ext/esi/test/test.c +11 -6
  135. data/lib/esi/cache.rb +7 -3
  136. data/lib/esi/config.rb +4 -8
  137. data/lib/esi/dispatcher.rb +14 -5
  138. data/lib/esi/processor.rb +1 -1
  139. data/lib/esi/proxy.rb +58 -48
  140. data/lib/esi/rack_adapter.rb +97 -0
  141. data/lib/esi/response.rb +30 -1
  142. data/lib/esi/version.rb +1 -1
  143. data/test/bench-plot.rb +31 -0
  144. data/test/benchmarks/csv-perf-serial0.4 +101 -0
  145. data/test/benchmarks/csv-perf-trunk +101 -0
  146. data/test/benchmarks/perf-serial0.4-n1000-c1.csv +101 -0
  147. data/test/benchmarks/perf-serial0.4-n1000-c2.csv +101 -0
  148. data/test/benchmarks/perf-serial0.4-n1000-c3.csv +101 -0
  149. data/test/benchmarks/perf-serial0.4-n1000-c4.csv +101 -0
  150. data/test/benchmarks/perf-serial0.4-n1000-c5.csv +101 -0
  151. data/test/benchmarks/perf-serial0.4-n1000-c6.csv +101 -0
  152. data/test/benchmarks/perf-serial0.4-n1000-c7.csv +101 -0
  153. data/test/benchmarks/perf-trunk-n1000-c1.csv +101 -0
  154. data/test/benchmarks/perf-trunk-n1000-c2.csv +101 -0
  155. data/test/benchmarks/perf-trunk-n1000-c3.csv +101 -0
  156. data/test/benchmarks/perf-trunk-n1000-c4.csv +101 -0
  157. data/test/benchmarks/perf-trunk-n1000-c5.csv +101 -0
  158. data/test/benchmarks/perf-trunk-n1000-c6.csv +101 -0
  159. data/test/benchmarks/perf-trunk-n1000-c7.csv +101 -0
  160. data/test/load_test.rb +5 -2
  161. data/test/load_test_ab.rb +9 -4
  162. metadata +261 -227
  163. data/doc/rdoc/classes/ESI/Config.src/M000057.html +0 -18
  164. data/doc/rdoc/classes/ESI/Config.src/M000058.html +0 -20
  165. data/doc/rdoc/classes/ESI/Config.src/M000060.html +0 -24
  166. data/doc/rdoc/classes/ESI/Dispatcher.src/M000088.html +0 -18
  167. data/doc/rdoc/classes/ESI/MemcachedCache.src/M000047.html +0 -18
  168. data/doc/rdoc/classes/ESI/Proxy.src/M000069.html +0 -20
  169. data/doc/rdoc/classes/ESI/Proxy.src/M000070.html +0 -55
  170. data/doc/rdoc/classes/ESI/Response.src/M000078.html +0 -23
  171. data/doc/rdoc/files/ext/esi/test/common_rl.html +0 -160
  172. data/doc/rdoc/files/ext/esi/test/parser_h.html +0 -101
  173. data/doc/rdoc/files/ext/esi/test/parser_rl.html +0 -827
  174. data/doc/rdoc/files/ext/esi/test/sp_c.html +0 -101
  175. data/ext/esi/test/common.rl +0 -46
  176. data/ext/esi/test/parser.c +0 -1875
  177. data/ext/esi/test/parser.h +0 -120
  178. data/ext/esi/test/parser.rl +0 -585
  179. data/ext/esi/test/sp.c +0 -125
data/lib/esi/processor.rb CHANGED
@@ -47,7 +47,7 @@ module ESI
47
47
  STDERR.puts "\n#{e.message}: #{e.backtrace.join("\n")}\n"
48
48
  ensure
49
49
  http_response.write( "0\r\n\r\n" )
50
- http_response.done = true
50
+ http_response.done = true if http_response.respond_to?(:done)
51
51
  end
52
52
 
53
53
  def send_chunk
data/lib/esi/proxy.rb CHANGED
@@ -3,13 +3,13 @@
3
3
  require 'uri'
4
4
  require 'timeout'
5
5
  require 'net/http'
6
- require 'mongrel'
7
6
  require 'esi/logger'
8
7
  require 'esi/cache'
9
8
  require 'esi/config'
10
9
  require 'esi/router'
11
10
  require 'esi/processor'
12
11
  require 'esi/version'
12
+ require 'mongrel'
13
13
 
14
14
  module ESI
15
15
 
@@ -24,24 +24,36 @@ module ESI
24
24
  @cache_buffer = nil
25
25
  end
26
26
 
27
- def process(request, response)
27
+ def process(url, request, response)
28
28
 
29
- start = Time.now
30
29
  status = 200
31
30
 
32
31
  http_params = http_params(request.params)
33
32
 
34
- url = @router.url_for(request.params["REQUEST_URI"])
35
- #log_debug "#{request.params["REQUEST_METHOD"]} => #{url}"
36
33
  chunk_count = 0
37
34
  bytes_sent = 0
35
+ sent_from_cache = false
38
36
  uri = URI.parse(url)
39
37
 
40
38
  path_with_query = uri.query ? "#{uri.path}?#{uri.query}" : uri.path
41
39
 
42
- if @config.cache.cached?( path_with_query, http_params )
43
- cache_buffer = @config.cache.get( path_with_query, http_params ).body
44
- bytes_sent = send_esi_buffered( status, response, request, http_params, cache_buffer )
40
+ # check if the origin is cached
41
+ cached_page = @config.cache.get( path_with_query, http_params )
42
+ if cached_page and cached_page.valid?
43
+ cache_buffer = cached_page.body
44
+ head, body = cache_buffer.split("\r\n\r\n")
45
+ buffer = StringIO.new
46
+ bytes_sent = send_esi_buffered( buffer, request, http_params, body )
47
+ buffer.rewind
48
+ bytes_sent = buffer.size
49
+ #puts bytes_sent.inspect
50
+ head = head.sub(/Content-Length:.*$/,"Content-Length: #{bytes_sent}")
51
+ #puts head
52
+ response.write head
53
+ response.write "\r\n\r\n"
54
+ response.write buffer.read
55
+ response.done = true if response.respond_to?(:done)
56
+ sent_from_cache = true
45
57
  else
46
58
  proxy_request = (request.params["REQUEST_METHOD"] == "POST") ?
47
59
  Net::HTTP::Post.new( path_with_query, http_params ) :
@@ -58,11 +70,7 @@ module ESI
58
70
  end
59
71
 
60
72
  end
61
-
62
- rescue => e
63
- STDERR.puts "\n#{e.message}: error at #{e.backtrace.first} msg at #{__FILE__}:#{__LINE__}\n"
64
- ensure
65
- log_request "\n#{url}, #{Time.now - start} seconds with status #{status} and #{chunk_count} chunks, #{bytes_sent} bytes\n"
73
+ [chunk_count, bytes_sent, status, sent_from_cache]
66
74
  end
67
75
 
68
76
  protected
@@ -82,37 +90,45 @@ module ESI
82
90
  end
83
91
  end
84
92
 
93
+ # build the initial HTTP HEAD response
94
+ header = Mongrel::Const::STATUS_FORMAT % [status, Mongrel::HTTP_STATUS_CODES[status]]
95
+ headers.each {|k,v| header << "#{k}: #{v}\r\n" }
85
96
 
86
97
  if status >= 500 or !@config.enable_esi_processor?( proxy_response )
87
- headers.each do|k,v|
88
- http_response.header[k] = v
89
- end
90
- proxy_direct( http_response, status, proxy_response )
98
+ http_response.write header
99
+ http_response.write "\r\n"
100
+ proxy_direct( http_response, proxy_response )
101
+ elsif http_request.params["HTTP_VERSION"] == "HTTP/1.0" and http_request.params["REQUEST_METHOD"] != "HEAD"
102
+ body = proxy_response.read_body
103
+ buffer = StringIO.new
104
+ send_esi_buffered( buffer, http_request, http_params, body )
105
+ buffer.rewind
106
+ header << "Content-Length: #{buffer.size}\r\n\r\n"
107
+ http_response.write( header )
108
+ buffer = buffer.read # replace with in memory
109
+ http_response.write( buffer )
110
+ @cache_buffer << header if @cache_buffer
111
+ @cache_buffer << buffer if @cache_buffer
112
+ [0,buffer.size]
91
113
  else
92
- if http_request.params["HTTP_VERSION"].match(/1.0/)
93
- buffer = proxy_response.read_body
94
- bytes=send_esi_buffered( status, http_response, http_request, http_params, buffer )
95
- @cache_buffer << buffer if @cache_buffer
96
- [0,bytes]
97
- else
98
- header = Mongrel::Const::STATUS_FORMAT % [status, Mongrel::HTTP_STATUS_CODES[status]]
99
- headers.each {|k,v| header << "#{k}: #{v}\r\n" }
100
- header << "Transfer-Encoding: chunked\r\n\r\n"
101
- http_response.write( header )
102
- proxy_filter_esi( http_request, http_response, http_params, proxy_response )
103
- end
114
+ # write current http header into the cache
115
+ @cache_buffer << header if @cache_buffer
116
+ header << "Transfer-Encoding: chunked\r\n\r\n" # don't save this to the cache
117
+ # now we don't know the content-length yet, but we save a spot for it
118
+ @cache_buffer << "Content-Length: \r\n\r\n" if @cache_buffer
119
+ http_response.write( header )
120
+ return [0,0] if http_request.params["REQUEST_METHOD"] == "HEAD"
121
+ proxy_filter_esi( http_request, http_response, http_params, proxy_response )
104
122
  end
105
-
106
123
  end
107
124
 
108
- def proxy_direct( http_response, status, proxy_response )
125
+ def proxy_direct( http_response, proxy_response )
109
126
  bytes_sent = 0
110
- http_response.start(status, true) do|head,out|
111
- proxy_response.read_body do|fragment|
112
- out << fragment
113
- bytes_sent += fragment.size
114
- end
127
+ proxy_response.read_body do|fragment|
128
+ http_response.write fragment
129
+ bytes_sent += fragment.size
115
130
  end
131
+ http_response.done = true if http_response.respond_to?(:done)
116
132
  return [0,bytes_sent]
117
133
  end
118
134
 
@@ -121,20 +137,14 @@ module ESI
121
137
  processor.send_body( http_request, http_params, http_response, proxy_response )
122
138
  end
123
139
 
124
- def send_esi_buffered( status, response, request, http_params, buffer )
125
- bytes = 0
126
- response.start(status, true) do|head,out|
127
- head["Server"] = SERVER
128
- parser = ESI::Parser.new( out, @router, @config.cache, 3 )
129
- parser.prepare( request.params, http_params )
130
- parser.process buffer
131
- parser.finish
132
- bytes = out.size
133
- end
134
- bytes
140
+ def send_esi_buffered( response, request, http_params, buffer )
141
+ parser = ESI::Parser.new( response, @router, @config.cache, 3 )
142
+ parser.prepare( request.params, http_params )
143
+ parser.process buffer
144
+ parser.finish
145
+ response.done = true if response.respond_to?(:done)
135
146
  end
136
147
 
137
-
138
148
  def read_status(response)
139
149
  Net::HTTPResponse::CODE_TO_OBJ.select { |k,v| v == response.class }.first[0].to_i rescue 500
140
150
  end
@@ -0,0 +1,97 @@
1
+ require 'rubygems'
2
+ require 'rack'
3
+ require 'rack/request'
4
+ require 'rack/response'
5
+ if $0 == __FILE__
6
+ $:.unshift File.join(File.dirname(__FILE__),'..')
7
+ $:.unshift File.join(File.dirname(__FILE__),'..','..','ext')
8
+ end
9
+ require 'esi/logger'
10
+ require 'esi/config'
11
+ require 'esi/proxy'
12
+
13
+ module ESI
14
+ # override how config works for rack
15
+ class RackConfig
16
+ def self.enable
17
+ ESI::Config.class_eval do
18
+ def self.config
19
+ @@config ||= ESI::Config.new( {} )
20
+ end
21
+ def self.define(listeners=nil)
22
+ puts ESI::Config.config.inspect
23
+ yield ESI::Config.config
24
+ puts ESI::Config.config.inspect
25
+ end
26
+ end
27
+ nil
28
+ end
29
+ end
30
+
31
+ # create wrappers for request
32
+ class ESI::Request < Rack::Request
33
+ def params
34
+ env
35
+ end
36
+ def request_params
37
+ self.GET.update(self.POST)
38
+ end
39
+ end
40
+
41
+ # create wrappers for response
42
+ #class ESI::Response < Rack::Response
43
+ # class Writer
44
+ # def initialize( writer )
45
+ # @writer = writer
46
+ # end
47
+ # def write(data)
48
+ # @writer.write( data )
49
+ # end
50
+ # end
51
+ # def socket
52
+ # Writer.new(self)
53
+ # end
54
+ #end
55
+
56
+ class RackAdapter
57
+ attr_reader :config
58
+ include ESI::Log
59
+
60
+ def initialize( config )
61
+ @config = config
62
+ end
63
+
64
+ def call(env)
65
+ start = Time.now
66
+ request = ESI::Request.new(env)
67
+ puts request.params["HTTP_VERSION"]
68
+
69
+ url = @config.router.url_for(request.params["REQUEST_URI"])
70
+
71
+ chunk_count = 0, bytes_sent = 0, status = 200, sent_from_cache = false
72
+ Rack::Response.new.finish do |response|
73
+ begin
74
+ chunk_count, bytes_sent, status, sent_from_cache = ESI::Proxy.new(@config).process(url, request, response)
75
+ rescue => e
76
+ log_error "\n#{e.message}: error at #{e.backtrace.first} msg at #{__FILE__}:#{__LINE__}\n"
77
+ ensure
78
+ log_request "\n#{url}, #{Time.now - start} seconds with status #{status} #{sent_from_cache ? "from cache" : ''} and #{chunk_count} chunks, #{bytes_sent} bytes\n"
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ def each
85
+ end
86
+
87
+ end
88
+ end
89
+
90
+ if $0 == __FILE__
91
+ require 'rubygems'
92
+ require 'ebb'
93
+ require 'yaml'
94
+ listeners = ESI::RackConfig.enable
95
+ eval( File.read(File.join(File.dirname(__FILE__),'..','..','samples','configs','config.rb')) )
96
+ Ebb.start_server(ESI::RackAdapter.new(ESI::Config.config), :port => 4444)
97
+ end
data/lib/esi/response.rb CHANGED
@@ -5,6 +5,35 @@ require 'thread'
5
5
  require 'stringio'
6
6
 
7
7
  module ESI
8
+ # this class allows me to write, <<, or call
9
+ # an object to send it bytes, priority being <<, then write, then call
10
+ class OutputAdapter
11
+ attr_reader :device
12
+ def initialize(output_device)
13
+ @device = output_device
14
+ if @device.respond_to?(:<<)
15
+ self.instance_eval do
16
+ def << (msg)
17
+ @device << msg
18
+ end
19
+ end
20
+ elsif @device.respond_to?(:write)
21
+ self.instance_eval do
22
+ def << (msg)
23
+ @device.write msg
24
+ end
25
+ end
26
+ elsif @device.respond_to?(:call)
27
+ self.instance_eval do
28
+ def << (msg)
29
+ @device.call msg
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ end
36
+
8
37
  class Response
9
38
  attr_reader :active_buffer
10
39
  attr_accessor :output
@@ -12,7 +41,7 @@ module ESI
12
41
  def initialize( output )
13
42
  @count = 0
14
43
  @back_buffer = []
15
- @output = output
44
+ @output = OutputAdapter.new(output)
16
45
  @last_out = 0
17
46
  @threads = []
18
47
  @active_buffer = reserve_buffer
data/lib/esi/version.rb CHANGED
@@ -2,7 +2,7 @@ module ESI #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 5
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'gruff'
3
+
4
+ trunkcsv = File.read(File.join(File.dirname(__FILE__),'benchmarks','csv-perf-trunk')).split("\n")
5
+ serial04csv = File.read(File.join(File.dirname(__FILE__),'benchmarks','csv-perf-serial0.4')).split("\n")
6
+
7
+ trunkcsv.shift
8
+ serial04csv.shift
9
+
10
+ g = Gruff::Line.new(1024)
11
+ g.title = "trunk vs 0.4"
12
+ g.font = "/usr/share/fonts/bitstream-vera/Vera.ttf" # my linux system's font
13
+
14
+
15
+ g.data("trunk",trunkcsv.collect {|pair| pair.split(",").last.to_f })
16
+
17
+ g.data("serial 0.4",serial04csv.collect {|pair| pair.split(",").last.to_f })
18
+
19
+ number_requests = 2000
20
+
21
+ # need to compute the percentiles
22
+ labels = { 1 => '1', 20 => '20', 40 => '40', 60 => '60', 80 => '80', 99 => '99' }
23
+
24
+ #g.hide_dots = true
25
+ g.labels = labels
26
+ g.y_axis_label = "Time per request (ms)"
27
+ g.x_axis_label = "Requests"
28
+ g.theme_37signals
29
+
30
+ puts "write: #{g.inspect}"
31
+ g.write(File.join(File.dirname(__FILE__),'benchmarks','perf.png'))
@@ -0,0 +1,101 @@
1
+ Percentage served,Time in ms
2
+ 0,5.000000e+00
3
+ 1,5.000000e+00
4
+ 2,5.000000e+00
5
+ 3,5.000000e+00
6
+ 4,5.000000e+00
7
+ 5,5.000000e+00
8
+ 6,5.000000e+00
9
+ 7,5.000000e+00
10
+ 8,5.000000e+00
11
+ 9,5.000000e+00
12
+ 10,5.000000e+00
13
+ 11,5.000000e+00
14
+ 12,5.000000e+00
15
+ 13,5.000000e+00
16
+ 14,5.000000e+00
17
+ 15,5.000000e+00
18
+ 16,5.000000e+00
19
+ 17,5.000000e+00
20
+ 18,5.000000e+00
21
+ 19,5.000000e+00
22
+ 20,5.000000e+00
23
+ 21,5.000000e+00
24
+ 22,5.000000e+00
25
+ 23,5.000000e+00
26
+ 24,5.000000e+00
27
+ 25,5.000000e+00
28
+ 26,5.000000e+00
29
+ 27,5.000000e+00
30
+ 28,5.000000e+00
31
+ 29,5.000000e+00
32
+ 30,5.000000e+00
33
+ 31,5.000000e+00
34
+ 32,5.000000e+00
35
+ 33,5.000000e+00
36
+ 34,5.000000e+00
37
+ 35,5.000000e+00
38
+ 36,5.000000e+00
39
+ 37,5.000000e+00
40
+ 38,5.000000e+00
41
+ 39,5.000000e+00
42
+ 40,5.000000e+00
43
+ 41,5.000000e+00
44
+ 42,5.000000e+00
45
+ 43,5.000000e+00
46
+ 44,5.000000e+00
47
+ 45,5.000000e+00
48
+ 46,5.000000e+00
49
+ 47,5.000000e+00
50
+ 48,5.000000e+00
51
+ 49,5.000000e+00
52
+ 50,5.000000e+00
53
+ 51,5.000000e+00
54
+ 52,5.000000e+00
55
+ 53,5.000000e+00
56
+ 54,5.000000e+00
57
+ 55,5.000000e+00
58
+ 56,5.000000e+00
59
+ 57,5.000000e+00
60
+ 58,5.000000e+00
61
+ 59,5.000000e+00
62
+ 60,5.000000e+00
63
+ 61,5.000000e+00
64
+ 62,5.000000e+00
65
+ 63,5.000000e+00
66
+ 64,5.000000e+00
67
+ 65,5.000000e+00
68
+ 66,5.000000e+00
69
+ 67,5.000000e+00
70
+ 68,5.000000e+00
71
+ 69,5.000000e+00
72
+ 70,5.000000e+00
73
+ 71,5.000000e+00
74
+ 72,5.000000e+00
75
+ 73,5.000000e+00
76
+ 74,5.000000e+00
77
+ 75,5.000000e+00
78
+ 76,5.000000e+00
79
+ 77,5.000000e+00
80
+ 78,5.000000e+00
81
+ 79,5.000000e+00
82
+ 80,5.000000e+00
83
+ 81,5.000000e+00
84
+ 82,5.000000e+00
85
+ 83,5.000000e+00
86
+ 84,5.000000e+00
87
+ 85,5.000000e+00
88
+ 86,5.000000e+00
89
+ 87,5.000000e+00
90
+ 88,5.000000e+00
91
+ 89,5.000000e+00
92
+ 90,5.000000e+00
93
+ 91,5.000000e+00
94
+ 92,5.000000e+00
95
+ 93,5.000000e+00
96
+ 94,5.000000e+00
97
+ 95,6.000000e+00
98
+ 96,2.800000e+01
99
+ 97,2.800000e+01
100
+ 98,3.700000e+01
101
+ 99,3.800000e+01