Skip to content

Instantly share code, notes, and snippets.

View twlvl's full-sized avatar

Kiryl Ye twlvl

  • Something Meaningful
  • 16:38 (UTC +03:00)
View GitHub Profile

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.

@twlvl
twlvl / AnyContextProvider.tsx
Last active March 6, 2025 13:54 — forked from programmer106/AnyContextProvider.tsx
ContextProvider.tsx
import React, { ConsumerProps, createContext, useCallback, useMemo, useState } from "react";
interface AnyContext {
isAnyCharacteristic: boolean,
anyApi: () => void,
}
export const AnyContext = createContext<AnyContext>({
isAnyCharacteristic: false,
anyApi: () => {throw new Error('Method is not implemented!')}
@twlvl
twlvl / _0__ssl_certbot_letsencrypt.md
Created February 13, 2024 17:07 — forked from maxivak/_0__ssl_certbot_letsencrypt.md
Let's encrypt SSL certificates using certbot in docker

Directories on host machine:

  • /data/certbot/letsencrypt

  • /data/certbot/www

  • Nginx server in docker container

docker run -d --name nginx \
@twlvl
twlvl / lodash-underscore-debounce-mock_jasmine_angular.js
Last active October 9, 2024 11:13 — forked from lancecaraccioli/lodash-debounce-mock_jasmine_angular
Mock underscore or lodash debounce funciton with passing the args
/**
* @description
* Rudimentary mock of lodash.debounce, which assumes the default debounce options
* - Jasmine 2.0.0
* - lodash 2.4.1, underscore 1.7.0
* - AngularJS 1.3.0
*/
function getDebounceMock ($timeout) {
return function mockDebounceForAngularJS(fn, ms) {
var callPromise;
// Intercepting HTTP calls with AngularJS.
angular.module('MyApp', [])
.config(function ($provide, $httpProvider) {
// Intercept http calls.
$provide.factory('MyHttpInterceptor', function ($q) {
return {
// On request success
request: function (config) {
// console.log(config); // Contains the data about the request before it is sent.
@twlvl
twlvl / OpenWithSublimeText3.bat
Last active June 8, 2019 11:05
Bat file for windows. Add open folder as sublime project in context menu
rem Run it with admin permissions
@echo off
SET st3Path=C:\Program Files\Sublime Text 3\sublime_text.exe
rem add it for all file types
@reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3" /t REG_SZ /v "" /d "Open with Sublime Text 3" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3" /t REG_EXPAND_SZ /v "Icon" /d "%st3Path%,0" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st3Path% \"%%1\"" /f
rem add it for folders