Friday 11:30 a.m.–noon

Kneel And Disconnect: Getting The Fastest Connection Out Of A Hostname

Ashwini Oruganti

Audience level:
Intermediate
Category:
Python Libraries

Description

Did you know that when you resolve a hostname, you can get multiple addresses back, and pick any one to connect to? Some of these addresses will connect near instantly, while others might take a long time or time out. This talk is about a Twisted endpoint API I built that takes a hostname, and returns the connection that takes the least time to complete, from the list of resolved host addresses.

Abstract

Meet [`HostnameEndpoint`][1] - it takes a hostname, does a `getaddrinfo` lookup on it, picks the first IPv4 and IPv6 address out of the returned list, and then using [a time-involving algorithm][3], tries connecting to both of them. It then gives you the connection that is established first, discarding the other one. All this sounds like it would take a lot of time, but here’s the interesting part: it does not. In this talk, I aim to walk you through the development of `HostnameEndpoint`, right from the beginning till its successful merge to the Twisted codebase. I’ll begin by talking about the use of time-related functions in Twisted, and how one can write fast and predictable tests for them. I’ll also tell you how Twisted performs multiple network operations “in parallel” (no, *not* threads), and how that’s what the reactor’s job is, essentially. As we progress, I’ll discuss the design decisions involved in the development of this endpoint and the important lessons on unit testing and test-driven development I learnt. I’ll then take you through the exciting journey of writing Twisted code, illustrate how the frightening Deferreds can be tamed to do your bidding, the ample number of times I found roadblocks and how I pushed them all away to put everything together and make it work. Finally, I'll show you how it’s still not perfect and could be improved further – largely by [starting over][2] with some parts of the code. [1]: https://twistedmatrix.com/trac/ticket/4859 [2]: https://twistedmatrix.com/trac/ticket/6698 [3]: http://tools.ietf.org/html/rfc6555#section-6