Skip to content

Instantly share code, notes, and snippets.

View nerdyman's full-sized avatar
👉
q:^)

Ricky nerdyman

👉
q:^)
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.

@nerdyman
nerdyman / README.md
Created January 31, 2020 11:52
Useful links
@nerdyman
nerdyman / README.md
Last active February 29, 2024 08:28
Prompt a File Download and respect the Content Disposition Header Using Fetch and FileSaver

Prompt a File Download With the Content Disposition Header Using JavaScript

Set the Server Response Headers

Expose the Content-Disposition header using the Access-Control-Expose-Headers header and set the Content-Disposition header as you would usually.

Access-Control-Expose-Headers: Content-Disposition
Content-Disposition: attachment; filename="example-file.csv"
@nerdyman
nerdyman / fix-docker-dns.md
Last active October 6, 2020 14:53
Fix docker DNS not working on Linux hosts

Docker DNS issues on systemd distros

(Hopefully) fix Docker DNS issues with systemd distros, e.g. ping: bad address 'google.com', Temporary failure resolving ..., EAI_AGAIN (tested on Arch).

Checklist

  1. Host DNS is correct (/etc/resolv.conf) and does not contain any invalid nameservers
  2. Ensure DNS resolves correctly (dig google.com @ipaddress.from.resolvconf.here)
  3. Ensure Docker container has same config as host docker run -it <your-image> cat /etc/resolv.conf
@nerdyman
nerdyman / README.md
Last active September 4, 2023 19:38
GTA V Working on Steam Play (Proton) on Arch Linux and proprietary Nvidia drivers

Steps for Arch Linux w/ Nividia proprietary driver

  • Install wine-staging (pacman -S wine-staging)
  • Install the latest Nvidia drivers (pacman -S nvidia-dkms nvidia-utils lib32-nvidia-utils)
  • Create a GTA DXVK config file [1]
  • Set GTA 5 launch option in Steam to DXVK_CONFIG_FILE=/<PATH_TO_YOUR_CONFIG>/gta_dxvk.conf %command%
  • Copy steam/steamapps/common/Proton 3.7/user_settings.sample.py to steam/steamapps/common/Proton 3.7/user_settings.py
    • Uncomment "PROTON_NO_ESYNC": "1" in user_settings.py
  • Copy all Windows fonts from a Windows ISO to /usr/share/fonts/WindowsFonts OR install ttf-ms-win10 from the AUR
  • Run fc-cache -vfr to rebuild the font cache
@nerdyman
nerdyman / ppi-calcuation.md
Created November 9, 2017 10:14
Calculate the PPI of an image

PPI Calculation

Variables

  • ppi as int
  • pageResolution in px
  • imageResolution in px
  • pageSize in inches

Sample values

@nerdyman
nerdyman / resolve-tsconfig-path-to-webpack-alias.js
Last active March 5, 2024 01:25
Convert TypeScript tsconfig paths to webpack alias paths
const { resolve } = require('path');
/**
* Resolve tsconfig.json paths to Webpack aliases
* @param {string} tsconfigPath - Path to tsconfig
* @param {string} webpackConfigBasePath - Path from tsconfig to Webpack config to create absolute aliases
* @return {object} - Webpack alias config
*/
function resolveTsconfigPathsToAlias({
tsconfigPath = './tsconfig.json',
@nerdyman
nerdyman / contrast.less
Created June 1, 2016 13:02
LESS contrast mixin - contrast mixin for LESS which works likes you'd expect it to
/**
* contrast.less
* the built-in contrast() function in LESS sucks, this one works
* calculates the perceptual brightness of a color and assigns an int value to it
* to determine whether contrasting color should be dark or light
*
* usage:
* #contrast(@color: #f00; @threshold: 43; @dark: #222; @light: #eee; @more: '!important');
*
* using specific params: