Skip to content

Instantly share code, notes, and snippets.

@toddlers
toddlers / function.js
Created August 31, 2017 07:15 — forked from vgeshel/function.js
AWS Lambda function for forwarding SNS notifications to Slack
console.log('Loading function');
const https = require('https');
const url = require('url');
// to get the slack hook url, go into slack admin and create a new "Incoming Webhook" integration
const slack_url = 'https://hooks.slack.com/services/...';
const slack_req_opts = url.parse(slack_url);
slack_req_opts.method = 'POST';
slack_req_opts.headers = {'Content-Type': 'application/json'};
package httptimeout
import (
"net/http"
"time"
"fmt"
)
type TimeoutTransport struct {
http.Transport
@toddlers
toddlers / httpclient.go
Created May 25, 2017 15:33 — forked from dmichael/httpclient.go
Light wrapper for the Go http client adding (essential) timeouts for both connect and readwrite.
package httpclient
import (
"net"
"net/http"
"time"
)
type Config struct {
ConnectTimeout time.Duration
@toddlers
toddlers / soft_net.sh
Created April 20, 2017 11:10
Print network soft IRQ back-pressure stats
#!/bin/bash
# A variation of:
# https://github.com/majek/dump/blob/master/how-to-receive-a-packet/softnet.sh
# by [email protected] / https://github.com/jwbensley
cmd="${0##*/}"
usage() {
cat >&2 <<EOI

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.

@toddlers
toddlers / _aws_golang_examples.md
Created March 5, 2017 05:43 — forked from eferro/_aws_golang_examples.md
golang aws: examples

AWS Golang SDK examples

@toddlers
toddlers / relink.py
Created February 24, 2017 07:38 — forked from reorx/relink.py
Utils for porting linux man page to dash.app
@toddlers
toddlers / piping.go
Created January 16, 2017 17:50 — forked from kylelemons/piping.go
piping exec.Cmd in golang (example sorts all regular files under a directory by their extension)
package main
import (
"bytes"
"exec"
"log"
"os"
)
// Pipeline strings together the given exec.Cmd commands in a similar fashion
@toddlers
toddlers / check.go
Created December 27, 2016 18:12 — forked from mattes/check.go
Check if file or directory exists in Golang
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
// path/to/whatever does not exist
}
if _, err := os.Stat("/path/to/whatever"); err == nil {
// path/to/whatever exists
}