mirror of
https://github.com/NicolasConstant/BirdsiteLive
synced 2025-06-05 21:49:16 +02:00
prevent saturation of the user retrieval API
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user