Skip to content

Instantly share code, notes, and snippets.

View epilys's full-sized avatar

Manos Pitsidianakis epilys

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.

@epilys
epilys / README~20240207-141357.md
Last active September 15, 2024 06:23
Caesar's Commentaries On The Gallic War interlinear translation by Frederick Holland Dewey in XeLaTeX
@epilys
epilys / list_fileinto.sieve
Created February 23, 2024 16:27
sieve filter to create new List mailboxes according to List-ID value
#require ["fileinto", "mailbox", "variables", "vnd.dovecot.debug", "regex"];
require ["fileinto", "mailbox", "variables", "regex", "vnd.dovecot.execute"];
if not exists "X-GitHub-Recipient" {
if header :regex :comparator "i;octet" "list-id" "(.*)" {
#set :lower "listid_raw" "${1}";
execute :input "${1}" :output "listid" "sanitize_name.sh";
fileinto :create "INBOX.Lists.${listid}";
stop;
@epilys
epilys / [email protected]
Created January 31, 2024 07:20
systemd service to send you an email on failure
[Unit]
Description=status email for %i to user
[Service]
Type=oneshot
ExecStart=/usr/local/bin/systemd-email [email protected] %i
User=debian
Group=systemd-journal
## Example usage:
@epilys
epilys / port-tunnel.service
Created January 31, 2024 07:15
systemd service to port forward service
[Unit]
Description=Setup a secure tunnel to a local API port on another server named my-other-server in ssh config
After=network-online.target
[Service]
SyslogIdentifier="my-api-tunnel"
ExecStartPre=/bin/sh -c "/usr/bin/ssh -O exit my-other-service; true"
ExecStop=/bin/sh -c "/usr/bin/ssh -O exit my-other-service; true"
ExecStart=/usr/bin/ssh -NT -o LogLevel=ERROR -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -L 8000:localhost:8000 my-other-service
#!python3
# Copyright (c) 2024 Manos Pitsidianakis <[email protected]>
# Licensed under the EUPL-1.2-or-later.
#
# You may obtain a copy of the Licence at:
# https://joinup.ec.europa.eu/software/page/eupl
#
# SPDX-License-Identifier: EUPL-1.2

$argon2id$v=19$m=64,t=512,p=2$xXhX0KQjCbIlev+gckWSYg$kNbnVzlIpCPwxlGME9r1sA

@epilys
epilys / LICENCE
Last active February 17, 2024 11:03
mastodon follower tracking (please do not use for unhealthy social media obsession reasons, it's for curiosity/fun only)
EUROPEAN UNION PUBLIC LICENCE v. 1.2
EUPL © the European Union 2007, 2016
This European Union Public Licence (the ‘EUPL’) applies to the Work (as defined
below) which is provided under the terms of this Licence. Any use of the Work,
other than as authorised under this Licence is prohibited (to the extent such
use is covered by a right of the copyright holder of the Work).
The Work is provided under the terms of this Licence when the Licensor (as
defined below) has placed the following notice immediately following the
#!/bin/sh
check_onwers() {
CRATE_NAME="$1"
echo "INFO: querying https://crates.io/api/v1/crates/${CRATE_NAME}/owners"
OWNERS_REPLY=$(curl --silent "https://crates.io/api/v1/crates/${CRATE_NAME}/owners")
echo "INFO: API reply was ${OWNERS_REPLY}"
@epilys
epilys / get_vim-plugged_descriptions.sh
Created July 17, 2023 10:58
Get descriptions of vim-plugged plugins from their github repo
#!/bin/zsh
for rep in $(cat ~/.config/nvim/init.vim|grep "^Plug '[^~:]\+" | sed "s/^Plug '\([^'\]*\)[/]\([^']\+\)'.*$/\1\/\2/p" | sort -d | uniq); do
echo -n "$rep " &&
curl --silent -L \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$rep | jq ".description";
done