question

Kim Strasser avatar image
Kim Strasser asked

How can I avoid that my title gets throttled?

It is ok to have more iterations, but please remember to put each thread sleep for a few seconds to avoid throttling.

From this thread: https://community.playfab.com/questions/50280/what-is-the-fastest-way-to-create-and-delete-test.html

I use the following code to create 30 test accounts:

public static async Task<(List<bool> accsuccessfullist)> CreateTestAccounts(this string language, int amount)
{
    Random random;
    bool successful = false;
    List<bool> accsuccessfullist = new List<bool>();

    for (int i = 0; i <= amount - 1; i++)
    {
        random = new Random();
        int number1 = random.Next(1000, 10000); // min = 1000, max = 9999
        int number2 = random.Next(1000, 10000);
        int number3 = random.Next(1000, 10000);
        int number4 = random.Next(1000, 10000);
        string customid = number1.ToString() + "-" + number2.ToString() + "-" + number3.ToString() + "-" + number4.ToString();        

        var result = await PlayFabClientAPI.LoginWithCustomIDAsync(new LoginWithCustomIDRequest()
        {
            TitleId = "E5E2C",
            CreateAccount = true,
            CustomId = customid
        });

        if (result.Error != null)
        {
            var errors = OnPlayFabError(result.Error, language);
        }
        else
        {
            string playfabid = result.Result.PlayFabId;
            string leaderboard1name = "Level 1-1";
            int leaderboard1score = 1000 + i;
            string leaderboard2name = "Level 1-2";
            int leaderboard2score = 2000 + i;
            string leaderboard3name = "Level 1-3";
            int leaderboard3score = random.Next(1000, 10000);
                    
            var resultaddstats = await PlayFabClientAPI.UpdatePlayerStatisticsAsync(new UpdatePlayerStatisticsRequest()
            {
                Statistics = new List<StatisticUpdate> { new StatisticUpdate { StatisticName = leaderboard1name, Value = leaderboard1score }, new StatisticUpdate { StatisticName = leaderboard2name, Value = leaderboard2score }, new StatisticUpdate { StatisticName = leaderboard3name, Value = leaderboard3score } }
            });

            if (resultaddstats.Error != null)
            {
                var errors = OnPlayFabError(resultaddstats.Error, language);
            }
            else
            {
                successful = true;
            }
        }

        accsuccessfullist.Add(successful);
    }
    
    return (accsuccessfullist);
}

Is it necessary to change something in my code so that my title doesn't risk to get throttled?

Is it only necessary to change something if I use API calls in a for loop?

Or is it also necessary to change something when I have two or more API calls that are executed immediately one after another(but without for loop)?

Example: I create only one new account and without for loop:

public static async Task<(bool successful)> CreateTestAccounts(this string language, int amount)
{
    Random random;
    bool successful = false;

    random = new Random();
    int number1 = random.Next(1000, 10000); // min = 1000, max = 9999
    int number2 = random.Next(1000, 10000);
    int number3 = random.Next(1000, 10000);
    int number4 = random.Next(1000, 10000);
    string customid = number1.ToString() + "-" + number2.ToString() + "-" + number3.ToString() + "-" + number4.ToString();        

    var result = await PlayFabClientAPI.LoginWithCustomIDAsync(new LoginWithCustomIDRequest()
    {
        TitleId = "E5E2C",
        CreateAccount = true,
        CustomId = customid
    });

    if (result.Error != null)
    {
        var errors = OnPlayFabError(result.Error, language);
    }
    else
    {
        string playfabid = result.Result.PlayFabId;
        string playertag = "Test Account";

        var resulttagacc = await PlayFab.PlayFabServerAPI.AddPlayerTagAsync(new PlayFab.ServerModels.AddPlayerTagRequest()
        {
            PlayFabId = playfabid,
            TagName = playertag
        });

        if (resulttagacc.Error != null)
        {
            var errors = OnPlayFabError(resulttagacc.Error, language);
        }
        else
        {
            string leaderboard1name = "Level 1-1";
            int leaderboard1score = 1000 + i;
            string leaderboard2name = "Level 1-2";
            int leaderboard2score = 2000 + i;
            string leaderboard3name = "Level 1-3";
            int leaderboard3score = random.Next(1000, 10000);
                    
            var resultaddstats = await PlayFabClientAPI.UpdatePlayerStatisticsAsync(new UpdatePlayerStatisticsRequest()
            {
                Statistics = new List<StatisticUpdate> { new StatisticUpdate { StatisticName = leaderboard1name, Value = leaderboard1score }, new StatisticUpdate { StatisticName = leaderboard2name, Value = leaderboard2score }, new StatisticUpdate { StatisticName = leaderboard3name, Value = leaderboard3score } }
            });

            if (resultaddstats.Error != null)
            {
                var errors = OnPlayFabError(resultaddstats.Error, language);
            }
            else
            {
                successful = true;
            }
        }
    }
    
    return (successful);
}

Should I change something in this code so that my title doesn't risk to get throttled?

Account Management
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

·
Sarah Zhang avatar image
Sarah Zhang answered

>> Is it only necessary to change something if I use API calls in a for loop? Or is it also necessary to change something when I have two or more API calls that are executed immediately one after another(but without for loop)?

It depends on the frequency of your API requests. If the frequency is high enough, the client may exceed the maximum API request rate, the title would get throttled. To avoid the title gets throttled, you can add the following code block to the “for” loop.

 await Task.Delay(2000);
2 comments
10 |1200

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

Kim Strasser avatar image Kim Strasser commented ·

How can I find out if my title gets throttled? Would I get a notification?

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Kim Strasser commented ·

It's a part of the error message. When the client API calls exceed the maximum API request rate, PlayFab would return the error "The client has exceed the maximum api request rate and is being throttled".

0 Likes 0 ·

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.