Skip to content

Instantly share code, notes, and snippets.

View apoorvanand's full-sized avatar

Apoorw Anand apoorvanand

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.

@apoorvanand
apoorvanand / multiple-exec-at-go-example.go
Created March 30, 2023 20:17 — forked from ahmetozer/multiple-exec-at-go-example.go
Golang Multiple exec.Command at same time with pipelining
package main
import (
// "fmt"
"os/exec"
"os"
)
func main() {
topCommand := exec.Command("top","-d 0.5", "-b", "-n 5")
@apoorvanand
apoorvanand / ubuntu-vnc-activate-remote.sh
Created February 21, 2023 09:05 — forked from samba/ubuntu-vnc-activate-remote.sh
Activate VNC support in Ubuntu, from command-line (for active sessions)
#!/bin/sh
# This assumes you have access to the system via SSH already, and need
# remote VNC access as the same user, and you want only the primary display.
export DISPLAY=:0
# Encoded password with http://www.motobit.com/util/base64-decoder-encoder.asp
export VNC_PASSWORD="dm5jX3Bhc3N3b3JkNzE=" # vnc_password71
export VNC_PASSWORD="dm5jX3Bhc3M=" # vnc_password (a character limit is enforced?)
# Sadly many common VNC clients don't support encryption.
@apoorvanand
apoorvanand / HttpInvocation.java
Created November 8, 2022 11:06 — forked from petrbouda/HttpInvocation.java
Retry Mechanism for JDK HTTP Client
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
@apoorvanand
apoorvanand / clean_code.md
Created September 16, 2022 21:23 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

@apoorvanand
apoorvanand / System Design.md
Created August 7, 2022 10:55 — forked from vasanthk/System Design.md
System Design Cheatsheet

System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
@apoorvanand
apoorvanand / simpleChord.py
Created July 24, 2022 06:04 — forked from whong92/simpleChord.py
Short python script of a simplified simulation of Chord P2P Distributed Hash Table (DHT) system. Contains functions to build the ring and simulate a search path in the DHT when a query from a peer (node) is given.
from pprint import pprint
def binarySearch(alist, item):
"""
binary search given alist and item to search for
returns is element was found, otherwise return the last midpoint before search failed
last midpoint is always either the predessecor or succesor to item (if the item is not found)
"""
first = 0
last = len(alist)-1
@apoorvanand
apoorvanand / DNUMS.py
Created July 17, 2022 14:43 — forked from viveksyngh/DNUMS.py
You are given an array of N integers, A1, A2 ,…, AN and an integer K. Return the of count of distinct numbers in all windows of size K. Formally, return an array of size N-K+1 where i’th element in this array contains number of distinct elements in sequence Ai, Ai+1 ,…, Ai+k-1.
class Solution:
# @param A : list of integers
# @param B : integer
# @return a list of integers
def dNums(self, A, B):
mapOfNums = {}
count = 0
ptr = -1
res = []
if B > len(A) :
@apoorvanand
apoorvanand / grokking_to_leetcode.md
Created July 10, 2022 22:11 — forked from tykurtz/grokking_to_leetcode.md
Grokking the coding interview equivalent leetcode problems

GROKKING NOTES

I liked the way Grokking the coding interview organized problems into learnable patterns. However, the course is expensive and the majority of the time the problems are copy-pasted from leetcode. As the explanations on leetcode are usually just as good, the course really boils down to being a glorified curated list of leetcode problems.

So below I made a list of leetcode problems that are as close to grokking problems as possible.

Pattern: Sliding Window

@apoorvanand
apoorvanand / sudoku_solver.py
Created July 1, 2022 13:54 — forked from RuolinZheng08/sudoku_solver.py
[Python] Sudoku Solver | CodePath Dynamic Programming & Backtracking Project
from itertools import product
class SudokuSolver:
"""
Solve a sudoku puzzle given as a 81-digit string with . denoting empty
"""
SHAPE = 9
GRID = 3
EMPTY = '.'