retry

package
v1.67.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 3 Imported by: 2

README

Package retry

Пакет retry предоставляет обёртку над "github.com/cenkalti/backoff/v4" для повторения операций с учётом времени и контекста.

Types

ExponentialBackoff

Структура ExponentialBackoff инкапсулирует в себе параметры экспоненциального бэкоффа, используемого для повторных попыток выполнения операций.

Methods:

func NewExponentialBackoff(maxElapsedTime time.Duration) ExponentialBackoff

Создаёт новый экземпляр ExponentialBackoff с заданным максимальным временем выполнения.

(e ExponentialBackoff) Do(ctx context.Context, operation func() error) error

Выполняет переданную операцию с использованием экспоненциального бэкоффа и поддержки контекста. Если операция завершается с ошибкой, она будет повторяться до истечения maxElapsedTime или отмены контекста.

Usage

package main

import (
	"context"
	"errors"
	"log"
	"time"

	"github.com/txix-open/isp-kit/retry"
)

func main() {
	retrier := retry.NewExponentialBackoff(10 * time.Second)

	err := retrier.Do(context.Background(), func() error {
		// Здесь должна быть логика, которую нужно повторить при ошибке
		log.Println("trying...")
		return errors.New("failed")
	})

	if err != nil {
		log.Fatalf("operation failed after retries: %v", err)
	}
}

Documentation

Overview

Package retry provides utilities for performing operations with retry logic. It wraps the cenkalti/backoff library to support exponential backoff strategies with context cancellation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExponentialBackoff

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

ExponentialBackoff configures and executes operations using an exponential backoff strategy.

func NewExponentialBackoff

func NewExponentialBackoff(maxElapsedTime time.Duration) ExponentialBackoff

NewExponentialBackoff creates a new ExponentialBackoff instance with the specified maximum elapsed time limit.

func (ExponentialBackoff) Do

func (e ExponentialBackoff) Do(ctx context.Context, operation func() error) error

Do executes the provided operation, retrying it on failure using exponential backoff. The function returns when the operation succeeds, the context is cancelled, or the maxElapsedTime limit is reached. Returns the last error if all attempts fail.

Jump to

Keyboard shortcuts

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