heiseonline / shariff-backend-php

👮 PHP backend for Shariff. Shariff enables website users to share their favorite content without compromising their privacy.
http://ct.de/-2467514
133 stars 44 forks source link

Rate Limiting on the Facebook Graph API #148

Open andre-st opened 5 years ago

andre-st commented 5 years ago

Problem: Facebook allows max. 200 requests/hour from a single user (server in the Shariff case). If the limit is exceeded, FB starts annoying the admin with regular error notifications in his Facebook account.

Possible solution: The Facebook ServiceInterface implementation in shariff-backend-php should watch the X-App-Usage HTTP response header and dynamically balance the call volume:

All responses to calls made to the Graph API include an X-App-Usage HTTP header. This header contains the current percentage of usage for your app. (https://developers.facebook.com/docs/graph-api/advanced/rate-limiting)

The ServiceInterface could define a canRequest($url) method (for getRequest($url)), with the backend returning the cached value if canRequest is false. Most ServiceInterface implementations would just return true. Or getRequest() could check the limit, return false or null and trigger the cache. Whatever works for you.

The Facebook implementation, however, would return a value with respect to the rate limiting header and thus dynamically balances the call volume. This would be a neat feature.