Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Flexible and configurable retry mechanism for Go.

License

NotificationsYou must be signed in to change notification settings

itpey/retry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flexible and configurable retry mechanism for Go. With RETRY, you can easily implement retry logic with customizable backoff strategies to enhance the resilience of your applications.

Go Referencelicense

Features

  • Configurable Retries: Set the maximum number of retries to control how many times an operation is attempted.
  • Backoff Strategies: Define initial and maximum backoff durations with optional jitter to avoid synchronized retries.
  • Jitter Support: Add randomness to backoff durations to prevent thundering herd problems.
  • Context Support: Utilize Go contexts for cancellation and timeouts, ensuring operations can be gracefully terminated.
  • Thread-Safe: Concurrently modify retry configurations safely across multiple goroutines.

Installation

Make sure you have Go installed and configured on your system. Use go install to installRETRY:

go get -u github.com/itpey/retry

Usage

Simple Retry

package mainimport ("context""errors""fmt""github.com/itpey/retry")funcmain() {r:=retry.New(retry.Config{MaxAttemptTimes:10,})fn:=func()error {returnerrors.New("temporary error")}ctx:=context.Background()iferr:=r.Do(ctx,fn);err!=nil {ifretry.IsMaxRetriesError(err) {fmt.Println("Failed after maximum number of retries")}else {fmt.Printf("Failed with error: %v\n",err)}}else {fmt.Println("Operation succeeded")}}

Retry with Backoff

package mainimport ("context""errors""fmt""time""github.com/itpey/retry")funcmain() {r:=retry.New(retry.Config{MaxAttemptTimes:5,InitialBackoff:100*time.Millisecond,MaxBackoff:1*time.Second,MaxJitter:50*time.Millisecond,})fn:=func()error {returnerrors.New("temporary error")}ctx:=context.Background()iferr:=r.Do(ctx,fn);err!=nil {ifretry.IsMaxRetriesError(err) {fmt.Println("Failed after maximum number of retries")}else {fmt.Printf("Failed with error: %v\n",err)}}else {fmt.Println("Operation succeeded")}}

Feedback and Contributions

If you encounter any issues or have suggestions for improvement, pleaseopen an issue on GitHub.

We welcome contributions! Fork the repository, make your changes, and submit a pull request.

License

RETRY is open-source software released under the Apache License, Version 2.0. You can find a copy of the license in theLICENSE file.

Author

RETRY was created byitpey


[8]ページ先頭

©2009-2025 Movatter.jp