Skip to content

Instantly share code, notes, and snippets.

View chrismaximin's full-sized avatar
🌐
WIRED

Chris Maximin chrismaximin

🌐
WIRED
View GitHub Profile
@chrismaximin
chrismaximin / 0-test.rb
Last active August 2, 2020 17:00
ActiveRecord's after_commit callback triggered once per row
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
@chrismaximin
chrismaximin / c10k.ru
Created November 2, 2018 08:02 — forked from WJWH/c10k.ru
The double hijack trick, serving 65k connections from a single ruby process
# This server demo does a socket hijack in Rack and then saves the socket to a global variable
# to prevent it from being GCed when the Puma thread ends. It will then write "BEEP" to each
# socket every ten seconds to prevent the connection timing out. During testing, it easily
# handled up to 65523 connections, after which it ran into the `ulimit` for open file descriptors.
# The bit with the waiting area is there because a normal `Set` is not thread safe and it would
# drop socket due to race conditions. The `Queue` is thread safe and will make sure all sockets
# are preserved.
# run with `rackup -q -p 8000 -o 0.0.0.0 c10k.ru`
# testing: install `ab` and then run `ab -c 20000 -n 20000 <ip adress of server>:8000/
@chrismaximin
chrismaximin / activerecord-inconsistent-caching.rb
Last active October 30, 2018 19:33
ActiveRecord inconsistent clearing of QueryCache
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
gem "sqlite3"
end
require "active_record"
@chrismaximin
chrismaximin / bench-fixnum.rb
Created September 14, 2018 14:20
Ruby next and +
require 'benchmark/ips'
ARRAY = [*1..1000]
def one_plus
ARRAY.each { |a| 1 + a }
end
def plus_one
ARRAY.each { |a| a + 1 }
require 'benchmark/ips'
ARRAY = [*1..1000]
def fastest
!ARRAY.empty?
end
def faster
ARRAY.size != 0