prevent saturation of the user retrieval API

This commit is contained in:
Nicolas Constant 2022-02-02 23:25:03 -05:00
parent 18e0397efe
commit c371218672
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 14 additions and 4 deletions

View File

@ -13,6 +13,8 @@ namespace BirdsiteLive.Statistics.Domain
void CalledTweetApi();
void CalledTimelineApi();
ApiStatistics GetStatistics();
int GetCurrentUserCalls();
}
//Rate limits: https://developer.twitter.com/en/docs/twitter-api/v1/rate-limits
@ -60,7 +62,12 @@ namespace BirdsiteLive.Statistics.Domain
foreach (var old in oldSnapshots) _snapshots.TryRemove(old, out var data);
}
public void CalledUserApi() //GET users/show - 900/15mins
public int GetCurrentUserCalls()
{
return _userCalls;
}
public void CalledUserApi() //GET users/show - 300/15mins
{
Interlocked.Increment(ref _userCalls);
}

View File

@ -32,6 +32,12 @@ namespace BirdsiteLive.Twitter
public TwitterUser GetUser(string username)
{
//Check if API is saturated
var currentCalls = _statisticsHandler.GetCurrentUserCalls();
var maxCalls = _statisticsHandler.GetStatistics().UserCallsMax;
if (currentCalls > maxCalls) return null;
//Proceed to account retrieval
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
ExceptionHandler.SwallowWebExceptions = false;
@ -49,9 +55,6 @@ namespace BirdsiteLive.Twitter
catch (Exception e)
{
_logger.LogError(e, "Error retrieving user {Username}", username);
// TODO keep track of error, see where to remove user if too much errors
return null;
}