Welcome ! This is the personal site / blog of Graham King. Most people come for the credit card generator, but I think the Categories (top right) are more interesting.

July 21, 2013

Online upgrades in Go

Posted in Software at 05:40 by graham

tl;dr Send your socket fd over a UNIX domain socket: syscall/passfd_test.go.

When your server holds long running connections (WebSocket, long-running HTTP, IRC, XMPP, etc) you often want to be able to upgrade the server without dropping the connections (zero downtime upgrade). In UNIX there are at least two ways to do this:

  1. Inherit the file descriptor
  2. Send the file descriptor over a domain socket

The first one is straightforward, because a UNIX process automatically inherits the file descriptors of it’s parent, except if they have the close-on-exec flag set. Go complicates things a bit by always setting that flag on it’s sockets (in net/sock_cloexec.go). For a child process to inherit it’s parent’s file descriptors, you have to manually add them to ExtraFiles in os/exec/Cmd. There’s an example in TestExtraFiles in os/exec/exec_test.go.

Usually you need to send more that just the connections to the child process. There will be some state, and probably a communication where the child tells the parents it’s ready to take over (after priming it’s cache, for example). Hence the second approach, unix domain sockets, is more interesting.

Read the rest of this entry »

June 14, 2013

Quote of the day: Bruce Schneier

Posted in Society at 04:02 by graham

The lesson here is that it is insufficient to protect ourselves with laws; we need to protect ourselves with mathematics

He’s talking about lessons from the Clipper Chip (bet you’d forgotten that one!), but he could just as easily be talking about current events.

May 27, 2013

Co-located teams are a business risk

Posted in Behaviour at 23:53 by graham

Early in my career, I worked for a company run by two ex-military officers. When they attended a distant meeting, they would take separate flights, because surely the company would not survive if they were both hurt in a crash. They never got injured in a plane, but they did get sick at the same time (the company survived). Shared offices turned out more dangerous than shared aeroplanes.

There’s a risk to placing your most valuable people within sneezing distance of each other.

You probably know and talk of your team’s “bus number”, but sickness strikes far more often than buses. We’ve all seen co-located teams drop one by one, and you’ve probably wished a sick colleague had stayed home rather than share his germs with you.

The biggest risk to humanity in the next 50 years is an influenza outbreak, according to Vaclav Smil in Global Catastrophes and Trends: The Next Fifty Years. Influenza doesn’t spread through IRC.

If we’re to build resilient companies, we need to think about what actually takes us away from our work, and structure our environment to mitigate that.

How many people can you afford to lose to sickness? For how long?

May 2, 2013

We are all polyglots

Posted in Software at 17:24 by graham

I used to know two programming languages at any one time; what I called a serious language and a what I called a scripting language. My initial serious language was C, my scripting language was Perl. The serious language was for client work, it paid the bills. The scripting language was for tools and toys (which is why many early web-apps were Perl CGI scripts).

We’ve been replacing C as our serious language since the 70s. C++ mostly succeeded, and became the official language of Microsoft Windows. Objective-C got a solid niche when Apple chose it for OSX, and later iOS. Java, became the serious language of web apps, and is now the language of Android. The two recent exciting developments here are Go and Rust.

In scripting-language world, Perl was largely replaced by Python and Ruby, and for web-app work by PHP.

So by now my serious language was Java, and my scripting language Python. But then three interesting things happened.

Read the rest of this entry »

April 26, 2013

Rust: What I learnt so far

Posted in Software at 22:58 by graham

This applies to 0.7pre, many things have changed in 0.8. Particularly core was renamed to std, and std renamed to extra.

Rust is an open-source programming language being developed mostly by Mozilla. It’s goal is the type of applications currently written in C++ (such as Firefox). Details at the Rust Wikipedia page.

I’ve been learning bits of it the past few days, and whilst Rust is still rough around the edges there’s a lot to enjoy. Rust is only at v0.7pre and changing daily, so you may have to adjust some of the code here.

Rust is a big language, and unless you come from C++ it will probably make your head hurt. In a good way :-)

The two most helpful introductions I have found so far are:

I’d encourage you to run through both of those, starting with Rust for Rubyists. When you get stuck reading one of them (and you will), switch back here.



At time of writing Rust is v0.7pre:

Read the rest of this entry »

March 26, 2013

PyCon 2013: My two favorite talks

Posted in Software at 16:41 by graham

PyCon is an annual gathering of Python programmers. All the talks are recorded and distributed freely on the web. My two favorite talks were:

March 5, 2013

Chapter books for a three / four year old

Posted in Misc at 23:32 by graham

Starting in summer 2012, when my son was 3.5, I started reading chapter books to him, a chapter or two a night. Before we started chapter books, it was always easy to find good books to read to them (from board books upwards). Now though, I struggled finding good chapter books for this age group.

I’ve collected a list of the ones we tried, in order, with notes if appropriate. I’m linking all the books to Amazon to be clear which book it was, but we got most from our local library.

Jason’s Quest – A lovely book to start with. A wonderful adventure, which never gets too frightening, or too hard to follow, and the good folks win in a good way.

Fantastic Mr. Fox – Might have been a little too frightening at the beginning, with the mean farmers trying to kill the fox. In retrospect I would have saved it till he was a few months older.

Mr. Popper’s Penguins

Read the rest of this entry »

February 23, 2013

Google World

Posted in Society at 05:54 by graham

First thing in the morning, I check my gmail on my Google Nexus 7 tablet, or my Nexus 4 phone, whichever is nearest. I have a conference call so I fire up my Chromebook. Google’s browser, Chrome, uses Google’s public DNS servers ( and Google network’s protocol (SPDY), to connect to gmail.

In the Google Calendar invite, I click the Google Hangouts link and video conference. I’m lucky to live in Kansas City, with it’s super-fast Google Fiber internet connection. We update a Google Doc, stored on Google Drive, with notes during the meeting.

I tell the team that the next version of our web app, written in Go (Google’s server language) was just uploaded onto Google App Engine (their app hosting platform). The client side is in Javascript, but we might update it to use Dart (Google’s client-side language), when that’s ready.

We’ve already got Google Checkout payment integrated, which makes measuring our Google Ads conversions with Google Analytics easy. We’re hoping to rank highly in Google Search, or no-one will find us. We’ve just started promoting it on our G+ page (we migrated from Blogger), and our Youtube demo video seemed popular.

Who knows, if the product goes really well, Google might acquire us.

It’s just a silly story. Move along. :-)

January 15, 2013

Keyword arguments in C

Posted in Software at 06:03 by graham

This is valid C nowadays:

my_func(.username="Bob", .is_admin=true);

I found it in 21st Century C. It requires a macro and a structure, and relies on three features introduced in C99.

#include <stdio.h>      // printf
#include <stdbool.h>    // bool - there's a bool type now

// Macro that turns the kwargs into an struct
#define my_func(...) my_func_base(\
    (struct user){.is_admin=false, __VA_ARGS__});

struct user {
    char *username;
    bool is_admin;

// The actual function - and yes there's single line comments too
void my_func_base(struct user u) {
    printf("Hello %s\n", u.username);

int main(int argc, char *argv[]) {
    my_func(.username="Bob", .is_admin=true);

The three new features introduced in C99 that make this possible are:

  • Compound literals which allow my_user = (user) {"Bob", true}.
  • Designated initializers which give us struct user my_user = {.username="Test", .is_admin=true}
  • Variadic macros which allow #define‘s to take ... as a parameter and have it substituted wherever __VA_ARGS__ appears in that macro.

All the keyword arguments are optional, because compound literal rules state that any unspecified arguments are set to zero / null of the appropriate type.

We even have default arguments, like the .is_admin=false in the macro above, because designated initializer rules state that if the argument is repeated, the last argument wins.

December 31, 2012

How will you measure your life? – book notes

Posted in Society at 19:37 by graham

How Will You Measure Your Life?, by Clayton M. Christensen is life advice for M.B.A. graduates. It dispenses valuable career, relationship, and ethics advice in business-school language.

High-achieving individuals (he’s a Harvard business-school professor) often over-invest themselves in their career and under-invest themselves in their family, and regret this later in life. This book is his attempt to correct that.

Read the rest of this entry »

« Previous Page« Previous entries « Previous Page · Next Page » Next entries »Next Page »