Retry Go

Retry library for Go

Published on

Edit this page

Introduction

In this blog post, I will introduce a new library called retrygo that I have developed for Go. The library provides a simple and easy-to-use API for retrying functions in Go. I will also share my first experience developing a Go library and the challenges I faced during the development process.

Motivation

Recently, I was working on a project that required retrying functionality in Go. On my previous Python projects, I used the python-aioretry by kaelzhang. It has a simple and very sensible approach to retrying functions.

I searched for similar libraries in Go, but I couldn’t find one that met my requirements. Most of the existing libraries were either too complex or complicated to use. So, I decided to develop my own retry library for Go, which would be simple, performant, easy to use, and fulfill my requirements.

Required Features

Development

I started the development of the retrygo library by defining the API and the required features.

API Design

The API is built so that first the user defines the behavior at Retry, and after — we call the functions we want to retry. (As we’ll see later, this is a pretty nice improvement that has a positive impact on performance.)

I decided that I shouldn’t be tied to a specific function return value, so Retry should be generic. Retry takes in RetryPolicy and RetryConfigurer.

Implementation

The implementation of the library was quite straightforward. I started by defining the Retry struct, which holds the retry policy and the retry configurer. Then, I implemented the Do method, which takes the function to retry and executes it according to the retry policy.

After that, I wrote the tests and benchmarks to ensure the correctness and performance of the library.

Finally, I implemented the predefined backoff strategies: constant, linear, exponential, and jitter. These backoff strategies can be used to configure the retry behavior.

Interesting feature(in my opinion) is that the library allows combining multiple backoff strategies. For example, you can combine exponential backoff with jitter to create a more complex retry strategy.

Benchmark

Benchmark Environment

Benchmark Setup

I used the Go benchmarking tool to measure the performance of the retrygo library and compare it with other existing retry libraries in Go. I created benchmarks package

Benchmark Results

Results you can find in the benchmarks-results gist.

Benchmark Analysis

icit-retrygo

avast-retry-go

sethvargo-go-retry

References