{"id":20062957,"url":"https://github.com/go-playground/backoff","last_synced_at":"2025-05-05T17:31:53.466Z","repository":{"id":57496736,"uuid":"115882041","full_name":"go-playground/backoff","owner":"go-playground","description":":bowtie: Backoff uses an exponential backoff algorithm to backoff between retries with optional auto-tuning functionality.","archived":false,"fork":false,"pushed_at":"2017-12-31T20:34:41.000Z","size":12,"stargazers_count":12,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-09T01:41:46.000Z","etag":null,"topics":["auto-tuning","backoff","exponential-backoff","retry"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/go-playground.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-31T19:37:34.000Z","updated_at":"2023-10-03T00:22:02.000Z","dependencies_parsed_at":"2022-09-03T02:10:32.999Z","dependency_job_id":null,"html_url":"https://github.com/go-playground/backoff","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Fbackoff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Fbackoff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Fbackoff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-playground%2Fbackoff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-playground","download_url":"https://codeload.github.com/go-playground/backoff/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252542262,"owners_count":21764934,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auto-tuning","backoff","exponential-backoff","retry"],"created_at":"2024-11-13T13:39:43.699Z","updated_at":"2025-05-05T17:31:53.171Z","avatar_url":"https://github.com/go-playground.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Backoff library\n=============\n![Project status](https://img.shields.io/badge/version-1.0.0-green.svg)\n[![Build Status](https://travis-ci.org/go-playground/backoff.svg?branch=master)](https://travis-ci.org/go-playground/backoff)\n[![Coverage Status](https://coveralls.io/repos/github/go-playground/backoff/badge.svg?branch=master)](https://coveralls.io/github/go-playground/backoff?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/backoff)](https://goreportcard.com/report/github.com/go-playground/backoff)\n[![GoDoc](https://godoc.org/github.com/go-playground/backoff?status.svg)](https://godoc.org/github.com/go-playground/backoff)\n![License](https://img.shields.io/dub/l/vibe-d.svg)\n\nBackoff library uses an exponential backoff algorithm to backoff between retries.\n\nWhat makes this different from other backoff libraries?\n1. Simple, by automatically calculating the exponential factor between the min and max backoff times; which properly tunes to your desired values.\n2. Provides an optional `AutoTune` function which will adjust the min backoff duration based upon successful backoff duration retries.\n\nWhy AutoTune?\n\nFor long running services that hit external services such as writing to a DB or that hit a 3rd party API's, where successful attempts backoff durations can vary over time as load changes. Additionally by using `AutoTune` it should provide an automatic jitter when multiple copies of a service are running.\n\nExample Basic\n------------\n```go\npackage main\n\nimport (\n\t\"errors\"\n\t\"time\"\n\n\t\"fmt\"\n\n\t\"github.com/go-playground/backoff\"\n)\n\nfunc main() {\n        // backoff.New(retries, min, max)\n\tbo := backoff.New(5, time.Second, time.Minute)\n\tdefer bo.Close()\n\n\t// retry function and notification function to log failures etc...\n\tbo.Run(func() (bail bool, err error) {\n\t\t// do something\n\t\treturn false, errors.New(\"ERR\")\n\t}, func(attempt uint16, waiting time.Duration, err error) {\n\t\tfmt.Printf(\"Attempt:%d Waiting:%s Error:%s\\n\", attempt, waiting, err)\n\n\t})\n}\n```\n\nExample AutoTune\n------------\n```go\npackage main\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"time\"\n\n\t\"os\"\n\n\t\"os/signal\"\n\t\"syscall\"\n\n\t\"math/rand\"\n\n\t\"github.com/go-playground/backoff\"\n)\n\nfunc main() {\n        // backoff.New(retries, min, max)\n\tbo := backoff.New(5, time.Second, 10*time.Second)\n\t// bo.AutoTune(poll, reset)\n\tbo.AutoTune(30*time.Second, 2*time.Minute)\n\tdefer bo.Close()\n\n\tgo func() {\n\t\tfor {\n\t\t\ttime.Sleep(time.Millisecond * 500)\n\t\t\tgo func() {\n\t\t\t\tbo.Run(func() (bool, error) {\n\t\t\t\t\t// simulating random success/failure\n\t\t\t\t\tcount := rand.Intn(5)\n\t\t\t\t\tswitch count {\n\t\t\t\t\tcase 1:\n\t\t\t\t\t\treturn false, errors.New(\"ERR\")\n\t\t\t\t\tcase 2:\n\t\t\t\t\t\treturn false, errors.New(\"ERR\")\n\t\t\t\t\tcase 3:\n\t\t\t\t\t\treturn false, nil\n\t\t\t\t\tcase 4:\n\t\t\t\t\t\treturn false, errors.New(\"ERR\")\n\t\t\t\t\tdefault: //case 5:\n\t\t\t\t\t\treturn false, nil\n\t\t\t\t\t}\n\t\t\t\t}, func(attempt uint16, waiting time.Duration, err error) {\n\t\t\t\t\tfmt.Printf(\"Attempt:%d Waiting: %s Error:%s\\n\", attempt, waiting, err)\n\t\t\t\t})\n\t\t\t}()\n\t\t}\n\t}()\n\ts := make(chan os.Signal, 1)\n\tsignal.Notify(s, syscall.SIGINT, syscall.SIGTERM)\n\t\u003c-s\n}\n```\n\nRequirements\n------------\n\\\u003e= Go v1.9\n\nPackage Versioning\n---------------\nI'm jumping on the vendoring bandwagon, you should vendor this package as I will not\nbe creating different version with gopkg.in like allot of my other libraries.\n\nWhy? because my time is spread pretty thin maintaining all of the libraries I have + LIFE,\nit is so freeing not to worry about it and will help me keep pouring out bigger and better\nthings for you the community.\n\nLicense\n------\nDistributed under MIT License, please see license file in code for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-playground%2Fbackoff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-playground%2Fbackoff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-playground%2Fbackoff/lists"}