Skip to content

Instantly share code, notes, and snippets.

@rcrowley
rcrowley / sendmsg.go
Created February 19, 2014 17:07
sendmsg(2) SCM_RIGHTS demo
package main
import (
"fmt"
"log"
"net"
"os"
"os/exec"
"reflect"
"syscall"
@rcrowley
rcrowley / bad-side-effects-on-mac-osx.sh
Last active August 29, 2015 13:56
A protip for authors of shell programs. I use this pattern *all the time* for working in a temporary directory and cleaning it up after I'm done but a quirk in how shells interpret failures in command substitution caused a program like these to remove my coworker's home directory when he ran said program on Mac OS X, which doesn't have `mktemp -d`.
set -e
# Looks clever, is short, but removes your working directory on Mac OS X
# where `mktemp -d` fails.
cd "$(mktemp -d)"
trap "rm -rf \"$PWD\"" EXIT INT QUIT TERM
# ...
@rcrowley
rcrowley / st.diff
Created November 14, 2013 22:52
Support X-Delete-After headers and fix segmented uploads in Rackspace's Swift Tool, st.
--- st.orig 2013-11-14 21:59:32.014428532 +0000 +++ st 2013-11-14 22:45:48.542834157 +0000
@@ -1455,6 +1455,8 @@
dest='leave_segments', default=False, help='Indicates that you want '
'the older segments of manifest objects left alone (in the case of '
'overwrites)')
+ parser.add_option('', '--ttl', dest='ttl', help='Set a time-to-live for '
+ 'the object via the X-Delete-After header.')
(options, args) = parse_args(parser, args)
args = args[1:]
if len(args) < 2:
@rcrowley
rcrowley / gist:6888979
Last active December 25, 2015 00:39
The things you have to do to process stdin line-by-line in Node.js and it still blows up this process' memory over blocking whatever's connected to stdin.
var readline = require('readline')
var ch = []
, closed = false
, rl = readline.createInterface({
input: process.stdin
, output: process.stdout
, terminal: false
})
@rcrowley
rcrowley / statebird.sh
Created July 18, 2013 15:04
How to get a reservation at State Bird Provisions
set -e
mail() {
echo "$@" |
/usr/bin/mail -a"From: Richard Crowley <r@rcrowley.org>" -s"State Bird Provisions" "1234567890@vtext.com"
echo "$(date): there was a reservation!" >&2
}
TMP="$(mktemp)"
trap "mail \"FAILURE\"; rm -f \"$TMP\"" EXIT INT QUIT TERM
@rcrowley
rcrowley / gist:6005614
Created July 16, 2013 03:50
Sausage balls

Sausage balls

Get your hands dirty, mixing the following in a big bowl:

  • 4 pounds sausage
  • 4 eggs, slightly beaten
  • 1 1/2 cups bread crumbs

Make 1-inch or a bit smaller balls from the sausage and brown them on all sides in a frying pan. With that done, combine the following in a slow cooker for the sauce:

@rcrowley
rcrowley / ssh.sh
Created May 23, 2013 20:53
VirtualBox-aware ssh(1)
#!/bin/sh
set -e
#/ Usage: ssh [...] [<username>@]<hostname>|<vmname> [...]
: ${BASE:="$HOME/VirtualBox VMs"}
usage() {
grep "^#/" "$0" | cut -c"4-" >&2
@rcrowley
rcrowley / grace.go
Last active March 1, 2023 16:06
Graceful stop in Go
package main
import (
"log"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
@rcrowley
rcrowley / acknowledgment_test.go
Last active October 14, 2022 09:22
Benchmark of goroutine acknowledgment patterns
// Go encourages us to organize our code using goroutines and to use
// channels of channels to implement request-response semantics [1].
//
// I have encountered far more instances that require acknowledgment
// than fully-fledged respones so I became curious whether channels
// of channels were indeed the best implementation strategy.
//
// In summary, yes, they are. These benchmarks demonstrate that
// channels perform better than mutexes, that condition variables are
// still clumsy, and that preallocation is a huge win when and if you
VERSION="2.4.1"
BUILD="betable1"
set -e -x
OLDESTPWD="$PWD"
cd "$(mktemp -d)"
trap "rm -rf \"$PWD\"" EXIT INT QUIT TERM