eli.thegreenplace.net eli.thegreenplace.net

Graceful shutdown of a TCP server in Go

This post is going to discuss how to gracefully shut down a TCP server in Go. While servers typically never stop running (until the process is killed), in some scenarios - e.g. in tests - it's useful to shut them down in an orderly way. High-level structure of TCP servers in Go Let's start with a quick review of the high-level structure of TCP servers implemented in Go. Go provides some convenient abstractions on top of...

eli.thegreenplace.net eli.thegreenplace.net

Summary of reading: October - December 2019

"What If?" by Randall Munroe - the author attemps to provide "serious scientific answers to absurd hypothetical questions", mixed with a large dose of typical XKCD humor and comics. Fun book. "Serious Cryptography" by Jean-Philippe Aumasson - a modern introduction to cryptography covering real-world algorithms and implementations. What sets this book apart from the classical crypto texts is the modern...

eli.thegreenplace.net eli.thegreenplace.net

"Beating" C with 400 lines of unoptimized assembly

Suggested title: "Beating" C with 400 lines of unoptimized assembly Earlier this week I ran into a fun quick blog post named Beating C with 70 lines of Go, which reimplements the basic functionality of wc in Go using various approaches and compares their performance. Apparently it's inspired by an earlier Haskell-based post and several other offshoots. This reminded me of my earlier post about reimplementing wc in pure...

eli.thegreenplace.net eli.thegreenplace.net

How to send good pull requests on GitHub

Over the past few years I authored or reviewed thousands of GitHub pull requests (PRs), both for work and for personal projects. I've come to believe there's a small set of useful rules of thumb for what makes a good PR, what makes a bad PR, and why getting the good ones merged is much easier - both for the PR author and the reviewer. Here's a quick checklist for a good PR. Each item is described in more detail below. Make sure the PR...

eli.thegreenplace.net eli.thegreenplace.net

Diffie-Hellman Key Exchange

This post presents the Diffie-Hellman Key Exchange (DHKE) - an important part of today's practical cryptography. Whenever you're accessing an HTTPS website, it's very likely that your browser and the server negotiated a shared secret key using the DHKE under the hood. Mathematical prerequisites The understand the math behind DHKE, you should be familiar with basic group theory. A group is a set with a binary operation, such that any...

eli.thegreenplace.net eli.thegreenplace.net

The Chinese Remainder Theorem

The Chinese Remainder Theorem (CRT) is very useful in cryptography and other domains. According to Wikipedia, its origin and name come from this riddle in a 3rd century book by a Chinese mathematician: There are certain things whose number is unknown. If we count them by threes, we have two left over; by fives, we have three left over; and by sevens, two are left over. How many things are there? Mathematically, this is a system of...

eli.thegreenplace.net eli.thegreenplace.net

Passing callbacks and pointers to Cgo

Cgo enables Go programs to invoke C libraries or any other library that exposes a C API. As such, it's a important part of a Go programmer's toolbox. Using Cgo can be tricky, however, especially when passing pointers and callback functions between Go and C code. This post discusses an end-to-end example that covers: Basic usage of Cgo, including linking a custom C library into the Go binary. Passing structs from Go to C. Passing Go...