{"id":19321287,"url":"https://github.com/leshow/retry_fn","last_synced_at":"2025-04-22T19:30:46.793Z","repository":{"id":57660781,"uuid":"312686724","full_name":"leshow/retry_fn","owner":"leshow","description":"retry executing a closure or future on a timer","archived":false,"fork":false,"pushed_at":"2024-09-12T21:56:51.000Z","size":61,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-02T02:46:25.876Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","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/leshow.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-13T21:22:14.000Z","updated_at":"2024-09-12T21:56:54.000Z","dependencies_parsed_at":"2024-11-10T01:36:33.680Z","dependency_job_id":null,"html_url":"https://github.com/leshow/retry_fn","commit_stats":null,"previous_names":["leshow/retry"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leshow%2Fretry_fn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leshow%2Fretry_fn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leshow%2Fretry_fn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leshow%2Fretry_fn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leshow","download_url":"https://codeload.github.com/leshow/retry_fn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250308199,"owners_count":21409223,"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":[],"created_at":"2024-11-10T01:36:28.140Z","updated_at":"2025-04-22T19:30:46.316Z","avatar_url":"https://github.com/leshow.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# retry_fn\n\n[![Build Status](https://github.com/leshow/retry_fn/workflows/Actions/badge.svg)](https://github.com/leshow/retry_fn/actions)\n[![Crate](https://img.shields.io/crates/v/retry_fn.svg)](https://crates.io/crates/retry_fn)\n[![API](https://docs.rs/retry_fn/badge.svg)](https://docs.rs/retry_fn)\n\nFunction for executing retry either as a closure with a std-based sleep (`thread::sleep`) or\nusing either of the most popular async runtime's. Tokio or async-std. Inspired by the other\nretry libraries out there, the desire is to keep this up-to-date and combine features from several.\n\n## Sync Example\n\n```rust\nuse std::{io, time::Duration};\nuse retry_fn::{retry, strategy::ExponentialBackoff, RetryResult};\n\nfn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let mut count = 0;\n    let res = retry(ExponentialBackoff::new(Duration::from_secs(2)), |op| {\n       if op.retries \u003e= 3 {\n           RetryResult::\u003c\u0026str, _\u003e::Err(io::Error::new(\n               io::ErrorKind::TimedOut,\n               \"timed out\",\n           ))\n       } else {\n           count += 1;\n           RetryResult::Retry()\n       }\n    });\n    assert_eq!(count, 3);\n    assert!(res.is_err());\n    Ok(())\n}\n```\n\n## Using tokio\n\nEnable the `tokio-runtime` feature to get access to this function\n\n```rust\nuse std::{io, sync::{Arc, Mutex}};\nuse retry_fn::{tokio::retry, strategy::Constant, RetryResult};\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let count: Arc\u003cMutex\u003ci32\u003e\u003e = Arc::new(Mutex::new(0));\n    let res = retry(Constant::from_millis(100), |op| {\n        let count = count.clone();\n        async move {\n            if op.retries \u003e= 3 {\n                RetryResult::\u003c\u0026str, _\u003e::Err(io::Error::new(\n                    io::ErrorKind::TimedOut,\n                    \"timed out\",\n                ))\n            } else {\n                *count.lock().unwrap() += 1;\n                RetryResult::Retry()\n            }\n        }\n    })\n    .await;\n    assert_eq!(*count.lock().unwrap(), 3);\n    assert!(res.is_err());\n    Ok(())\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleshow%2Fretry_fn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleshow%2Fretry_fn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleshow%2Fretry_fn/lists"}