Skip to content

Instantly share code, notes, and snippets.

@mccutchen
mccutchen / .envrc
Created November 3, 2022 14:06
Example direnv .envrc for Python projects that manages a local virtualenv that updates automatically when requirements.txt changes
#!/bin/bash
VENV_DIR=".venv"
REQUIREMENTS_HASH_FILE="$VENV_DIR/requirements.shasum"
REQUIREMENTS_HASH_FUNC="shasum"
REQUIREMENTS_HASH_VAL=$($REQUIREMENTS_HASH_FUNC requirements.txt | cut -d ' ' -f 1)
function reset_venv() {
rm -rf "$VENV_DIR"
@creack
creack / main.go
Created January 7, 2018 17:30 — forked from enricofoltran/main.go
A simple golang web server with basic logging, tracing, health check, graceful shutdown and zero dependencies
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
@enricofoltran
enricofoltran / main.go
Last active April 1, 2024 00:17
A simple golang web server with basic logging, tracing, health check, graceful shutdown and zero dependencies
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
@matijs
matijs / README.md
Last active February 22, 2024 05:35
Solarized Dark profile for macOS Terminal.app

Solarized Dark profile for Terminal.app on macOS High Sierra

Based on the excellent Solarized (Dark) created by Ethan Schoonover. For source code, check the main Solarized repository on GitHub.

Installation

Open and save Solarized Dark.terminal.

Import from the “Profiles” tab in the settings of Terminal.app or just double-click the file after downloading.

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.

@wonderbeyond
wonderbeyond / graceful_shutdown_tornado_web_server.py
Last active February 22, 2023 05:34 — forked from mywaiting/graceful_shutdown_tornado_web_server.py
The example to how to shutdown tornado web server gracefully...
#!/usr/bin/env python
"""
How to use it:
1. Just `kill -2 PROCESS_ID` or `kill -15 PROCESS_ID`,
The Tornado Web Server Will shutdown after process all the request.
2. When you run it behind Nginx, it can graceful reboot your production server.
"""
import time
@mccutchen
mccutchen / gen_batches.py
Last active August 29, 2015 14:07
flexible batching of sequences in Python
def gen_batches(xs, size):
"""
Given a sequence xs and a batch size, yield batches from the sequence as
lists of length size, where the last batch might be smaller than the
rest.
>>> list(gen_batches(range(9), 3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> list(gen_batches(range(11), 3))
@acolyer
acolyer / service-checklist.md
Last active January 30, 2024 17:39
Internet Scale Services Checklist

Internet Scale Services Checklist

A checklist for designing and developing internet scale services, inspired by James Hamilton's 2007 paper "On Desgining and Deploying Internet-Scale Services."

Basic tenets

  • Does the design expect failures to happen regularly and handle them gracefully?
  • Have we kept things as simple as possible?
@piscisaureus
piscisaureus / pr.md
Created August 13, 2012 16:12
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

@mccutchen
mccutchen / run_proc.py
Created June 12, 2012 18:50
run_proc.py
def run_proc(cmd, stdin=None, env=None):
"""Runs the given cmd as a subprocess, where cmd is a list suitable
for passing to subprocess.call. Returns a 3-tuple of
(exit code, stdout, stderr)
If stdin is not None, it will be passed into the subprocess on STDIN. If
env is not None, it will be used to augment the environment of the
subprocess.
"""