{"id":16683243,"url":"https://github.com/replaysmike/anyretry","last_synced_at":"2025-04-12T05:06:27.858Z","repository":{"id":65413761,"uuid":"163906272","full_name":"replaysMike/AnyRetry","owner":"replaysMike","description":"A simple CSharp library for wrapping asynchronous code with retry behavior.","archived":false,"fork":false,"pushed_at":"2021-09-08T20:40:31.000Z","size":119,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T15:58:03.956Z","etag":null,"topics":["async","asynchronous","backoff","backoff-strategy","retry"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/replaysMike.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":"2019-01-03T01:06:03.000Z","updated_at":"2021-09-08T20:40:34.000Z","dependencies_parsed_at":"2023-01-23T10:55:04.003Z","dependency_job_id":null,"html_url":"https://github.com/replaysMike/AnyRetry","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/replaysMike%2FAnyRetry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/replaysMike%2FAnyRetry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/replaysMike%2FAnyRetry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/replaysMike%2FAnyRetry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/replaysMike","download_url":"https://codeload.github.com/replaysMike/AnyRetry/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248519540,"owners_count":21117761,"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":["async","asynchronous","backoff","backoff-strategy","retry"],"created_at":"2024-10-12T14:23:53.990Z","updated_at":"2025-04-12T05:06:27.769Z","avatar_url":"https://github.com/replaysMike.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AnyRetry\n\n[![nuget](https://img.shields.io/nuget/v/AnyRetry.svg)](https://www.nuget.org/packages/AnyRetry/)\n[![nuget](https://img.shields.io/nuget/dt/AnyRetry.svg)](https://www.nuget.org/packages/AnyRetry/)\n[![Build status](https://ci.appveyor.com/api/projects/status/25qjrjyhxv8t3dm7?svg=true)](https://ci.appveyor.com/project/MichaelBrown/anyretry)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/c933a86542a844889cdd64df99328b09)](https://www.codacy.com/app/replaysMike/AnyRetry?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=replaysMike/AnyRetry\u0026amp;utm_campaign=Badge_Grade)\n[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/c933a86542a844889cdd64df99328b09)](https://www.codacy.com/app/replaysMike/AnyRetry?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=replaysMike/AnyRetry\u0026utm_campaign=Badge_Coverage)\n\nA simple CSharp library for retrying operations with backoff and async support.\n\n## Description\n\nAnyRetry allows you to retry operations that may fail such as database operations, network or anything which may throw an unexpected temporary exception. It supports auto-backoff using different available algorithms which is real handy for retrying network operations. Support is available for .Net Framework 4+ and .Net Standard 2.0+\n\n## Installation\nInstall AnyRetry from the Package Manager Console:\n```\nPM\u003e Install-Package AnyRetry\n```\n\n## Usage\n\nBasic usage would be to retry execution of a method if it throws an exception.\nIf any exception is thrown, it will retry up to 10 times every 5 seconds (configurable). If it exceeds the maximum number of retries, a `RetryTimeoutException` will be thrown.\n\n```csharp\nusing AnyRetry;\n\nvar maxRetries = 10;\nvar retryEvery = TimeSpan.FromSeconds(5);\nRetry.Do(() =\u003e\n{\n    DoMyNetworkOperation();\n}, retryEvery, maxRetries);\n\n```\n\nTo only retry if a specific known exception is thrown, but you want to abort if any other type of exception is thrown:\n\n```csharp\nvar maxRetries = 10;\nvar retryEvery = TimeSpan.FromSeconds(5);\nRetry.Do(() =\u003e\n{\n    DoMyNetworkOperation();\n}, retryEvery, maxRetries, typeof(SocketException), typeof(IOException));\n```\n\nTo use a backoff schedule (to increase your retry timeouts exponentially for example) simply specify the retry policy:\n```csharp\nvar maxRetries = 10;\nvar retryEvery = TimeSpan.FromSeconds(5);\nRetry.Do(() =\u003e\n{\n    DoMyNetworkOperation();\n}, retryEvery, maxRetries, RetryPolicy.ExponentialBackoff);\n```\n\n### Retry Policies\n\nThere are 3 types of policies available: `StaticDelay`, `ExponentialBackoff`, `EasedBackoff`. \n\n`StaticDelay` specifies that the retry time is always the same. `ExponentialBackoff` will multiply the `retryEvery` exponentially on every failure (a maximum can also be specified). `EasedBackoff` allows you to choose a standard easing algorithm, such as `ExponentialEaseOut` or `QuadraticEaseOut` for example.\n\nSpecifying policy options:\n```csharp\nvar maxRetries = 10;\nvar retryEvery = TimeSpan.FromSeconds(5);\nvar policyOptions = new RetryPolicyOptions { \n    EasingFunction = EasingFunction.QuadraticEaseOut,\n    MaxRetryInterval = TimeSpan.FromSeconds(30),\n    // optional: specify we want to ease over 5 retries, not the default of maxRetries=10\n    MaxRetrySteps = 5\n};\nRetry.Do(() =\u003e\n{\n    DoMyNetworkOperation();\n}, retryEvery, maxRetries, RetryPolicy.EasedBackoff, policyOptions);\n```\nIf you wish to specify easing over a period shorter than the maximum number of retries, use the `MaxRetrySteps` option and specify a value shorter than the maximum number of retries. When it hits this limit it will plateau and use the `MaxRetryInterval` for the remaining retries.\n\n### Asynchronous usage\n\nAsync usage is identical to the synchronous examples:\n\n```csharp\nvar maxRetries = 10;\nvar retryEvery = TimeSpan.FromSeconds(5);\nawait Retry.DoAsync(async () =\u003e\n{\n    await DoMyNetworkOperationAsync();\n}, retryEvery, maxRetries);\n```\n\n### Advanced tips\n\nIf you need access to the retry information, you can specify access as follows:\n\n```csharp\nvar maxRetries = 10;\nvar retryEvery = TimeSpan.FromSeconds(5);\n Retry.Do((retryIteration, maxRetryCount) =\u003e\n{\n    Console.WriteLine($\"Retry #: {retryIteration}\");\n    Console.WriteLine($\"MaxRetries #: {maxRetryCount}\");\n    DoMyNetworkOperation();\n}, retryEvery, maxRetries);\n```\n\nIf you want to run some code when a retry fails, use the onFailure handler:\n```csharp\nvar maxRetries = 10;\nvar retryEvery = TimeSpan.FromSeconds(5);\n Retry.Do(() =\u003e\n{\n    DoMyNetworkOperation();\n}, retryEvery, maxRetries, RetryPolicy.StaticDelay, RetryPolicyOptions.None, (retryIteration, maxRetryCount) =\u003e {\n  // add your custom error code here\n});\n```\n\nA rarely useful feature is being able to always retry if a condition is true, and if a condition is false it will retry up to the maximum number of retries. This can be useful if you want to always retry based on some condition like a status check, but if that condition isn't met to retry until failure.\n```csharp\nvar maxRetries = 1;\nvar retryEvery = TimeSpan.FromMilliseconds(100);\nvar i = 0;\nRetry.Do(() =\u003e\n{\n    i++;\n    DoMyNetworkOperation();\n}, retryEvery, \n    maxRetries, \n    RetryPolicy.StaticDelay, \n    RetryPolicyOptions.None, \n    (retryIteration, maxRetryCount) =\u003e {},\n    () =\u003e {\n        // this must return true otherwise it will retry forever!\n        // the result of this is it will retry until i is 10, no matter the maxRetries value.\n        return i != 10;\n    }\n);\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freplaysmike%2Fanyretry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freplaysmike%2Fanyretry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freplaysmike%2Fanyretry/lists"}