Skip to content

Instantly share code, notes, and snippets.

View samarpanda's full-sized avatar
👋

Samar Panda samarpanda

👋
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.

@abhishekdubey1
abhishekdubey1 / linkedin-manage-requests
Last active April 13, 2021 01:46
Scroll down to comments to see how to use this code snippet
function manageRequests(acceptAll, ignoreAll, acceptCount){
let invitationsList = document.getElementsByClassName('mn-invitation-list')[0]
let requests = invitationsList.querySelectorAll('li')
let lengthOfRequests = requests.length
function alertUser(fnCall, count){
alert(`Hey User, you are about to ${fnCall} - ${count} connection requests`)
}
function handleRequests(num, count) {
for(let i=0; i<count; i++) {
requests[i].querySelector('.invitation-card__action-container').querySelectorAll('button')[num].click()
@rauschma
rauschma / impatient-js-es2021.md
Last active August 31, 2023 07:02
ES2021 edition of “JavaScript for impatient programmers”

What is new in the ES2021 edition of “JavaScript for impatient programmers”?

Free to read online: exploringjs.com/impatient-js/

  • The exercises now run as native ESM modules on Node.js. Previously, they were run via the esm package.
  • Material on new ES2021 features:
    • String.prototype.replaceAll()
    • Promise.any()
    • Logical assignment operators
  • Underscores (_) as separators in number literals and bigint literals
@tkadlec
tkadlec / perf-diagnostics.css
Last active August 17, 2024 19:33
CSS used to highlight potential performance issues
:root {
--violation-color: red; /* used for clear issues */
--warning-color: orange; /* used for potential issues we should look into */
}
/* IMAGES */
/*
* Lazy-Loaded Images Check
* ====
@Nooshu
Nooshu / other-run-metrics.jq
Created October 7, 2020 23:26
Extract other important page load metrics from the WebPageTest API
#!/usr/bin/env jq -rMf
# Extract other metrics from the run data
# Headers for resulting CSV
["TTFB", "First Contentful Paint", "Start Render", "DOM Complete" , "Fully Loaded"],
# drill down into the runs data
(
.data.runs
# convert the run data into an object and drill down into the run request data
@charlespwd
charlespwd / make-mp4-from-profile
Last active November 1, 2020 09:19
Make a mp4 from a Chrome DevTools Performance Profile
#!/usr/bin/env bash
set -eou pipefail
profile=$1
frameRate=${2:-10}
timeline='timeline.json'
cat $profile \
| jq '.[] | select(.args.snapshot | . and (type == "string") and contains("/9j"))' \
| jq -s . \
> $timeline
Commands
------------
1. Build Docker Image
docker build -t test .
2. Run container /w image
docker run -d --publish 8888:5000 test
3. Login to ECR
aws ecr get-login-password --region REGIONHERE!!!! | docker login --username AWS --password-stdin ACCOUNTIDHERE!!!!.dkr.ecr.REGIONHERE!!!.amazonaws.com
@Mau5Machine
Mau5Machine / dynamic.yaml
Last active January 31, 2023 08:05
Traefik Dynamic Configuration File
## Setting up the middleware for redirect to https ##
http:
middlewares:
redirect:
redirectScheme:
scheme: https
@Mau5Machine
Mau5Machine / docker-compose.yml
Last active July 4, 2024 16:57
Traefik Configuration and Setup
version: "3.3"
services:
################################################
#### Traefik Proxy Setup #####
###############################################
traefik:
image: traefik:v2.0
restart: always