DISQUS

macournoyer: Pusher & Async With Thin - macournoyer's blog

  • kabari · 6 months ago
    Dude...you're like a hero.
  • superjared · 6 months ago
    Thanks for the writeup!

    There is a typo:

    EM.next_tick dp
  • macournoyer · 6 months ago
    hey thx guys! glad you like it.

    I fixed the typo.
  • Collin Miller · 6 months ago
    Oh excellent!
    Mind if I use your DeferrableBody code in http://github.com/collin/orbited-ruby
  • macournoyer · 6 months ago
    Hey Collin, that looks like a really cool project!
    Of course fell free to use DeferrableBody. I stole it from raggi initially.
  • Collin Miller · 5 months ago
    @macournoyer

    :) Awesome. This post and the resources linked to within have proven invaluable :)

    http://github.com/collin/orbited-ruby/tree/master

    Have it almost working with the existing Orbited client code :)

    Anybody have a notion of when we can expect to have a unified asynchronous callback api in Rack? Thin is nice, but it'd be even nicer if more rack servers could be expected to run this stuff.
  • raggi · 3 months ago
    Awesome writeup Marc :-)
  • collintmiller · 3 months ago
    I've been doing more with this callback lately and I've found a use-case that I couldn't figure out how to do without a modification to Thin.

    http://gist.github.com/187018

    I'm using rack to build an http proxy. The proxy works rougly like this:

    Request comes in.
    Open Socket to the host and port of the Request.
    Replay entire Request Body onto Socket.
    As data comes back over Socket, write to Request's Async Chunk Callback.

    Any thoughts about better ways to do this?
  • raggi · 3 months ago
    Use a deferrable body?
  • collintmiller · 3 months ago
    I am using a modified deferrable body, the issue I'm running into is I don't want to return a standard [status, headers, deferrable_body] response.

    With my proxy I want to play the "remote" response back directly over the "local" request.

    My rack handler looks like this:

    def call(env)
    request = Rack::Request.new
    # using a modified DeferrableBody so I can control the callback used.
    socket = EM.connect request.host, request.port, RequestProxy, DeferrableBody(@env['async.chunk_callback'])
    socket.send_data @env['http.request_body'] # plain, unparsed FULL http request
    end

    class TCPSocketProxy < EventMachine::Connection
    def initialize socket # socket is the async chunk callback
    @socket = socket
    end

    def recieve_data data
    @socket.call data
    #
    EM.next_tick { close_conncetion } if finished?
    end

    def unbind
    @socket.succeed
    end

    def finished?
    # I have implementations of these, but need to redo them with a proper HTTPResponse parser
    return false unless headers_recieved?
    return true if not_modified?
    return false unless content_length
    return true if body_length = content_length
    end
    end
  • raggi · 3 months ago
    pop into #eventmachine on freenode sometime, we can probably help you out
  • mattkaufman · 2 months ago
    great, thanks for the sinatra add on...