Skip to content

Instantly share code, notes, and snippets.

View kdwinter's full-sized avatar

Kenneth De Winter kdwinter

  • Ghent, Belgium
  • 16:15 (UTC +01:00)
View GitHub Profile
@rpheath
rpheath / authlogic-to-devise.md
Last active December 22, 2024 04:36
Steps and troubleshooting tips when moving Authlogic to Devise.

How To: Authlogic to Devise

Step 1: Migration

  1. bin/rails g migration AuthlogicToDevise
  2. (see the file below for the actual migration, authlogic_to_devise.rb)
  3. bin/rake db:migrate

Step 2: Update Gemfile

  1. gem "devise", "~> 2.2.0"
  2. bundle install
@otobrglez
otobrglez / jaccard_recommendation.rb
Last active April 2, 2024 17:51
Simple recommendation system written in Ruby based on Jaccard index.
# Simple Recommendation Engine in Ruby
# Visit: http://otobrglez.opalab.com
# Author: Oto Brglez <[email protected]>
class Book < Struct.new(:title)
def words
@words ||= self.title.gsub(/[a-zA-Z]{3,}/).map(&:downcase).uniq.sort
end
anonymous
anonymous / arch install
Created June 20, 2015 06:36
#!/bin/bash
#This script is best used if modified for each person
#The install can quickly completed if there is a local mirror.
#on an existing arch install, run: darkhttpd /var/cache/pacman/pkg/
#then set LOCALMIRROR to point to that machine
#It's okay if errors come up, pacman will resolve them.
export LOCALMIRROR="Server = http://192.168.1.1:8080"
export MIRRORLIST="/etc/pacman.d/mirrorlist"
@fntlnz
fntlnz / i3wm-fedora.md
Last active December 23, 2024 08:47
i3wm fedora lightdm

Install i3

dnf install i3

Add exec i3 to xinitrc

echo "exec i3" > ~/.xinitrc
@shiroyasha
shiroyasha / dog.rb
Created February 16, 2016 22:20
Method tracer for Ruby classes
class Dog
attr_writer :name
def initialize(name)
@name = name
end
def bark
puts "patrick"
end
@aaronlauterer
aaronlauterer / Arch-ZFSRoot-on-dm-crypt-UEFI
Last active November 5, 2017 11:18
ZFSRoot installation over a dm-crypt volume for Arch Linux (UEFI)
# arch uefi dm-crypt zfsroot install (archiso)
# modified to work with current repos as of 2016-06-16 and with hints from
# comments by larskotthoff
# uses only one boot partition for EFI and initramfs
# partition disk
# start at 1MB (sector 2048)
512Mib EFI
@johnramsden
johnramsden / zfsinstall-1-setup.sh
Last active October 25, 2025 02:36
Install scripts for installing Arch Linux on ZFS. Not runnable, just listed commands.
#!/bin/bash
# Check before running, may need intervention
# Pass in the following to the script, or hardcode it.
# Uncomment if hardcoding input.
BOOT_PARTITION="/dev/sdg1"
DISK_1="ata-SanDisk_SDSSDXPS480G_152271401093"
DISK_2="ata-SanDisk_SDSSDXPS480G_154501401266"
POOL="vault"
@saponace
saponace / LUKS + ext4 on one disk
Last active May 25, 2019 23:29
RAID1 disk setup
Let /dev/sdb be the disk.
# Create partition
sudo cfdisk /dev/sdb -> cerate partition /dev/sdb1
# Initialize disk data with random bits
sudo shred --verbose --random-source=/dev/urandom --iterations=3 /dev/sdb1
# Create the cryptographic device (leave hash and iter-time to default values if the drive will be mounted on a slow device)
sudo cryptsetup --verbose --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 5000 luksFormat /dev/sdb1
# Open device
sudo cryptsetup open --type luks /dev/sdb1 DEVSDB1
var str = 'class ಠ_ಠ extends Array {constructor(j = "a", ...c) {const q = (({u: e}) => {return { [`s${c}`]: Symbol(j) };})({});super(j, q, ...c);}}' +
'new Promise((f) => {const a = function* (){return "\u{20BB7}".match(/./u)[0].length === 2 || true;};for (let vre of a()) {' +
'const [uw, as, he, re] = [new Set(), new WeakSet(), new Map(), new WeakMap()];break;}f(new Proxy({}, {get: (han, h) => h in han ? han[h] ' +
': "42".repeat(0o10)}));}).then(bi => new ಠ_ಠ(bi.rd));';
try {
eval(str);
} catch(e) {
alert('Your browser does not support ES6!')
}

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.