httpretry

package module
v0.0.0-...-c8147b2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README ยถ

httpretry

httpretry is a robust and configurable HTTP client for Go that enhances the standard http.Client by providing automatic retries for requests failing due to timeouts or other transient errors. It allows developers to retain their existing HTTP client logic while adding powerful retry mechanisms with customizable backoff policies.

Features

  • ๐Ÿ›  Enhances existing HTTP clients with retry capabilities
  • ๐Ÿ”„ Automatic retries for timeout-related errors and transient HTTP failures
  • โณ Customizable timeouts and connection settings
  • ๐Ÿ“œ Configurable backoff strategies (exponential, linear, or custom)
  • ๐Ÿ” Debug mode for logging retry attempts
  • ๐Ÿ›  Seamless integration with both environment-based and programmatic configurations

Installation

To install httpretry, run:

go get github.com/dings-things/httpretry

Quickstart

Hereโ€™s how to use httpretry without modifying existing HTTP logic:

package main

import (
	"fmt"
	"net/http"
	"github.com/dings-things/httpretry"
)

func main() {
	// Create retry-enabled settings
	settings := httpretry.NewHTTPSettings(
		httpretry.WithMaxRetry(3),
		httpretry.WithDebugMode(true),
		httpretry.WithRequestTimeout(5 * time.Second),
	)

	// Create an HTTP client with retry support
	client := httpretry.NewClient(settings)

	// Making a request that may fail due to timeouts or transient errors
	resp, err := client.Get("https://httpbin.org/status/500")

	if err != nil {
		fmt.Println("Request failed after retries:", err)
	} else {
		fmt.Println("Response received with status:", resp.StatusCode)
	}
}

Why httpretry?

Traditional HTTP clients immediately fail on timeouts or transient errors.
With httpretry, you can extend your existing client logic to automatically retry failed requests caused by:

โœ… Network timeouts
โœ… Gateway or service unavailable errors (5xx responses)
โœ… Temporary connection failures

It ensures that your application remains resilient and responsive even in unreliable network conditions.


Usage

Creating a Custom HTTP Client with Retry Support
settings := httpretry.NewHTTPSettings(
    httpretry.WithMaxRetry(5),
    httpretry.WithRequestTimeout(10 * time.Second), // Handles timeout-based retries
    httpretry.WithBackoffPolicy(func(attempt int) time.Duration {
        return time.Duration(attempt) * time.Second
    }),
)

client := httpretry.NewClient(settings)
Making HTTP Requests with Automatic Retries

Instead of modifying your HTTP logic, just use the client as you normally would:

req, _ := http.NewRequest("GET", "https://httpbin.org/status/502", nil)
resp, err := client.Do(req)

if err != nil {
    fmt.Println("Request failed after retries:", err)
} else {
    fmt.Println("Response received:", resp.StatusCode)
}
Handling Requests That May Timeout

If you have long-running requests, you can use timeouts with retries:

settings := httpretry.NewHTTPSettings(
    httpretry.WithMaxRetry(3),
    httpretry.WithRequestTimeout(5 * time.Second), // Retries only when timeout occurs
)

client := httpretry.NewClient(settings)

Now, requests that time out will automatically retry instead of failing instantly.


Configuring via Environment Variables

You can configure httpretry using environment variables:

export MAX_REQUEST_RETRY=5
export DEBUG_MODE=true
export REQUEST_TIMEOUT=5s

Then initialize settings using:

settings := httpretry.NewSettings()
client := httpretry.NewClient(settings)

Advanced Configuration

Set Maximum Retries
settings := httpretry.NewHTTPSettings(
    httpretry.WithMaxRetry(5),
)
Enable Debug Mode
settings := httpretry.NewHTTPSettings(
    httpretry.WithDebugMode(true),
)
Customize Backoff Policy
settings := httpretry.NewHTTPSettings(
    httpretry.WithBackoffPolicy(func(attempt int) time.Duration {
        return time.Duration(attempt*2) * time.Second
    }),
)
Control Timeout Handling
settings := httpretry.NewHTTPSettings(
    httpretry.WithRequestTimeout(5 * time.Second),
)

License

httpretry is open-source and available under the MIT License.

Documentation ยถ

Index ยถ

Constants ยถ

This section is empty.

Variables ยถ

This section is empty.

Functions ยถ

func NewClient ยถ

func NewClient(settings *Settings, retryStatusCodes ...int) *http.Client

NewClient HTTP ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žฌ์‹œ๋„ ์„ค์ •์„ ์ ์šฉ

Types ยถ

type HTTPOption ยถ

type HTTPOption func(*Settings)

HTTPOption http ์„ค์ •

func WithBackoffPolicy ยถ

func WithBackoffPolicy(policy func(attempt int) time.Duration) HTTPOption

WithBackoffPolicy ์š”์ฒญ ์‹คํŒจ ์‹œ, backoff ์ •์ฑ…์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

๊ธฐ๋ณธ์œผ๋กœ ์ง€์ˆ˜ ๋ฐฑ์˜คํ”„๊ฐ€ ์ ์šฉ๋จ

Parameters:

  • policy: (func(attempt int) time.Duration) ๋ฐฑ์˜คํ”„ ์ •์ฑ…

func WithDebugMode ยถ

func WithDebugMode(isDebug bool) HTTPOption

WithDebugMode ๋””๋ฒ„๊ทธ ๋ชจ๋“œ ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

๋””๋ฒ„๊ทธ ๋ชจ๋“œ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ ํ•ฉ๋‹ˆ๋‹ค. ๋””๋ฒ„๊ทธ ๋ชจ๋“œ ์ „ํ™˜ ์‹œ, stdout์œผ๋กœ ์žฌ์‹œ๋„ ์‚ฌ์œ ๋ฅผ ํ”„๋ฆฐํŠธํ•ฉ๋‹ˆ๋‹ค.

Parameters:

  • isDebug: (bool) ๋””๋ฒ„๊น… ๋ชจ๋“œ ์—ฌ๋ถ€

func WithExpectContinueTimeout ยถ

func WithExpectContinueTimeout(timeout time.Duration) HTTPOption

WithExpectContinueTimeout ExpectContinueTimeout ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ์ „์— ์„œ๋ฒ„์˜ ์Šน์ธ์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์‹œ๊ฐ„

์ฃผ์˜: 1์ดˆ ์ดํ•˜์˜ ๋‹จ์œ„๋กœ timeout์„ ๊ฑฐ๋Š” ๊ฒƒ์€ ์œ„ํ—˜ํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ฒ„๊ฐ€ ์Šน์ธ์„ ๋ณด๋‚ด์ง€ ์•Š์„ ๊ฒฝ์šฐ, ํด๋ผ์ด์–ธํŠธ๋Š” ์š”์ฒญ์„ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.

Parameters:

  • timeout: (time.Duration) ์Šน์ธ ๋Œ€๊ธฐ ์‹œ๊ฐ„

func WithIdleConnTimeout ยถ

func WithIdleConnTimeout(timeout time.Duration) HTTPOption

WithIdleConnTimeout IdleConnTimeout ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์œ ํœด ์ƒํƒœ์ธ TCP ์—ฐ๊ฒฐ์„ ์–ผ๋งˆ๋‚˜ ์œ ์ง€ํ•  ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

Parameters:

  • timeout: (time.Duration) TCP ์—ฐ๊ฒฐ ์œ ์ง€ ์‹œ๊ฐ„

func WithInsecure ยถ

func WithInsecure(insecure bool) HTTPOption

WithInsecure SSL/TLS ์ธ์ฆ์„œ ์œ ํšจ์„ฑ ๊ฒ€์ฆ ์—ฌ๋ถ€ ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

์ธ์ฆ์„œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์‹ค์‹œ ํ•  ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. true ์‹œ, ์ธ์ฆ์„œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Parameters:

  • isSecure: (bool) SSL/TLS ์ธ์ฆ์„œ ์œ ํšจ์„ฑ ๊ฒ€์ฆ ์—ฌ๋ถ€

func WithMaxIdleConns ยถ

func WithMaxIdleConns(maxIdleConns int) HTTPOption

WithMaxIdleConns MaxIdleConns ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์œ ํœด(Idle) ์—ฐ๊ฒฐ์˜ ์ˆ˜๋ฅผ ์ง€์ •

Parameters:

  • maxIdleConns: (int) TCP ์—ฐ๊ฒฐ์˜ ์ตœ๋Œ€ ์ˆ˜

func WithMaxRetry ยถ

func WithMaxRetry(maxRetry int) HTTPOption

WithMaxRetry MaxRetry ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

์ตœ๋Œ€๋กœ ์žฌ์‹œ๋„ํ•˜๋Š” ํšŸ์ˆ˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์‹คํŒจ ์‹œ ์ง€์ •๋œ ํšŸ์ˆ˜ +1ํšŒ ๋งŒํผ ์žฌ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.

Parameters:

  • maxRetry: (int) ์žฌ์‹œ๋„ ํ•˜๊ณ ์ž ํ•˜๋Š” ํšŸ์ˆ˜

func WithRequestTimeout ยถ

func WithRequestTimeout(timeout time.Duration) HTTPOption

WithRequestTimeout RequestTimeout ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

์ „์ฒด HTTP์š”์ฒญ์˜ ์ตœ๋Œ€ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ง€์ •. Timeout ๋ฐœ์ƒ ์‹œ, ์ „์ฒด ์š”์ฒญ์„ ์ทจ์†Œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์žฌ์‹œ๋„ ํ•˜์ง€ ์•Š์Œ ์ฃผ์˜

์ฃผ์˜: 1์ดˆ ์ดํ•˜์˜ ๋‹จ์œ„๋กœ timeout์„ ๊ฑฐ๋Š” ๊ฒƒ์€ ์œ„ํ—˜ํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ฒ„๊ฐ€ ์‘๋‹ต ํ—ค๋”๋ฅผ ๋ณด๋‚ด์ง€ ์•Š์„ ๊ฒฝ์šฐ, ํด๋ผ์ด์–ธํŠธ๋Š” ์š”์ฒญ์„ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.

REFS ยถ

Parameters:

  • timeout: (time.Duration) ์ „์ฒด ์š”์ฒญ ์ตœ๋Œ€ ์‹คํ–‰ ์‹œ๊ฐ„

func WithResponseHeaderTimeout ยถ

func WithResponseHeaderTimeout(timeout time.Duration) HTTPOption

WithResponseHeaderTimeout ResponseHeaderTimeout ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‘๋‹ต ํ—ค๋”๋ฅผ ๋ฐ›๋Š” ๋ฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์ตœ๋Œ€ ์‹œ๊ฐ„์„ ์ง€์ •

์ฃผ์˜: 1์ดˆ ์ดํ•˜์˜ ๋‹จ์œ„๋กœ timeout์„ ๊ฑฐ๋Š” ๊ฒƒ์€ ์œ„ํ—˜ํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ฒ„๊ฐ€ ์‘๋‹ต ํ—ค๋”๋ฅผ ๋ณด๋‚ด์ง€ ์•Š์„ ๊ฒฝ์šฐ, ํด๋ผ์ด์–ธํŠธ๋Š” ์š”์ฒญ์„ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.

Parameters:

  • timeout: (time.Duration) ์‘๋‹ต ํ—ค๋” ์ˆ˜์‹  ์‹œ๊ฐ„

func WithTLSHandshakeTimeout ยถ

func WithTLSHandshakeTimeout(timeout time.Duration) HTTPOption

WithTLSHandshakeTimeout TLSHandshakeTimeout ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” Option

์ตœ์ดˆ SSL/TLS ์ธ์ฆ์„œ ๊ฒ€์ฆ์ดํ›„, TLS ํ•ธ๋“œ์…ฐ์ดํฌ๊ฐ€ ์œ ์ง€๋˜๋Š” ์‹œ๊ฐ„์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

Parameters:

  • timeout: (time.Duration) TLS ํ•ธ๋“œ์…ฐ์ดํฌ ์œ ์ง€ ์‹œ๊ฐ„

type Settings ยถ

type Settings struct {
	MaxRetry              int           `env:"MAX_REQUEST_RETRY,default=3"`
	DebugMode             bool          `env:"DEBUG_MODE,default=false"`
	Insecure              bool          `env:"INSECURE,default=false"`
	MaxIdleConns          int           `env:"MAX_IDLE_CONNECTIONS,default=15"`
	IdleConnTimeout       time.Duration `env:"CONNECTION_TIMEOUT,default=90s"`
	TLSHandshakeTimeout   time.Duration `env:"TLS_TIMEOUT,default=10s"`
	ExpectContinueTimeout time.Duration `env:"CONTINUE_TIMEOUT,defualt=1s"`
	ResponseHeaderTimeout time.Duration `env:"HEADER_TIMEOUT,default=10s"`
	RequestTimeout        time.Duration `env:"REQUEST_TIMEOUT,default=10s"`
	BackoffPolicy         func(attempt int) time.Duration
}

Settings of http retry

func NewHTTPSettings ยถ

func NewHTTPSettings(opts ...HTTPOption) *Settings

NewHTTPSettings httpOption๋“ค์„ ์ธ์ž๋กœ ์„ค์ • struct์„ ์ƒ์„ฑ

fx ๋˜๋Š” env๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , HTTP Retry Client๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ, ์˜ต์…˜ ์„ธํŒ…์„ ์œ„ํ•œ ์ƒ์„ฑ์ž

Parameters:

  • opts: (...HTTPOption) ๊ธฐ๋ณธ ์„ธํŒ… ๊ฐ’์—์„œ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ์ถ”๊ฐ€๋˜๋Š” ์˜ต์…˜ ๊ฐ’

func NewSettings ยถ

func NewSettings() *Settings

NewSettings constructor

Directories ยถ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ ๆข็ฝ‘ๅ€