Skip to content

Instantly share code, notes, and snippets.

<?php
/*
Paul's Simple Diff Algorithm v 0.1
(C) Paul Butler 2007 <http://www.paulbutler.org/>
May be used and distributed under the zlib/libpng license.
This code is intended for learning purposes; it was written with short
code taking priority over performance. It could be used in a practical
application, but there are a few ways it could be optimized.
var context = new webkitAudioContext(),
oscillator = context.createOscillator();
oscillator.type = oscillator.SQUARE;
oscillator.frequency.value = 1000;
oscillator.connect(context.destination);
oscillator.noteOn && oscillator.noteOn(0);
setTimeout('oscillator.disconnect()', 500);
var playSingleChime = function()
{
var context = new webkitAudioContext(),
gain = context.createGainNode(),
osc = context.createOscillator();
osc.connect(gain);
gain.connect(context.destination);
osc.frequency.value = 1000.0;
@dmytro
dmytro / ssh-multi.sh
Created October 31, 2012 03:46
Start multiple synchronized SSH connections with Tmux
#!/bin/bash
# ssh-multi
# D.Kovalov
# Based on http://linuxpixies.blogspot.jp/2011/06/tmux-copy-mode-and-how-to-control.html
# a script to ssh multiple servers over multiple tmux panes
starttmux() {
if [ -z "$HOSTS" ]; then
@jonschlinkert
jonschlinkert / markdown-cheatsheet.md
Last active April 11, 2024 04:45
A better markdown cheatsheet.
@thanpolas
thanpolas / Gruntfie.js
Last active December 26, 2015 16:48
Grunt Config for node server + livereload
module.exports = function (grunt) {
require('load-grunt-tasks')(grunt);
grunt.initConfig({
express: {
options: {
// Override defaults here
},
web: {
options: {
@sielay
sielay / gist:0aa4077829f35f5e0310f9e0cc9fdc71
Created August 10, 2016 10:06
Haproxy - Capture client IP when behind CloudFlare or not. Also keep x-forwarded-for in logs
frontend www-http
bind :80
bind *:443 ssl crt /etc/haproxy/certs no-sslv3
capture request header X-Forwarded-For len 50
acl is_cf req.hdr(cf-connecting-ip) -m found
http-request set-header X-Client-IP %[src] if !is_cf
http-request set-header X-Client-IP %[hdr(cf-connecting-ip)] if is_cf

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.

@zveronline
zveronline / haproxy-cloudflare
Last active September 29, 2023 07:17 — forked from sielay/gist:0aa4077829f35f5e0310f9e0cc9fdc71
Haproxy - Capture client IP when behind CloudFlare or not. Also keep x-forwarded-for in logs
acl from_cf src -f /etc/haproxy/cf-ips
http-request set-src req.hdr(CF-Connecting-IP) if from_cf
option forwardfor if-none
@jult
jult / sysctl.conf
Last active April 23, 2024 11:32
[Debian 12 update!] sysctl config for linux server with 8 GB DDR4 RAM or more, SSD and 1Gbe (or faster) NIC
kernel.core_uses_pid = 1
kernel.domainname = your_local_domain_name
kernel.msgmax = 65535
kernel.msgmnb = 65535
kernel.pid_max = 65535
kernel.printk = 2 3 1 2
kernel.randomize_va_space = 2
kernel.shmall = 268435456
kernel.shmmax = 268435456
kernel.sysrq = 0