こんにちは私はThinのドキュメントを読んでいて、eventmachineはかなり新しいですが、Deferrablesがどのように機能するかを知っています。私の目標は、体が部分的に延期されてストリーミングされるときに、Thinがどのように機能するかを理解することです。
以下は、私が取り組んでいて頭を動かそうとしている例です。
class DeferrableBody
include EventMachine::Deferrable
def call(body)
body.each do |chunk|
@body_callback.call(chunk)
end
# @body_callback.call()
end
def each &blk
@body_callback = blk
end
end
class AsyncApp
# This is a template async response. N.B. Can't use string for body on 1.9
AsyncResponse = [-1, {}, []].freeze
puts "Aysnc testing #{AsyncResponse.inspect}"
def call(env)
body = DeferrableBody.new
# Get the headers out there asap, let the client know we're alive...
EventMachine::next_tick do
puts "Next tick running....."
env['async.callback'].call [200, {'Content-Type' => 'text/plain'}, body]
end
# Semi-emulate a long db request, instead of a timer, in reality we'd be
# waiting for the response data. Whilst this happens, other connections
# can be serviced.
# This could be any callback based thing though, a deferrable waiting on
# IO data, a db request, an http request, an smtp send, whatever.
EventMachine::add_timer(2) do
puts "Timer started.."
body.call ["Woah, async!\n"]
EventMachine::add_timer(5) {
# This could actually happen any time, you could spawn off to new
# threads, pause as a good looking lady walks by, whatever.
# Just shows off how we can defer chunks of data in the body, you can
# even call this many times.
body.call ["Cheers then!"]
puts "Succeed Called."
body.succeed
}
end
# throw :async # Still works for supporting non-async frameworks...
puts "Async REsponse sent."
AsyncResponse # May end up in Rack :-)
end
end
# The additions to env for async.connection and async.callback absolutely
# destroy the speed of the request if Lint is doing it's checks on env.
# It is also important to note that an async response will not pass through
# any further middleware, as the async response notification has been passed
# right up to the webserver, and the callback goes directly there too.
# Middleware could possibly catch :async, and also provide a different
# async.connection and async.callback.
# use Rack::Lint
run AsyncApp.new
call
私がはっきりと理解していない部分は、とメソッドのDeferrableBodyクラス内で何が起こるかeach
です。
タイマーが@body_callbackに格納されたブロックとして起動すると、それぞれがデータのチャンクを受信し、本体でsuccessが呼び出されると本文を送信しますが、これらのブロックでisyield
または call
呼び出されると、送信時にどのように単一のメッセージになりますか。
何が起こっているのかを理解するのに十分なクロージャを理解していないように感じます。これに関する助けをいただければ幸いです。
ありがとうございました。