包装速率限制API调用 [英] Wrapping rate limiting API call

查看:400
本文介绍了包装速率限制API调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以访问每秒接受最大通话次数的API调用。如果超出速率,则会抛出异常



我想将这个调用包含在一个抽象中,这个调用必须保持调用速率在限制之下。它将像一个网络路由器:处理多个呼叫,并将结果返回到正确的呼叫者关心呼叫率。目标是使调用代码尽可能不了解该限制。否则,具有此调用的代码中的每个部分都必须包装到try-catch中!



例如:从可以添加2个数字的extern API调用方法。此API可以每秒 5次调用。任何高于此值的操作都会导致异常。



为了说明这个问题,限制通话速率的外部服务就像这个问题



如何使用Observables构建限速API?



其他信息:

因为你不需要担心这个限制每次从代码的任何部分调用此方法,你想到设计一个包装器方法,你可以调用而不用担心速率限制。在内部你关心的限制,但在外面你公开一个简单的异步方法。



它类似于一个Web服务器。如何将正确的结果返回给正确的客户?



多个调用者将调用此方法,他们将获得结果。这个抽象应该像一个代理。



我该怎么办?



包装器方法应该像

  public async Task< Results& MyMethod()

在方法内部,它将执行逻辑,也许使用Reactive Extensions我不知道。



但是怎么样?我的意思是,多次调用此方法应该将结果返回到正确的调用者。这是可能吗?



非常感谢。

解决方案

有限速库可用(请参阅Esendex的TokenBucket Github Nuget )。



用法非常简单,此示例将限制轮询1 a第二

  //创建一个容量为1令牌的令牌桶,以1令牌/秒的固定间隔。 
ITokenBucket bucket = TokenBuckets.Construct()
.WithCapacity(1)
.WithFixedIntervalRefillStrategy(1,TimeSpan.FromSeconds(1))


// ...

while(true)
{
//从令牌桶中获取令牌。如果令牌不可用,此方法将阻塞,直到
//补充策略将一个添加到桶。
bucket.Consume(1);

Poll();
}



我还需要使它的项目不同步作为扩展方法:

  public static class TokenBucketExtensions 
{
public static Task ConsumeAsync(this ITokenBucket tokenBucket )
{
return Task.Factory.StartNew(tokenBucket.Consume);
}
}

使用这个,你不需要throw / catch异常和编写包装器变得相当微不足道。


I have access an API call that accepts a maximum rate of calls per second. If the rate is exceeded, an exception is thrown.

I would like to wrap this call into an abstraction that does the necessary to keep the call rate under the limit. It would act like a network router: handling multiple calls and returning the results to the correct caller caring about the call rate. The goal is to make the calling code as unaware as possible about that limitation. Otherwise, every part in the code having this call would have to be wrapped into a try-catch!

For example: Imagine that you can call a method from an extern API that can add 2 numbers. This API can be called 5 times per second. Anything higher than this will result in an exception.

To illustrate the problem, the external service that limits the call rate is like the one in the answer to this question

How to build a rate-limiting API with Observables?

ADDITIONAL INFO:

Since you don't want the worry about that limit every time you call this method from any part of your code, you think about designing a wrapper method that you could call without worrying about the rate limit. On the inside you care about the limit, but on the outside you expose a simple async method.

It's similar to a web server. How does it return the correct pack of results to the correct customer?

Multiple callers will call this method, and they will get the results as they come. This abstraction should act like a proxy.

How could I do it?

I'm sure the firm of the wrapper method should be like

public async Task<Results> MyMethod()

And inside the method it will perform the logic, maybe using Reactive Extensions (Buffer). I don't know.

But how? I mean, multiple calls to this method should return the results to the correct caller. Is this even possible?

Thank you a lot!

解决方案

There are rate limiting libraries available (see Esendex's TokenBucket Github or Nuget).

Usage is very simple, this example would limit polling to 1 a second

// Create a token bucket with a capacity of 1 token that refills at a fixed interval of 1 token/sec.
ITokenBucket bucket = TokenBuckets.Construct()
  .WithCapacity(1)
  .WithFixedIntervalRefillStrategy(1, TimeSpan.FromSeconds(1))
  .Build();

// ...

while (true)
{
  // Consume a token from the token bucket.  If a token is not available this method will block until
  // the refill strategy adds one to the bucket.
  bucket.Consume(1);

  Poll();
}

I have also needed to make it async for a project of mine, I simply made an extension method:

public static class TokenBucketExtensions
{
    public static Task ConsumeAsync(this ITokenBucket tokenBucket)
    {
        return Task.Factory.StartNew(tokenBucket.Consume);
    }
}

Using this you wouldn't need to throw/catch exceptions and writing a wrapper becomes fairly trivial

这篇关于包装速率限制API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆