[ANSWERED] How to use the rate limiter

Apr 16, 2015 at 7:38 PM
Edited Apr 16, 2015 at 7:43 PM

I can't manage to understand how to use the rate limiter, and the documentation is too short for me.

I tried on purpose to send the same search query time after time to reach the limiter but can't understand how it is supposed to work.

Here is what I did :
public void SearchByDay()
var rateLimits = RateLimit.GetCurrentCredentialsRateLimits();
// Enable Tweetinvi RateLimit Handler
RateLimit.UseRateLimitAwaiter = true;
// Getting notified when your application is being stopped to wait for RateLimits to be available
RateLimit.QueryAwaitingForRateLimit += (sender, args) =>
Console.WriteLine("{0} is awaiting {1}ms for RateLimit to be available", args.Query, args.ResetInMilliseconds);
for (int i = 0; i <= 200; i++)
rateLimits = RateLimit.GetCurrentCredentialsRateLimits();
The event wasn't triggered but now I can't access anything (i hope Twitter will not ban me for too long)

Anyway, can you provide a clearer way to test if I didn't exceeded the search limit and in case I reached the limit to wait for the counter reset.

Thanks a lot
Apr 16, 2015 at 11:33 PM
Edited Apr 16, 2015 at 11:40 PM
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.
Marked as answer by PsykotropyK on 4/16/2015 at 3:49 PM
Apr 18, 2015 at 10:15 PM

Tweetinvi contains a RateLimit awaiter since version
// When performing a query, the RateLimitAwaiter will await for the RateLimits to be available before executing a query.
RateLimit.RateLimitTrackerOption = RateLimitTrackerOptions.TrackAndAwait;

// The RateLimitAwaiter only update the cache RateLimits and let you handle them on your own.
RateLimit.RateLimitTrackerOption = RateLimitTrackerOptions.TrackOnly;

// Use the QueryBeforeExecute to handle the RateLimit and do what you want with it
TweetinviEvents.QueryBeforeExecute += (sender, args) =>
    var queryRateLimit = RateLimit.GetQueryRateLimit(args.QueryURL);

    if (queryRateLimit != null && queryRateLimit.Remaining == 0)