gotry

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

GoTry - Simple Retry Module for Go

GoTry is a lightweight, configurable retry module for Go, designed to make retrying operations like HTTP requests easier. It allows developers to specify custom retry logic, backoff strategies, and error handling in a simple and flexible way.

Features

  • Configurable Retry Logic: Set the number of retries, backoff duration, and maximum jitter.
  • Custom Retry Conditions: Define conditions under which a retry should be attempted.
  • Retry Callbacks: Execute custom code on each retry attempt.

Installation

To install GoTry, use go get:

go get github.com/PabloSanchi/gotry

Usage

package main

import (
    "fmt"
    "net/http"
    "time"

    "github.com/PabloSanchi/gotry"
)

func main() {
    retryableFunc := func() (*http.Response, error) {
        // replace with your actual HTTP request logic
        return http.Get("https://example.com")
    }

    resp, err := gotry.Retry(retryableFunc,
        gotry.WithRetries(5),
        gotry.WithBackoff(2*time.Second),
        gotry.WithMaxJitter(500*time.Millisecond),
        gotry.WithOnRetry(func(attempt uint, err error) {
            fmt.Printf("Retry attempt %d: %v\n", attempt, err)
        }),
    )

    if err != nil {
        fmt.Printf("Failed to get response: %v\n", err)
        return
    }

    // we assume there is a body from this request;
    defer resp.Body.Close()
    body := resp.Body
    
    // do something with the body
    // ...
}

Configuration Options

  • WithRetries(uint): Set the number of retry attempts.
  • WithBackoff(time.Duration): Set the backoff duration between retries.
  • WithMaxJitter(time.Duration): Add jitter to the backoff duration.
  • WithOnRetry(OnRetryFunc): Define a callback function to be executed on each retry.
  • WithRetryIf(RetryIfFunc): Specify the condition under which retries should be attempted.

License

This project is licensed under the Apache License. See the LICENSE file for details.

Simplification of the Avast retry-go module

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry

func Retry(retryableFunc RetryableFuncWithResponse, options ...RetryOption) (*http.Response, error)

Retry retries the provided retryableFunc according to the retry configuration options.

Types

type OnRetryFunc

type OnRetryFunc func(attempt uint, err error)

OnRetryFunc is a function type that is called on each retry attempt.

type RetryConfig

type RetryConfig struct {
	// contains filtered or unexported fields
}

RetryConfig contains configuration options for the retry mechanism.

type RetryIfFunc

type RetryIfFunc func(error) bool

RetryIfFunc determines whether a retry should be attempted based on the error.

type RetryOption

type RetryOption func(*RetryConfig)

RetryOption is a function type for modifying RetryConfig options.

func WithBackoff

func WithBackoff(backoff time.Duration) RetryOption

WithBackoff sets the backoff duration between retries.

func WithMaxJitter

func WithMaxJitter(maxJitter time.Duration) RetryOption

WithMaxJitter sets the maximum jitter duration to add to the backoff.

func WithOnRetry

func WithOnRetry(onRetry OnRetryFunc) RetryOption

WithOnRetry sets the callback function to execute on each retry.

func WithRetries

func WithRetries(retries uint) RetryOption

WithRetries sets the number of retries for the retry configuration.

func WithRetryIf

func WithRetryIf(retryIf RetryIfFunc) RetryOption

WithRetryIf sets the condition to determine whether to retry based on the error.

type RetryableFuncWithResponse

type RetryableFuncWithResponse func() (*http.Response, error)

RetryableFuncWithResponse represents a function that returns an HTTP response or an error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL