Skip to content

Instantly share code, notes, and snippets.

View apoorvanand's full-sized avatar

Apoorw Anand apoorvanand

View GitHub Profile
services:
postiz:
image: ghcr.io/gitroomhq/postiz-app:latest
container_name: postiz
restart: always
environment:
# === Required Settings
MAIN_URL: "https://postiz.your-server.com"
FRONTEND_URL: "https://postiz.your-server.com"
NEXT_PUBLIC_BACKEND_URL: "https://postiz.your-server.com/api"
#!/bin/bash
set -e
# ===========================================
# CONFIGURATION
# ===========================================
POSTIZ_DOMAIN="social.thenotary.app"
GROWCHIEF_DOMAIN="linkedin.thenotary.app"
POSTIZ_DIR="/opt/postiz"
GROWCHIEF_DIR="/opt/growchief"
version: '3'
services:
tracecar_db:
image: postgres:13-alpine
environment:
POSTGRES_USER: tracecar
POSTGRES_PASSWORD: your_password
POSTGRES_DB: tracecar
networks:
- tracecar_network

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.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import io.lettuce.core.ClientOptions;
import io.lettuce.core.RedisURI;
import io.lettuce.core.resource.DefaultClientResources;
import io.lettuce.core.resource.DefaultEventLoopGroupProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import io.lettuce.core.ClientOptions;
import io.lettuce.core.resource.ClientResources;
import io.lettuce.core.resource.DefaultClientResources;
@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.
package reverse_proxy
import (
"bytes"
"fmt"
"github.com/viggin/svc-api-gateway/internal/models"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/palantir/stacktrace"
"github.com/sirupsen/logrus"
@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;