10 November 2018

net-hippie 0.2.0

by mo


I recently updated net-hippie to support a couple of new features.

Authorization Headers

The first feature is a simplified API for generating Basic and Bearer Authorization headers.

Hereโ€™s an example:

require 'net/hippie'

client = Net::Hippie::Client.new
headers = { 'Authorization' => Net::Hippie.basic_auth('username', 'password') }
client.get('https://www.example.org', headers: headers)

The ability to send the Basic Authentication scheme in the Authorization header was already available but it wasnโ€™t immediately obvious how to do so.

0.2.0 introduces two new methods:

  • Net::Hippie.basic_auth(username, password)
  • Net::Hippie.bearer_auth(token)

These methods can be used to generate properly encoded Authorization headers.

Retries with exponential backoff + jitter

The second feature is the automatic retries with exponential back off + jitter.

The network is unreliable.

The new retry API extends the existing Net::Hippie::Client functionality.

E.g.

require 'net/hippie'

client = Net::Hippie::Client.new
response = client.with_retry(retries: 6) do |x|
  x.get(uri)
end

The with_retry method accepts a retries option that allows you to specify how many retries to attempt before failing the request.

Each retry gets a delay between requests. The retry delay is calculated using an exponential backoff.

E.g.

  • retry 1 -> delay 0.1 seconds
  • retry 2 -> delay 0.2 seconds
  • retry 3 -> delay 0.4 seconds
  • retry 4 -> delay 0.8 seconds
  • retry 5 -> delay 1.6 seconds
  • retry 6 -> delay 3.2 seconds
  • retry 7 -> delay 6.4 seconds
  • retry 8 -> delay 12.8 seconds

The more retries you add, the longer the delay between retries. On top of the exponential delay, a jitter is calculated to reduce the chance of multiple clients using the same deterministic algorithm to take down the target host again. The first retry starts with 100ms delay and begins to increment from there.

Example log file:

WARN -- : `execution expired` Retry: 1/6 Delay: 0.12793953723427418s
WARN -- : `execution expired` Retry: 2/6 Delay: 0.21066872302157263s
WARN -- : `execution expired` Retry: 3/6 Delay: 0.4009757967921617s
WARN -- : `execution expired` Retry: 4/6 Delay: 0.8152826302255023s
WARN -- : `execution expired` Retry: 5/6 Delay: 1.6068949702520532s
WARN -- : `execution expired` Retry: 6/6 Delay: 3.204004944487974s

You can download the latest version from RubyGems.

Peace, ๐Ÿ’– and Ruby!

๐Ÿ’Ž