Skip to content

Instantly share code, notes, and snippets.

View RubaXa's full-sized avatar
🕯️
Holding a candle while the AI is birthing «you absolutely right».

Lebedev Konstantin RubaXa

🕯️
Holding a candle while the AI is birthing «you absolutely right».
View GitHub Profile
@dergachev
dergachev / GIF-Screencast-OSX.md
Last active May 23, 2026 17:36
OS X Screencast to animated GIF

OS X Screencast to animated GIF

This gist shows how to create a GIF screencast using only free OS X tools: QuickTime, ffmpeg, and gifsicle.

Screencapture GIF

Instructions

To capture the video (filesize: 19MB), using the free "QuickTime Player" application:

/**
* Watcher leaks for jQuery
* RubaXa <trash@rubaxa.org>
* MIT Licensed.
*
* API:
* $.leaks.get();
* $.leaks.watch();
* $.leaks.unwatch();
* $.leaks.remove();
@RubaXa
RubaXa / jquery.event.scroll.js
Last active December 17, 2015 06:58
jQuery extension, add support `scrollstart` and `scrollend` events.
/**
* jQuery extension, add support `scrollstart` and `scrollend` events.
*
* @author RubaXa <trash@rubaxa.org>
* @github https://gist.github.com/RubaXa/5568964
* @license MIT
*
*
* @settings
* $.special.scrollend.delay = 300; // default ms
@maccman
maccman / jquery.ajax.queue.coffee
Last active January 13, 2018 12:03
Queueing jQuery Ajax requests. Usage $.ajax({queue: true})
$ = jQuery
queues = {}
running = false
queue = (name) ->
name = 'default' if name is true
queues[name] or= []
next = (name) ->
@RubaXa
RubaXa / Promise.js
Last active September 16, 2017 18:17
«Promise.js» — is supported as a native interface and $.Deferred.
/**
* @author RubaXa <trash@rubaxa.org>
* @license MIT
*/
(function () {
"use strict";
function _then(promise, method, callback) {
return function () {
/**
* User Timing polyfill (http://www.w3.org/TR/user-timing/)
* @author RubaXa <trash@rubaxa.org>
*/
(function (window){
var
startOffset = Date.now ? Date.now() : +(new Date)
, performance = window.performance || {}
, _entries = []
@termi
termi / deepExtend.js
Last active August 29, 2015 13:57
deep extend
var _hasOwn = Object.prototype.hasOwnProperty;
var _toString = Object.prototype.toString;
var _hasGOPD = typeof Object.getOwnPropertyDescriptor === 'function';
var _hasDP = typeof Object.defineProperty === 'function';
function _getOwnPropertyDescriptor(obj, prop) {
if ( _hasGOPD ) {
return Object.getOwnPropertyDescriptor(obj, prop)
}
else {
@RubaXa
RubaXa / xhr.js
Last active July 26, 2018 05:30
Микро обертка для выполенения XHR-запросов
/**
* Микро обертка для выполенения XHR-запросов
* @namepace window.xhr
* @example
* xhr.load('/path/to', {type: 'POST'}, function (err, xhr) {
* // ...
* });
*/
(function (global) {
'use strict';
@RubaXa
RubaXa / vars.js
Last active July 28, 2022 08:45
Multivar & Trailing comma vs. Diff
// 1. var
var name = 'name';
var i, length;
var j = 0;
// 2. var
var name = 'name';
var i;
var j = 0;
var length;

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.