Skip to content

Instantly share code, notes, and snippets.

@raggi
Created September 1, 2010 01:34
Show Gist options
  • Save raggi/560087 to your computer and use it in GitHub Desktop.
Save raggi/560087 to your computer and use it in GitHub Desktop.
the secrets of the web scale sauce
require 'eventmachine'
EM.run do
SEKRET_SAUCE = EM.attach(
open(RUBY_PLATFORM =~ /mswin|mingw/ ? 'NUL:' : '/dev/null', 'w')
)
EM.start_server('0.0.0.0', 80, Module.new do
def post_init; proxy_incoming_to(SEKRET_SAUCE); end
end)
end
require 'socket'
SEKRET_SAUCE = open RUBY_PLATFORM =~ /mswin|mingw/ ? 'NUL:' : '/dev/null', 'w'
Socket.tcp_server_loop(80) do |client|
Thread.new { IO.copy_stream(client, SEKRET_SAUCE) }
end
#!/usr/bin/env ruby
abort 'Usage: shardnull path num_shards' unless ARGV.size == 2
abort 'shardnull must be run as root' unless Process.uid == 0
require 'rubygems'
require 'ffi'
class File
extend FFI::Library
ffi_lib 'c'
attach_function :mknod, [ :string, :int, :int ], :bool
attach_function :perror, [ :string ], :void
end
path, num_shards = *ARGV
shards = Array.new(num_shards.to_i) { |n| File.join(path, "null#{n}") }
shards.each do |path|
if File.mknod(path, 020666, File.stat('/dev/null').rdev)
File.perror("Error making #{path}")
abort
end
# modes are set via umask (laaame), so we have to open this up here:
File.chmod 0666, path
puts path
end
@tarcieri
Copy link

Holy shit, you literally sharded /dev/null

@raggi
Copy link
Author

raggi commented May 20, 2012

SCALING BRO. HOW DO YOU DO IT? 🤘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment