{"id":19990752,"url":"https://github.com/David-Desmaisons/RateLimiter","last_synced_at":"2025-05-04T10:30:42.257Z","repository":{"id":45705724,"uuid":"59446689","full_name":"David-Desmaisons/RateLimiter","owner":"David-Desmaisons","description":"C# rate limiting utility","archived":false,"fork":false,"pushed_at":"2022-12-08T11:34:44.000Z","size":639,"stargazers_count":288,"open_issues_count":17,"forks_count":34,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-05-02T05:04:13.636Z","etag":null,"topics":["asynchronous","client","csharp","rate-limiting"],"latest_commit_sha":null,"homepage":"http://david-desmaisons.github.io/RateLimiter/","language":"C#","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/David-Desmaisons.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":"2016-05-23T02:34:40.000Z","updated_at":"2025-04-24T16:39:24.000Z","dependencies_parsed_at":"2023-01-25T12:45:48.265Z","dependency_job_id":null,"html_url":"https://github.com/David-Desmaisons/RateLimiter","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/David-Desmaisons%2FRateLimiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/David-Desmaisons%2FRateLimiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/David-Desmaisons%2FRateLimiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/David-Desmaisons%2FRateLimiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/David-Desmaisons","download_url":"https://codeload.github.com/David-Desmaisons/RateLimiter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252253086,"owners_count":21718768,"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":["asynchronous","client","csharp","rate-limiting"],"created_at":"2024-11-13T04:51:27.414Z","updated_at":"2025-05-04T10:30:41.748Z","avatar_url":"https://github.com/David-Desmaisons.png","language":"C#","funding_links":[],"categories":["C\\#"],"sub_categories":[],"readme":"# RateLimiter\n\n[![build](https://img.shields.io/appveyor/ci/David-Desmaisons/RateLimiter.svg)](https://ci.appveyor.com/project/David-Desmaisons/RateLimiter)\n[![codecov](https://codecov.io/gh/David-Desmaisons/RateLimiter/branch/master/graph/badge.svg)](https://codecov.io/gh/David-Desmaisons/RateLimiter)\n[![NuGet Badge](https://buildstats.info/nuget/RateLimiter)](https://www.nuget.org/packages/RateLimiter/)\n[![MIT License](https://img.shields.io/github/license/David-Desmaisons/RateLimiter.svg)](https://github.com/David-Desmaisons/RateLimiter/blob/master/LICENSE)\n\nC# client-side rate limiting utility.\n\nhttp://david-desmaisons.github.io/RateLimiter/\n\n## Motivation\nThe initial motivation was to create helper to respect Web Services rate limit in client application.\nHowever this helper can also be also in other scenarios where you need to temporally limit the usage of one shared resource.\n\n## Features\n* Easy to use\n* Fully asynchronous: lower resource usage than thread sleep\n* Cancellable via CancellationToken\n* Thread safe so you can share time constraints object to rate limit different threads using the same resource\n* Composable: ability to compose different rate limits in one constraint\n\n## Installation\n```bash\nInstall-Package RateLimiter -Version 2.2.0\n```\n\n## Sample usage\n\n### Basic\n\nRateLimiters are awaitable: the code executed after the await will respect the time constraint:\n\n\n```C#\n    using ComposableAsync;\n\n    // Create Time constraint: max five times by second\n    var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));\n\n    // Use it\n    for(int i=0; i\u003c1000; i++)\n    {\n        await timeConstraint;\n        Trace.WriteLine(string.Format(\"{0:MM/dd/yyy HH:mm:ss.fff}\", DateTime.Now));\n    }\n```\n\nOutput\n```\n05/23/2016 00:14:44.791\n05/23/2016 00:14:44.958\n05/23/2016 00:14:44.959\n05/23/2016 00:14:44.959\n05/23/2016 00:14:44.960\n05/23/2016 00:14:45.959\n05/23/2016 00:14:45.960\n05/23/2016 00:14:45.961\n05/23/2016 00:14:45.961\n05/23/2016 00:14:45.962\n05/23/2016 00:14:46.973\n...\n```\n\n### As http DelegatingHandler\n\n```C#\n    using System.Net.Http;\n\n    //...\n    var handler = TimeLimiter\n            .GetFromMaxCountByInterval(60, TimeSpan.FromMinutes(1))\n            .AsDelegatingHandler();\n    var Client = new HttpClient(handler)\n```\n\n### With cancellation token:\n\n```C#\n    // Create Time constraint: max three times by second\n    var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(3, TimeSpan.FromSeconds(1));\n    var cancellationSource = new CancellationTokenSource(1100);\n\n    // Use it\n    while(true)\n    {\n        await timeConstraint.Enqueue(ConsoleIt, cancellationSource.Token);\n    }\n    \n    //....\n    private static void ConsoleIt()\n    {\n        Trace.WriteLine(string.Format(\"{0:MM/dd/yyy HH:mm:ss.fff}\", DateTime.Now));\n    }\n\n```\n\nOutput\n```\n07/07/2019 18:09:35.645\n07/07/2019 18:09:35.648\n07/07/2019 18:09:35.648\n07/07/2019 18:09:36.649\n07/07/2019 18:09:36.650\n07/07/2019 18:09:36.650\n```\n\n\n### Composed\n\n```C#\n    // Create first constraint: max five times by second\n    var constraint = new CountByIntervalAwaitableConstraint(5, TimeSpan.FromSeconds(1));\n    \n    / /Create second constraint: one time each 100 ms\n    var constraint2 = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(100));\n    \n    // Compose the two constraints\n    var timeConstraint = TimeLimiter.Compose(constraint, constraint2);\n\n    // Use it\n    for(int i=0; i\u003c1000; i++)\n    {\n        await timeConstraint;\n        Trace.WriteLine(string.Format(\"{0:MM/dd/yyy HH:mm:ss.fff}\", DateTime.Now));\n    }       \n```\n\nOutput\n```\n05/21/2016 23:52:48.573\n05/21/2016 23:52:48.682\n05/21/2016 23:52:48.809\n05/21/2016 23:52:48.922\n05/21/2016 23:52:49.024\n05/21/2016 23:52:49.575\n05/21/2016 23:52:49.685\n05/21/2016 23:52:49.810\n05/21/2016 23:52:49.942\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDavid-Desmaisons%2FRateLimiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDavid-Desmaisons%2FRateLimiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDavid-Desmaisons%2FRateLimiter/lists"}