This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

spring-social-twitter 1.0.1 and Rate Limit

Jan 18th, 2012, 04:34 AM

I've been testing potential behaviour of my app when an authenticated user of twitter is near their rate limit. I'm finding the TwitterTemplate is returning an OperationNotPermittedException rather than an expected RateLimitException.

I'm not particularly comfortable with the idea of parsing the message text of an exception to decide whether to rethrow it.

To give some context to this; if a status update fails, in most cases I plan to throw it away, but for 'rate limit reached, I'll place that message back on a queue for a delayed delivery (determined by the new RateLimitStatus object).

Comment

I noticed if I try the same thing with spring-social-facebook, when I hit my application's posting limit for an authenticated user I get the RateLimitExceeded exception rather than OperationNotPermittedException.

Twitter apparently don't count 'posts' toward the 'rate limit' - probably hence the lack of RateLimitExceeded exception, I'm wondering therefore it there should be a new 'PostLimitExceeded' exception or similar for these types of circumstances?

Comment

I've discovered TwitterErrorHandler does not handle this type of error. Whenever a HttpStatus.FORBIDDEN status code is returned; duplicate status; status too long; and invalid recipient are checked before throwing a general OperationNotPermittedException...

I'd recommend the error response is also checked for the text "User is over daily status update limit" which will result in a RateLimitExceeded exception being thrown. This will make it consistent with the spring-social-facebook and a distinctly catchable exception.

Comment

This is now fixed and I've already pushed a snapshot build (1.0.2.BUILD-SNAPSHOT) includes the fix.

I also spent some time looking into how to get rate limit details for this particular case. As you noted RateLimitStatus doesn't include this information, but that's because Twitter doesn't return any useful information in this particular case. Even though I'm getting this error, the "account/rate_limit_status" endpoint indicates that I have not yet exceeded the rate limit. I also examined the response headers, hoping to find some useful information there that might indicate how long before the limit is cleared, but I could find no such information in the headers.

It's unfortunate that Twitter doesn't seem to give any clues regarding the status of this particular rate limit, as that information would (at very least) be useful in documentation or possibly as data attached to the RateLimitExceededException.

If you discover a way to get stats on this kind of rate limit, please let me know and I'll see if I can work it in.

Comment

The posting limit seems a bit of a black art with Twitter, I read somewhere on the dev site they reduce the posting limits to shape traffic depending on the load on the site.

I also read that a 'post' does not reduce the number of api calls you are allowed, yet each time I got a failed tweet, the rate limit started decreasing each time. Although interestingly, the time to resume was exactly 1 hour after I originally met my posting limit, but sadly does not correspond to when I can start posting again!