Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
Server launched: 10 acceptors, 10 workers.
Exception in thread "RubyThread-34: /Users/ryan/fun/timeserver/lib/timeserver/server.rb:37" java.nio.channels.IllegalBlockingModeException
at java.nio.channels.spi.AbstractSelectableChannel.configureBlocking(AbstractSelectableChannel.java:257)
at org.jruby.util.io.SelectBlob.tidyUp(SelectBlob.java:352)
at org.jruby.util.io.SelectBlob.goForIt(SelectBlob.java:94)
at org.jruby.RubyIO.select_static(RubyIO.java:3398)
at org.jruby.RubyIO.select(RubyIO.java:3394)
at org.jruby.RubyIO$INVOKER$s$0$3$select.call(RubyIO$INVOKER$s$0$3$select.gen:65535)
at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:69)
Server created with 5 acceptors and 10 workers.
Exception in thread "RubyThread-15: server.rb:1" java.lang.NullPointerException
at org.jruby.ext.socket.RubyTCPServer.accept(RubyTCPServer.java:180)
at org.jruby.ext.socket.RubyTCPServer$i$0$0$accept.call(RubyTCPServer$i$0$0$accept.gen:65535)
at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:133)
at server.chained_5_rescue_2$RUBY$SYNTHETICrun_acceptors(server.rb:35)
at server.chained_4_rescue_1$RUBY$SYNTHETICrun_acceptors(server.rb:34)
at server.block_1$RUBY$run_acceptors(server.rb:33)
at server$block_1$RUBY$run_acceptors.call(server$block_1$RUBY$run_acceptors:65535)
at org.jruby.runtime.CompiledBlock19.yield(CompiledBlock19.java:163)
@ryanlecompte
ryanlecompte / gist:2280799
Created April 2, 2012 04:41
Example of a preforking server that uses threads instead of Kernel#fork
require 'socket'
require 'thread'
# Example of a "threaded preforking" server using multiple acceptor threads
# and a pool of worker threads to service the actual requests. Note
# that this is similar to a real preforking server that uses Kernel#fork
# to service requests concurrently with a single listener socket. The only
# difference is that instead of multiple forked processes, we use multiple
# acceptor threads.
class Server
@ryanlecompte
ryanlecompte / gist:2163807
Created March 22, 2012 20:34
Monkey-patching the aws-sdk gem to dump its raw requests
require 'aws-sdk'
module RequestDumper
def new(*args, &block)
super.extend(DumpRequests)
end
module DumpRequests
def request(*args, &block)
puts "SENDING: #{args.first.body}"
@ryanlecompte
ryanlecompte / processor.rb
Created February 5, 2012 04:12
alternative to invoking middleware in sidekiq
def process(msg)
klass = constantize(msg['class'])
invoke_chain(klass.new, msg)
@boss.processor_done!(current_actor)
end
def invoke_chain(worker, msg)
chain = Sidekiq::Middleware::Chain.retrieve.dup
traverse_chain = lambda do
if chain.empty?
big_hash = 10_000.times.with_object({}) { |i, hash| hash[i] = Array.new(i) }
Benchmark.bm do |x|
x.report("values.min") { big_hash.values.min }
x.report("each_value.min") { big_hash.each_value.min }
end
__END__
$ ruby speed.rb
user system total real
Using /Users/ryan/.rvm/gems/ruby-1.9.2-p290
[1] pry(main)> class Foo
[1] pry(main)* define_method(:hmm, proc { |x,y| p [x,y] })
[1] pry(main)* end
=> #<Proc:0x007f8cab911060@(pry):2 (lambda)>
[2] pry(main)> Foo.new.hmm(1)
ArgumentError: wrong number of arguments (1 for 2)
from (pry):2:in `block in <class:Foo>'
[3] pry(main)> Foo.new.hmm(1,2)
[1, 2]
@ryanlecompte
ryanlecompte / gist:1645523
Created January 20, 2012 05:38
Interpreter crash
[1] pry(main)> def foo
[1] pry(main)* binding
[1] pry(main)* end
=> nil
[2] pry(main)> b = foo { puts 'hi!' }
=> #<Binding:0x007f8b7a0e2ec0>
[3] pry(main)> b.eval('Proc.new').call
(pry):5: [BUG] Segmentation fault
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.1]
class M
def self.foo
puts Z
end
class Z
end
end
M.foo
@ryanlecompte
ryanlecompte / gist:1619490
Created January 16, 2012 06:59
Experimenting with forking and unix sockets in Ruby
require 'base64'
require 'socket'
require 'fileutils'
# UnixSocketForker is an experiment of inter-process communication using
# plain unix sockets to communicate between forked processes and the
# parent process. This can also be done via IO.pipe. In this experiment,
# the jobs are simply random arrays whose sums are calculated in the forked
# worker processes.
class UnixSocketForker