Skip to content

Instantly share code, notes, and snippets.

View annismckenzie's full-sized avatar

Daniel Lohse annismckenzie

View GitHub Profile
@annismckenzie
annismckenzie / bash_strict_mode.md
Created January 25, 2023 21:22 — forked from mohanpedala/bash_strict_mode.md
set -e, -u, -o, -x pipefail explanation
@annismckenzie
annismckenzie / main.go
Last active September 30, 2018 12:07 — forked from zupzup/main.go
Log deletion script for ELK-stack logs written in Go – see https://zupzup.org/log-deletion-elk-go/
package main
import (
"context"
"flag"
log "github.com/sirupsen/logrus"
"gopkg.in/olivere/elastic.v6"
"os"
"strings"
"time"

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.

@annismckenzie
annismckenzie / buffering_response_writer.go
Created February 26, 2017 12:56
Example of a gin middleware to handle sessions and gin's response writer
package responseWriter
import (
"bufio"
"bytes"
"net"
"net/http"
"net/http/httptest"
)

Keybase proof

I hereby claim:

  • I am annismckenzie on github.
  • I am annismckenzie (https://keybase.io/annismckenzie) on keybase.
  • I have a public key ASB9U9Z8wcPQS7pNHi_jB2ssUidCZ-s5A891ReQuWTgjRAo

To claim this, I am signing this object: