Quantcast
Channel: Tweetinvi a friendly Twitter C# library
Viewing all articles
Browse latest Browse all 4126

New Post: [ANSWERED] How to use the rate limiter

$
0
0
Ok I will answer myself, it may help someone. Sorry if I don't post much code here, it is not yet written :)

What I decided to do is to first initiate a local variable that will work like the rate limit counter, the difference is that I will not request the limit to Twitter as I will decrement it one my side for each search done.

It basically looks like this
    static public SearchRateLimit GetSearchRateLimit()
    {
        SearchRateLimit searchratelimit = new SearchRateLimit();

        // get all the rate limits
        ITokenRateLimits rateLimits = RateLimit.GetCurrentCredentialsRateLimits();
        searchratelimit.Limit = rateLimits.SearchTweetsLimit.Limit;
        searchratelimit.Remaining = rateLimits.SearchTweetsLimit.Remaining;

        // initiate the reset time only if there already was a request
        if (searchratelimit.Limit != searchratelimit.Remaining)
        {
            searchratelimit.Reset = rateLimits.SearchTweetsLimit.ResetDateTime;
        }
        return searchratelimit;
    }
with the appropriate SearchRateLimit class

As you see I don't write a Reset time if the remaining is not different from the limit : when they are equal it means no search was done yet and so the reset time will not be very meaningful. As such, after a search, if there is no reset time i just call back GetSearchRateLimit(). After each search, I also decrement the Remaining. I will call for a pause in my code execution if Remaining = 0 and ResetDateTime > Now. I will reset the SearchRateLimit if ResetDateTime < Now (it means the limit has been reset).

Note that :
  • I don't recommend to check the limit rate with GetCurrentCredentialsRateLimits() too often as it is rate limited.
  • This will not work properly if you use several threads and they can't use the same SearchRateLimit variable.

Viewing all articles
Browse latest Browse all 4126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>