Skip to content

Instantly share code, notes, and snippets.

View yoshuawuyts's full-sized avatar

Yosh yoshuawuyts

View GitHub Profile
rust-play /
Created September 16, 2019 02:59
Code shared from the Rust Playground
#[derive(Debug, PartialEq)]
enum State {
Waiting { waiting_time: usize },
Filling { rate: usize },
#[derive(Debug, Clone, Copy)]
enum Event {
KodrAus /
Last active June 29, 2020 19:11
Converting between `tracing` key values and `log` key values
use std::fmt;
use log::kv::{self, Source, value::{self, Fill}};
use tracing::{Value, Field, field::{self, Visit}};
pub struct LogField<'kvs>(&'kvs Field, &'kvs dyn Value);
impl fmt::Debug for LogField<'_> {
View hypercore trees
Roots for record 0 (stored at leaf 0): [ 0 ]
0: 0
Roots for record 1 (stored at leaf 2): [ 1 ]
0: 0─┐
1: 2─┘
Roots for record 2 (stored at leaf 4): [ 1, 4 ]
0: 0─┐
jamesmunns /
Created July 28, 2018 16:04 — forked from kylemanna/
Minimal instructions for installing arch linux on an UEFI NVMe system with full system encryption using dm-crypt and luks
valeriomazzeo / github_release.rb
Created July 5, 2017 21:24
Creates or update a GitHub release for the given tag name
View github_release.rb
#!/usr/bin/env ruby
require 'optparse'
require 'octokit'
options = {} do |opt|
opt.on('-s', '--secret SECRET', 'GitHub access token') { |o| options[:secret] = o }
opt.on('-r', '--repo-slug REPO_SLUG', 'Repo slug. i.e.: apple/swift') { |o| options[:repo_slug] = o }
opt.on('-c', '--changelog-file CHANGELOG_FILE', 'Changelog path') { |o| options[:changelog_file] = o }
mafintosh /
Last active June 21, 2018 17:40
Resets the npm defaults to how they used to be
npm config set loglevel http
npm config set progress false
npm config set package-lock false
npm config set save false
mkdir -p ~/.config/configstore/
printf '{"optOut": true,"lastUpdateCheck": 0}' > ~/.config/configstore/update-notifier-npm.json

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.

sfan5 /
Last active July 16, 2023 14:11
bootable systemd-nspawn containers with Linux distributions: Alpine, Arch Linux, Ubuntu
#!/bin/bash -e
# Creates a systemd-nspawn container with Alpine
if [ $UID -ne 0 ]; then
echo "run this script as root" >&2