{"id":13764056,"url":"https://github.com/ssgreg/repeat","last_synced_at":"2026-01-17T17:53:05.256Z","repository":{"id":57488528,"uuid":"111648609","full_name":"ssgreg/repeat","owner":"ssgreg","description":"Go implementation of different backoff strategies useful for retrying operations and heartbeating.","archived":false,"fork":false,"pushed_at":"2020-07-24T08:18:11.000Z","size":37,"stargazers_count":85,"open_issues_count":0,"forks_count":7,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-16T23:32:29.760Z","etag":null,"topics":["backoff","exponential-backoff","go","golang","heartbeat","repeat","retry"],"latest_commit_sha":null,"homepage":"","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/ssgreg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-11-22T07:06:47.000Z","updated_at":"2024-11-06T11:54:34.000Z","dependencies_parsed_at":"2022-08-29T15:01:52.047Z","dependency_job_id":null,"html_url":"https://github.com/ssgreg/repeat","commit_stats":null,"previous_names":["ssgreg/backoff"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssgreg%2Frepeat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssgreg%2Frepeat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssgreg%2Frepeat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssgreg%2Frepeat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ssgreg","download_url":"https://codeload.github.com/ssgreg/repeat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253453292,"owners_count":21911070,"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":["backoff","exponential-backoff","go","golang","heartbeat","repeat","retry"],"created_at":"2024-08-03T15:01:11.335Z","updated_at":"2026-01-17T17:53:05.195Z","avatar_url":"https://github.com/ssgreg.png","language":"Go","readme":"# Repeat\n\n![Alt text](https://user-images.githubusercontent.com/1574981/33310621-48c78416-d433-11e7-9a80-36d2381901d0.png \"repeat\")\n[![GoDoc](https://godoc.org/github.com/ssgreg/repeat?status.svg)](https://godoc.org/github.com/ssgreg/repeat)\n[![Build Status](https://travis-ci.org/ssgreg/repeat.svg?branch=master)](https://travis-ci.org/ssgreg/repeat)\n[![Go Report Status](https://goreportcard.com/badge/github.com/ssgreg/repeat)](https://goreportcard.com/report/github.com/ssgreg/repeat)\n[![GoCover](https://gocover.io/_badge/github.com/ssgreg/repeat)](https://gocover.io/github.com/ssgreg/repeat)\n\nGo implementation of different backoff strategies useful for retrying operations and heartbeating.\n\n## Examples\n\n### Backoff\n\nLet's imagine that we need to do a REST call on remote server but it could fail with a bunch of different issues. We can repeat failed operation using exponential backoff policies.\n\n\u003e *Exponential backoff* is an algorithm that uses feedback to multiplicatively decrease the rate of some process, in order to gradually find an acceptable rate.\n\nThe example below tries to repeat operation 10 times using a full jitter backoff. [See algorithm details here.](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/)\n\n```go\n    // An example operation that do some useful stuff.\n    // It fails five first times.\n    var last time.Time\n    op := func(c int) error {\n        printInfo(c, \u0026last)\n        if c \u003c 5 {\n            return repeat.HintTemporary(errors.New(\"can't connect to a server\"))\n        }\n        return nil\n    }\n\n    // Repeat op on any error, with 10 retries, with a backoff.\n    err := repeat.Repeat(\n        // Our op with additional call counter.\n        repeat.FnWithCounter(op),\n        // Force the repetition to stop in case the previous operation\n        // returns nil.\n        repeat.StopOnSuccess(),\n        // 10 retries max.\n        repeat.LimitMaxTries(10),\n        // Specify a delay that uses a backoff.\n        repeat.WithDelay(\n            repeat.FullJitterBackoff(500*time.Millisecond).Set(),\n        ),\n    )\n```\n\nThe example of output:\n\n```\nAttempt #0, Delay 0s\nAttempt #1, Delay 373.617912ms\nAttempt #2, Delay 668.004225ms\nAttempt #3, Delay 1.220076558s\nAttempt #4, Delay 2.716156336s\nAttempt #5, Delay 6.458431017s\nRepetition process is finished with: \u003cnil\u003e\n```\n\n### Backoff with timeout\n\nThe example below is almost the same as the previous one. It adds one important feature - possibility to cancel operation repetition using context's timeout.\n\n```go\n    // A context with cancel.\n    // Repetition will be cancelled in 3 seconds.\n    ctx, cancelFunc := context.WithCancel(context.Background())\n    go func() {\n        time.Sleep(3 * time.Second)\n        cancelFunc()\n    }()\n\n    // Repeat op on any error, with 10 retries, with a backoff.\n    err := repeat.Repeat(\n        ...\n        // Specify a delay that uses a backoff.\n        repeat.WithDelay(\n            repeat.FullJitterBackoff(500*time.Millisecond).Set(),\n            repeat.SetContext(ctx),\n        ),\n        ...\n    )\n```\n\nThe example of output:\n\n```\nAttempt #0, Delay 0s\nAttempt #1, Delay 358.728046ms\nAttempt #2, Delay 845.361787ms\nAttempt #3, Delay 61.527485ms\nRepetition process is finished with: context canceled\n```\n\n### Heartbeating\n\nLet's imagine we need to periodically report execution progress to remote server. The example below repeats the operation each second until it will be cancelled using passed context.\n\n```go\n    // An example operation that do heartbeat.\n    var last time.Time\n    op := func(c int) error {\n        printInfo(c, \u0026last)\n        return nil\n    }\n\n    // A context with cancel.\n    // Repetition will be cancelled in 7 seconds.\n    ctx, cancelFunc := context.WithCancel(context.Background())\n    go func() {\n        time.Sleep(7 * time.Second)\n        cancelFunc()\n    }()\n\n    err := repeat.Repeat(\n        // Heartbeating op.\n        repeat.FnWithCounter(op),\n        // Delay with fixed backoff and context.\n        repeat.WithDelay(\n            repeat.FixedBackoff(time.Second).Set(),\n            repeat.SetContext(ctx),\n        ),\n    )\n```\n\nThe example of output:\n\n```\nAttempt #0, Delay 0s\nAttempt #1, Delay 1.001129426s\nAttempt #2, Delay 1.000155727s\nAttempt #3, Delay 1.001131014s\nAttempt #4, Delay 1.000500428s\nAttempt #5, Delay 1.0008985s\nAttempt #6, Delay 1.000417057s\nRepetition process is finished with: context canceled\n```\n\n### Heartbeating with error timeout\n\nThe example below is almost the same as the previous one but it will be cancelled using special error timeout. This timeout resets each time the operations return nil.\n\n```go\n    // An example operation that do heartbeat.\n    // It fails 5 times after 3 successfull tries.\n    var last time.Time\n    op := func(c int) error {\n        printInfo(c, \u0026last)\n        if c \u003e 3 \u0026\u0026 c \u003c 8 {\n            return repeat.HintTemporary(errors.New(\"can't connect to a server\"))\n        }\n        return nil\n    }\n\n    err := repeat.Repeat(\n        // Heartbeating op.\n        repeat.FnWithCounter(op),\n        // Delay with fixed backoff and error timeout.\n        repeat.WithDelay(\n            repeat.FixedBackoff(time.Second).Set(),\n            repeat.SetErrorsTimeout(3*time.Second),\n        ),\n    )\n```\n\nThe example of output:\n\n```\nAttempt #0, Delay 0s\nAttempt #1, Delay 1.001634616s\nAttempt #2, Delay 1.004912408s\nAttempt #3, Delay 1.001021358s\nAttempt #4, Delay 1.001249459s\nAttempt #5, Delay 1.004320833s\nRepetition process is finished with: can't connect to a server\n```\n","funding_links":[],"categories":["工具库","Utilities","公用事业公司","工具库`可以提升效率的通用代码库和工具`","实用工具","Go","Utility"],"sub_categories":["交流","HTTP Clients","Utility/Miscellaneous","实用程序/Miscellaneous","查询语","Advanced Console UIs","Fail injection"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssgreg%2Frepeat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fssgreg%2Frepeat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssgreg%2Frepeat/lists"}