Skip to content

Instantly share code, notes, and snippets.

Avatar

James Tucker raggi

View GitHub Profile
View config.ru
#!/usr/bin/env thin -e production -R config.ru -V start
# config.ru
# N.B. Rack::Lint doesn't yet support the api, use production mode rack.
require 'eventmachine'
class AsyncApp
class DeferrableBody
include EventMachine::Deferrable
View config.ru
#!/usr/bin/env rackup
require 'forward'
run Rack::Forwarder.new('google.com')
View gist:61367
class FiberedMutex
#
# Creates a new FiberedMutex
#
def initialize
@waiting = []
@locked = false
end
#
@raggi
raggi / gist:79926
Created Mar 16, 2009
Example implementation for Sinatra::Async, to be used with async thin
View gist:79926
# async_sinatra_example.ru
require 'sinatra'
# Normally Sinatra::Base expects that the completion of a request is
# determined by the block exiting, and returning a value for the body.
#
# In an async environment, we want to tell the webserver that we're not going
# to provide a response now, but some time in the future.
#
# The a* methods provide a method for doing this, by informing the server of
@raggi
raggi / gist:80247
Created Mar 17, 2009
eventmachine postgres to sequel async shim
View gist:80247
# I stole this code from em-mysql (tmm1 <3)
module Sequel
class Database
attr_accessor :_async
end
class Dataset
def async_insert *args, &cb
db._async.insert insert_sql(*args), &cb
nil
@raggi
raggi / sinatra_metal.rb
Created Mar 18, 2009
sinatra as rails metal example - mostly unnecessary
View sinatra_metal.rb
require 'sinatra/metal'
class SinatraMetal < Sinatra::Base
include Sinatra::Metal
get '/sinatra' do
'hello sinatra!'
end
end
@raggi
raggi / gdb_stack_size.rb
Created Apr 30, 2009
show the effects of stack size on ruby's performance
View gdb_stack_size.rb
#!/usr/bin/env ruby
require "benchmark"
bit64 = 1.size == 8
open('gdb-script', 'w') do |f|
f.puts <<-EOS
p ((unsigned int)rb_gc_stack_start - (unsigned int)$#{bit64 ? 'r' : 'e'}sp)
detach
@raggi
raggi / cranking.rb
Created May 29, 2009
EventMachine.crank
View cranking.rb
require 'thread'
module EventMachine
module Test
module Utils
module Cranking
# Stolen from MenTaLguYs Omnibus
class Waiter
def initialize
View benchmark.rb
require "benchmark"
class Hash
# Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking precedence over the second.
def reverse_merge(other_hash)
other_hash.merge(self)
end
# Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking precedence over the second.
# Modifies the receiver in place.
def reverse_merge!(other_hash)
View return.rb
module Enumerable
def return
each do |i|
value = yield(i)
return value if value
end
nil
end
end