Auto rate limiting pausing

Jun 17, 2015 at 10:59 PM
I am trying to have tweetinvi wait for ratelimits to be ready again to carry on scraping, based on the post linvi made:
https://tweetinvi.codeplex.com/discussions/638893

// Tweetinvi will manage the RateLimits for you and automatically resume the operation when needed.
RateLimit.RateLimitTrackerOption = RateLimitTrackerOptions.TrackAndAwait;
RateLimit.QueryAwaitingForRateLimit += (sender, args) =>
{
    Console.WriteLine("WAITING FOR RATE LIMITS ({0} seconds) : {1}", args.QueryRateLimit.ResetDateTimeInSeconds, args.Query);
};
On hitting a rate limit, the program does indeed pause, however I expectd the event to file and log to the console it was rate limited, but it didn't. After a 15 minute or so pause, the program returned with 80,000 results, although 100k were requested.


Is my assumption wrong that with the code as pasted above, the event should fire logging the rate limiting? Or am I missing something.

I am using the latest download version 0.971 (ish I think)
Jun 17, 2015 at 11:05 PM
 if (weakDelegate.IsAlive)
                {
                    weakDelegate.Invoke(sender, e);
                }
                else
                {
                    lock (_syncRoot)
                    {
                        _handlers.Remove(weakDelegate);
                    }
                }
\Tweetinvi.Core\Events\WeakEvent.cs
line 64

not too sure what this code is doing, but it is ..IsAlive == false, so the handler is removed. Not sure why it is not alive though
Jun 17, 2015 at 11:15 PM
so, if I ignore the check
if (weakDelegate.IsAlive){
...
and just fire the code, it all seems to work.


So the question is (slightly more refined)

What does IsAlive mean, and why is it false, can I just ignore it safely?


Cheers
Jun 18, 2015 at 12:32 AM
Hi,

The WeakDelegate allows to prevent memory leaks. If you find your WeakDelegate being null, please call a method instead of inline event.
The reason is that if your application registers an event on a static class, the object which registered the event will never be freed from the memory because it has a pointer to an object (static) that is never freed.
RateLimit.QueryAwaitingForRateLimit += QueryAwaitingForRateLimit;

private void QueryAwaitingForRateLimit(object sender, QueryAwaitingEventArgs e)
{
    throw new NotImplementedException();
}
Linvi
Marked as answer by linvi on 6/17/2015 at 4:51 PM
Jun 18, 2015 at 12:50 AM
Edited Jun 18, 2015 at 12:51 AM
Now that I am thinking about it should not be my responsibility to ensure that developers free their memory correctly.
I will consider removing the WeakEvents or having a parameter set to false by default and if set to true will use WeakEvents instead of classic Events.

I have created a work item for this 'issue'.

This work item will be done for the next release of Tweetinvi. In the meantime, please do not remove the .IsAlive instead register your events as explained above (using a function instead of lambda expression).

Finally please note that their is a new documentation for RateLimits on github.

I think it could be useful and I would greatly appreciate any feedback.

Kind Regards,
Linvi
Marked as answer by linvi on 6/17/2015 at 4:51 PM
Jun 19, 2015 at 2:30 AM
Thanks for the explanation.

I don't tend to worry about memory leaks, my programs crash way before they are a problem lol.


Will try your suggestions and take a look at the new docs


cheers