Skip to content

Instantly share code, notes, and snippets.

@creachadair
creachadair / waiter-sync.md
Last active October 26, 2021 04:24
Multiple waiter synchronization in Go

Multiple-Waiter Synchronization in Go

Often you have an object that is waiting for some result, e.g., an error to be delivered (once) by some goroutine. Multiple goroutines would like to wait for that to happen.

A traditional solution uses a condition variable, e.g.,

var result value
var ready bool
var mu sync.Mutex
@creachadair
creachadair / sync-channels.md
Last active August 21, 2026 18:47
Synchronizing close for multi-writer channels in Go

Synchronizing Close for Multi-Writer Channels in Go

(see also: https://godoc.org/github.com/creachadair/msync#Collector)

Many Go APIs allow multiple concurrent writers to a single channel, for example, to enqueue work for a task-processing API. When we do this, there is a tricky question of how to safely close the channel. Because this is tricky, many APIs do not bother, which complicates cleanup for the reader side of the channel. Dave Cheney has a good survey of channel corners that I recommend to anyone who is working in this space.

The basic problem is that writing to a closed channel triggers a panic. If some writer can detect that it is the "last", it can close the channel itself -- however that is often not practical unless all the writers are fully under the control of the API itself.

I have found a useful tactic for dealing with this situation.

@creachadair
creachadair / fast-mph.md
Created September 18, 2021 14:27
Fast minimal perfect hashing

Fast Minimal Perfect Hashing

Algorithm notes from

A. Limasset, G. Rizk, Rr. Chikhi, and P. Peterlongo: Fast and scalable minimal perfect hashing for massive key sets https://arxiv.org/pdf/1702.03154.pdf.

The goal of this algorithm is to create a conflict-free hash of the keys in the collection. The strategy is to create a zero-valued bit vector with at least 1 bit per key, and hash each key to a position in that vector. Any key that hashes uniquely is assigned that position, and removed from the working set.

@creachadair
creachadair / fuse-ref.md
Last active September 11, 2021 14:03
References on FUSE
@creachadair
creachadair / interfaces-and-tests.md
Last active October 8, 2021 22:23
Interfaces and satisfaction checks in Go packages

Interfaces and Satisfaction Checks in Go Packages

When a Go package defines a concrete type to implement some interface, it is common to ask the compiler to verify that your type satisfies the desired interface, e.g.,

package mything

import "some/other/pkg"

// Concrete implements pkg.Interface using a pellucid ammonite in
@creachadair
creachadair / binary-cookies.md
Last active November 7, 2023 16:21
macOS Binary cookies file format

Binary cookies file format

Browsers and other macOS applications use the NSHTTPCookieStorage API to store cookies. The API writes .binarycookies files in a specialized binary format. The binary file format has the following structure:

Bytes Format Description
4 text magic number ('cook')
4 uint32 BE page count (np)
*4 [i] uint32 BE page i data size S, bytes; *repeat np times
*S [i] bytes page i contents; *repeat np times
@creachadair
creachadair / ilof-queries.md
Last active December 5, 2021 17:25
Metadata queries for ILoF

Metadata Queries for ILoF

These queries require curl and jq.

  • Twitter handles for all guests who have them:

    curl -s https://inlieuof.fun/guests.json | jq -r '[.guests[].twitter|select(.)]|sort[]'
@creachadair
creachadair / chrome-encrypted-cookies.md
Last active September 20, 2026 07:52
Encryption format for Chrome browser cookies

Google Chrome Encrypted Cookies

Google Chrome stores browser cookies in an SQLite database. The database has two tables, meta containing format and version metadata, and cookies with the contents of the cookies. The cookies table uses this schema:

-- To reproduce: sqlite path/to/Cookies .schema
CREATE TABLE cookies (
   creation_utc     INTEGER  NOT NULL,  -- microseconds since epoch
   host_key         TEXT     NOT NULL,  -- domain
   name             TEXT     NOT NULL,
@creachadair
creachadair / bookmarklet.md
Last active August 26, 2023 18:13
Notes on browser bookmarklets

Bookmarklet Notes

Broadly speaking, a bookmarklet is just a bookmark containing a javascript: URL, e.g.,

javascript:void%28alert%28%22test%22%29%29

Note that the contents of the URL need to be escaped in the Usual Way™.

@creachadair
creachadair / install-go.sh
Last active May 3, 2026 15:00
Install Go from source at a tagged version
#!/usr/bin/env bash
##
## Usage: install-go.sh [<version>]
## e.g.: install-go.sh 1.22.3
##
## If a version is omitted, the latest available release is installed.
##
## By default, the script installs a precompiled toolchain for the specified OS
## and architecture. Set SOURCE=1 to build from source instead. Source builds
## require a bootstrap toolchain, which the script will fetch if needed.