NullVoxPopuli / meshchat-core

core functionality of meshchat implemented in ruby.
MIT License
3 stars 0 forks source link

http requests can be async! #7

Closed NullVoxPopuli closed 8 years ago

NullVoxPopuli commented 8 years ago

https://www.igvita.com/2009/05/13/fibers-cooperative-scheduling-in-ruby/

require 'eventmachine'
require 'em-http'
require 'fiber'

def async_fetch(url)
  f = Fiber.current
  http = EventMachine::HttpRequest.new(url).get :timeout => 10
  http.callback { f.resume(http) }
  http.errback { f.resume(http) }

  return Fiber.yield
end

EventMachine.run do
  Fiber.new{
    puts "Setting up HTTP request #1"
    data = async_fetch('http://www.google.com/')
    puts "Fetched page #1: #{data.response_header.status}"

    EventMachine.stop
  }.resume
end
NullVoxPopuli commented 8 years ago

For General chat, or anything where multiple long-running tasks are running, do them in parallel! https://github.com/igrigorik/em-synchrony#fiber-aware-multi-interface-parallel-http-requests

NullVoxPopuli commented 8 years ago

Status Update: http requests are now evented.

I don't know if its worth it to use synchrony, cause I don't really care for batch requests to finish at the same time.