简体   繁体   English

在 C# 中限制客户端 api 的最佳方法

[英]Best way to rate limit clientside api in C#

I've ran into an issue which i'm struggling to decide the best way to solve.我遇到了一个问题,我正在努力决定解决的最佳方法。 Perhaps my software articheture needs to change?也许我的软件架构需要改变?

I have a cron job which hits my website method every 10 seconds and then on my website the method then makes an API call each time to an API however the API is rate limited x amount in a minute and y amount a day我有一个 cron 工作,它每 10 秒点击一次我的网站方法,然后在我的网站上,该方法每次都会对 API 进行一次 API 调用,但是 API 的速率限制为一分钟内 x 次和每天 y 次

Currently i'm exceeding the API limits and need to control this in the website method somehow.目前我超出了 API 限制,需要以某种方式在网站方法中控制它。 I've thought storing in a file perhaps but seems hacky similary to a database as I don't currently use one for this project.我曾想过存储在一个文件中,但似乎与数据库类似,因为我目前没有在这个项目中使用一个。

I've tried this package: https://github.com/David-Desmaisons/RateLimiter but alas it doesn't work in my scenario and I think it would work if I did one request with a loop as provided in his examples.我已经尝试过这个包: https : //github.com/David-Desmaisons/RateLimiter但可惜它在我的场景中不起作用,我认为如果我按照他的示例中提供的循环执行一个请求,它会起作用。 I noticed he had a persistent timer(PersistentCountByIntervalAwaitableConstraint) but he has no documentation or examples for it(I emailed him incase).我注意到他有一个持久计时器(PersistentCountByIntervalAwaitableConstraint),但他没有相关的文档或示例(我给他发了电子邮件)。 I've done a lot of googling around and can't find any examples of this only server rate limiting which is the other way around server limiting client and not client limiting requests to server我已经做了很多谷歌搜索,但找不到这个唯一的服务器速率限制的任何例子,这是服务器限制客户端的另一种方式,而不是客户端限制对服务器的请求

How can I solve my issue without changing the cronjobs?如何在更改 cronjob 的情况下解决我的问题? What does everyone think the best solution to this is?每个人都认为最好的解决方案是什么?

Assuming that you don't want to change the clients generating the load, there is no choice but to implement rate limiting on the server.假设您不想更改生成负载的客户端,则别无选择,只能在服务器上实施速率限制。

Since an ASP.NET application can be restarted at any time, the state used for that rate-limiting must be persisted somewhere.由于 ASP.NET 应用程序可以随时重新启动,因此用于该速率限制的状态必须保留在某处。 You can choose any data store you like for that.您可以为此选择任何您喜欢的数据存储。

In this case you have two limits: One per minute and one per day.在这种情况下,您有两个限制:每分钟一个和每天一个。 If you simply apply two separate rate limiters you will end up with the daily limit being exceeded fairly quickly.如果您简单地应用两个单独的速率限制器,您最终会很快超过每日限制。 After that, there will be no further access for the rest of the day.在那之后,当天剩余的时间将无法进一步访问。 Likely, this is undesirable.很可能,这是不可取的。

It seems better to only apply the daily limit because it is more restrictive.只应用每日限制似乎更好,因为它更具限制性。 A simple solution would be to calculate how far apart requests must be to meet the daily limit.一个简单的解决方案是计算请求必须相隔多远才能满足每日限制。 Then, you store the date of the last request.然后,您存储上次请求的日期。 Any new incoming request is immediately failed if not enough time has passed.如果时间不够,任何新的传入请求都会立即失败。

Let me know if this helps you.如果这对您有帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# 服务用于 Shopify 管理员 API 集成速率限制问题 - c# service for Shopify Admin API integration rate limit issue 如果使用WebApiThrottle超过API速率限制,则将API请求阻止5分钟-C#Web API - Block API requests for 5 mins if API rate limit exceeds using WebApiThrottle - C# Web API 限制c#中文本框十进制输入的最佳方法 - Best way to limit textbox decimal input in c# C# - 2个客户端程序交易变量信息 - 有更好的方法吗? - C# - 2 clientside programs trading variable information — is there a better way? Echo Nest API:将curl命令转换为用于检查速率限制的C# - Echo Nest API: convert curl command for checking rate limit to c# 使用C#MCV的GMail API超出了用户速率限制 - User-rate limit exceeded for GMail API using C# MCV 将C#转换为客户端Javascript - Convert C# to clientside Javascript 与C#开发人员就API中的错误进行沟通的最佳方法是什么? - Whats the best way to communicate with C# developers about bugs in their API? C#:使用 Polly 发出 HTTP 请求的节流/速率限制 - C#: Throttle/rate limit outgoing HTTP requests with Polly 如何在C#中检查响应标头中的rate_limit_status - How to check rate_limit_status in response header in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM