Skip to content

Instantly share code, notes, and snippets.

View raggi's full-sized avatar

James Tucker raggi

View GitHub Profile
@raggi
raggi / config.ru
Created November 19, 2008 03:28 — forked from lifo/gist:26399
#!/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
#!/usr/bin/env rackup
require 'forward'
run Rack::Forwarder.new('google.com')
@raggi
raggi / gist:61367
Created February 10, 2009 12:36 — forked from tmm1/gist:61014
class FiberedMutex
#
# Creates a new FiberedMutex
#
def initialize
@waiting = []
@locked = false
end
#
@raggi
raggi / gist:79926
Created March 16, 2009 15:26
Example implementation for Sinatra::Async, to be used with async thin
# 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 March 17, 2009 02:47
eventmachine postgres to sequel async shim
# 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 March 18, 2009 15:40
sinatra as rails metal example - mostly unnecessary
require 'sinatra/metal'
class SinatraMetal < Sinatra::Base
include Sinatra::Metal
get '/sinatra' do
'hello sinatra!'
end
end
@raggi
raggi / gdb_stack_size.rb
Created April 30, 2009 02:40
show the effects of stack size on ruby's performance
#!/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 02:20
EventMachine.crank
require 'thread'
module EventMachine
module Test
module Utils
module Cranking
# Stolen from MenTaLguYs Omnibus
class Waiter
def initialize
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)
@raggi
raggi / return.rb
Created June 18, 2009 17:04 — forked from lifo/return.rb
module Enumerable
def return
each do |i|
value = yield(i)
return value if value
end
nil
end
end