Skip to content

Instantly share code, notes, and snippets.

View safe_hash.rb
# frozen_string_literal: true
class SafeHash < Hash
def initialize(namespace)
@namespace = namespace
@lock =
merge!({ @namespace => })
View tbqueue.rb
class TBQueue
def initialize size
tvar = Concurrent::TVar
@read = []
@write = []
@rsize = 0
@wsize = size

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

View fiber_sleep.rb
require 'fiber'
require 'concurrent' # gem install concurrent-ruby
Thread.abort_on_exception = true
class Async
def self.perform(&block)
instance = new
until do
unless instance.yields.empty?
View gist:dfbedcd2793b4a013f10611526d4847f
benolee / Dynflow
Created Jan 13, 2018 — forked from ShimShtein/Dynflow
Dynflow performance analysis.
View Dynflow

break /home/shim/Documents/foreman/dynflow/lib/dynflow/utils.rb:60

Why do we need indifferent_hash while saving?

 "/home/shim/Documents/foreman/dynflow/lib/dynflow/utils.rb:60:in `indifferent_hash'",
 "/home/shim/Documents/foreman/dynflow/lib/dynflow/persistence_adapters/sequel.rb:268:in `extract_metadata'",
 "/home/shim/Documents/foreman/dynflow/lib/dynflow/persistence_adapters/sequel.rb:226:in `prepare_record'",
 "/home/shim/Documents/foreman/dynflow/lib/dynflow/persistence_adapters/sequel.rb:236:in `save'",
 "/home/shim/Documents/foreman/dynflow/lib/dynflow/persistence_adapters/sequel.rb:125:in `save_action'",
benolee /
Created Nov 17, 2017 — forked from robbwagoner/
nginx socket socat
benolee /
Created Sep 28, 2017 — forked from marshallm/
Baseline pt-archiver bash script for MySQL
source mysql_access
TODAY=$(date +"%Y-%m-%d_%H%M%S")
mysql api_test \
--socket=$MYSQL_SOCK \

Debugging ruby performance - Aman Gupta

C, Linux, networks, cpu, and memory usage

  • lsof -nPp <pid>
  • tcpdump
  • write the data to a file, load it up in wireshark
  • strace, trace systems calls and signals (jump into kernelspace)
  • SIGVTALRM, ruby 1.8 every 10msec to switch threads (green threads)
  • posix-spawn instead of fork-exec, check out posix-spawn gem
  • ltrace

– ltrace -c ruby foo.rb # summary mode



An introduction to curl using GitHub's API

The Basics

Makes a basic GET request to the specifed URI