question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RateLimit.GetQueryRateLimit(args.QueryURL) is null

See original GitHub issue

Hi Linvi,

I’m using TweetinviEvents.QueryBeforeExecute to check whether the next request is rate limited or not. I noticed two strange behaviours:

  • QueryBeforeExecute is called more times or wasn’t even called when i executed the request
  • RateLimit.GetQueryRateLimit(args.QueryURL) returns null after around 100 same requests (though there are still a lot left) Can you check please my code below?

Regards, Levente

private static void InitializeTweetinviConfig(bool isApp = false)
        {
            ExceptionHandler.SwallowWebExceptions = false;

            // use of multiple twitter apps
            _isApp = isApp;

            RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly;

            TweetinviConfig.ApplicationSettings.TweetMode = TweetMode.Extended;
        }
private void InitializeTweetinviConfigWithAuth(string accessToken, string accessTokenSecret, bool isApp = false)
        {
            InitializeTweetinviConfig(isApp);
            
            // i have tried with and without clear cache
            // RateLimit.ClearRateLimitCache();

            TweetinviEvents.CurrentThreadEvents.QueryBeforeExecute += (sender, args) =>
            {
                var rateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);
                if (rateLimits != null)
                {
                    if (rateLimits.Remaining > 0)
                    {
                        // We have enough resource to execute the query
                        return;
                    }
                }
                else
                {
                    return;
                }                
            
                // it's code it wasn't reached cuz rateLimits was null
                var shouldChangeCredentials = _isApp;
                if (shouldChangeCredentials)
                {
                    //if it's app request, loop through all customer credentials until find one that are not rate limited 
                    foreach (var item in _alternativeTwitterCustomerCreds)
                    {
                        var alternativeCredential = Auth.SetApplicationOnlyCredentials(item.Key, item.Value, true); 
                        var alternativeQueryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL, alternativeCredential);
                        if (alternativeQueryRateLimits != null)
                        {
                            if (alternativeQueryRateLimits.Remaining > 0)
                            {
                                args.TwitterQuery.TwitterCredentials = alternativeCredential;
                                return;
                            }
                        }
                    }
                }
            };

            Auth.SetUserCredentials(_consumerKey, _consumerSecret, accessToken, accessTokenSecret);            
        }
public async Task<List<ITweet>> GetPosts(string accessToken, string accessTokenSecret, List<long> postIds)
        {          
            return await Sync.ExecuteTaskAsync(() =>
            {
                    InitializeTweetinviConfigWithAuth(accessToken, accessTokenSecret, true);

                    return Tweet.GetTweets(postIds.ToArray()).ToList();
            });
        }
        //test api endpoint, for testing rate limit
        [HttpGet]
        public async Task<IHttpActionResult> GetTwitterPostsByIds()
        {
                string accessToken = "-------------------------";
                string accessTokenSecret = "-------------------------";

                List<long> ids = new List<long>() { 771698365948100608,
                                                    779297282642542593,
                                                    779313850462113792 };
                for (int i = 0; i < 1000; i++)
                {
                    List<ITweet> tweets = await _twitterService.GetPosts(accessToken, accessTokenSecret, ids);
                }
                return Ok();
        }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
linvicommented, Jun 20, 2017

I will be looking into it as part of the next release. I will provide some feedback as quickly as possible.

0reactions
prakash-patelcommented, Aug 26, 2019

@linvi I am still having issue, so i started catching exception using manual exception.

public class Twitter
{
    public Twitter()
    {
         //Catch all the twitter operation related error
         ExceptionHandler.SwallowWebExceptions = false;`
         
         Auth.SetUserCredentials(_currentConsumerCredentials.ConsumerKey, 
        _currentConsumerCredentials.ConsumerSecret, accessToken, accessTokenSecret);
 
        RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly;
        TweetinviEvents.QueryBeforeExecute += (sender, args) =>
            {
                var rateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);
                if (rateLimits != null)
                {
                    if (rateLimits.Remaining > 0)
                    {
                        // We have enough resource to execute the query
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
    }


public GetTweet()
    {
            
            try
            {
               // logic here for getting tweets
            }
            catch (TwitterException error)
            {
                if (error.StatusCode == TweetinviConsts.STATUS_CODE_TOO_MANY_REQUEST)
                {
                    // The RateLimit is exhausted. Perform your code to manage it!
                }
            }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

ratelimit tracking issue #381 - linvi/tweetinvi
i tried to search tweets using ur method to deal with rate limit but there is ... GetQueryRateLimit(args. ... if (queryRateLimits != null)...
Read more >
Is there a way to "notify" when you hit the hourly limit?
QueryBeforeExecute += (sender, args) => { var queryRateLimit = RateLimit.GetQueryRateLimit(args.QueryURL); if (queryRateLimit != null) { if ...
Read more >
TweetInvi - Cannot Unfollow Users
GetQueryRateLimit (args.QueryURL); // Some methods are not RateLimited. Invoking such a method will result in the queryRateLimits to be null ...
Read more >
Tweetinvi a friendly Twitter C# library - RSSing.com
If the credentials are set to null, the CurrentThreadCredentials will be used; if they have not been set either, an exception will be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found