question

Brent Batas (Lisk) avatar image
Brent Batas (Lisk) asked

API Queue to rate-limit my API calls

On my dedicated server, I want to make an API call, then run some code on the result. I'm working with the C# SDK.

Using async/await, this code works fine:

var result = await PlayFabAPI.Call();
// do stuff // I can return the result to the caller


However, it gets more complicated because I want to introduce an "API Queue" which runs only up to 10x api calls a second, to stay within the PlayFab API rate limits for dedicated servers. It also retries API calls if they fail due to throttling (something like exponential backoff).

The only way I can currently think of doing it is like follows:

Action<string> whenApiCallFinished = (result) => 
{
    // do stuff
};

AddToAPIQueue(() => 
{ 
    var result = PlayFabAPI.Call();
    whenApiCallFinished(result);
});


// I now have no way to return the result to the caller


However, this feels hacky because now I am mixing in callbacks with async/await. My immediate question is: is it possible to do this API Queue logic with pure async/await instead of callbacks?

My broader question is: is this a good approach to trying to stay within PlayFab limits? I'm very open to changing my approach if you guys have a better solution. I figured I'd ask about the broader question before spending more hours figuring out the immediate question.

If my approach is good, then I would appreciate any guidance on the immediate question as well.

sdks
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Brent Batas (Lisk) avatar image
Brent Batas (Lisk) answered

I finally ended up figuring out a solution using semaphores (SemaphoreSlim) instead of my initial queue-based approach. I basically have requesters await WaitAsync() and have the SemaphoreSlim periodically release locks. I was able to avoid using any Delay() or Wait() or callbacks, so I am pretty happy with the solution.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.