optimizations
This commit is contained in:
parent
f3ea6b58a7
commit
2dacf466fd
2 changed files with 11 additions and 36 deletions
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BirdsiteLive.Common.Settings;
|
using BirdsiteLive.Common.Settings;
|
||||||
|
@ -26,11 +27,9 @@ namespace BirdsiteLive.Twitter.Tools
|
||||||
private readonly ILogger<TwitterAuthenticationInitializer> _logger;
|
private readonly ILogger<TwitterAuthenticationInitializer> _logger;
|
||||||
private static bool _initialized;
|
private static bool _initialized;
|
||||||
private readonly IHttpClientFactory _httpClientFactory;
|
private readonly IHttpClientFactory _httpClientFactory;
|
||||||
private List<HttpClient> _twitterClients = new List<HttpClient>();
|
private ConcurrentDictionary<String, String> _token2 = new ConcurrentDictionary<string, string>();
|
||||||
private List<(String, String)> _tokens = new List<(string,string)>();
|
|
||||||
static Random rnd = new Random();
|
static Random rnd = new Random();
|
||||||
private RateLimiter _rateLimiter;
|
private RateLimiter _rateLimiter;
|
||||||
static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
|
|
||||||
private const int _targetClients = 3;
|
private const int _targetClients = 3;
|
||||||
private InstanceSettings _instanceSettings;
|
private InstanceSettings _instanceSettings;
|
||||||
private readonly (string, string)[] _apiKeys = new[]
|
private readonly (string, string)[] _apiKeys = new[]
|
||||||
|
@ -41,7 +40,6 @@ namespace BirdsiteLive.Twitter.Tools
|
||||||
("3rJOl1ODzm9yZy63FACdg", "5jPoQ5kQvMJFDYRNE8bQ4rHuds4xJqhvgNJM4awaE8"), // Mac
|
("3rJOl1ODzm9yZy63FACdg", "5jPoQ5kQvMJFDYRNE8bQ4rHuds4xJqhvgNJM4awaE8"), // Mac
|
||||||
};
|
};
|
||||||
public String BearerToken {
|
public String BearerToken {
|
||||||
//get { return "AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw"; }
|
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return _instanceSettings.TwitterBearerToken;
|
return _instanceSettings.TwitterBearerToken;
|
||||||
|
@ -82,20 +80,8 @@ namespace BirdsiteLive.Twitter.Tools
|
||||||
public async Task RefreshClient(HttpRequestMessage req)
|
public async Task RefreshClient(HttpRequestMessage req)
|
||||||
{
|
{
|
||||||
string token = req.Headers.GetValues("x-guest-token").First();
|
string token = req.Headers.GetValues("x-guest-token").First();
|
||||||
string bearer = req.Headers.GetValues("Authorization").First().Replace("Bearer ", "");
|
|
||||||
|
|
||||||
var i = _tokens.IndexOf((bearer, token));
|
_token2.TryRemove(token, out _);
|
||||||
|
|
||||||
// this is prabably not thread safe but yolo
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_twitterClients.RemoveAt(i);
|
|
||||||
_tokens.RemoveAt(i);
|
|
||||||
}
|
|
||||||
catch (IndexOutOfRangeException _)
|
|
||||||
{
|
|
||||||
_logger.LogError("Error refreshing twitter token");
|
|
||||||
}
|
|
||||||
|
|
||||||
await RefreshCred();
|
await RefreshCred();
|
||||||
await Task.Delay(1000);
|
await Task.Delay(1000);
|
||||||
|
@ -104,21 +90,8 @@ namespace BirdsiteLive.Twitter.Tools
|
||||||
|
|
||||||
private async Task RefreshCred()
|
private async Task RefreshCred()
|
||||||
{
|
{
|
||||||
|
|
||||||
(string bearer, string guest) = await GetCred();
|
(string bearer, string guest) = await GetCred();
|
||||||
|
_token2.TryAdd(guest, bearer);
|
||||||
HttpClient client = _httpClientFactory.CreateClient();
|
|
||||||
//HttpClient client = new HttpClient();
|
|
||||||
|
|
||||||
_twitterClients.Add(client);
|
|
||||||
_tokens.Add((bearer,guest));
|
|
||||||
|
|
||||||
if (_twitterClients.Count > _targetClients)
|
|
||||||
{
|
|
||||||
_twitterClients.RemoveAt(0);
|
|
||||||
_tokens.RemoveAt(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<(string, string)> GetCred()
|
private async Task<(string, string)> GetCred()
|
||||||
|
@ -144,16 +117,15 @@ namespace BirdsiteLive.Twitter.Tools
|
||||||
|
|
||||||
public async Task<HttpClient> MakeHttpClient()
|
public async Task<HttpClient> MakeHttpClient()
|
||||||
{
|
{
|
||||||
if (_twitterClients.Count < 2)
|
if (_token2.Count < _targetClients)
|
||||||
await RefreshCred();
|
await RefreshCred();
|
||||||
int r = rnd.Next(_twitterClients.Count);
|
return _httpClientFactory.CreateClient();
|
||||||
return _twitterClients[r];
|
|
||||||
}
|
}
|
||||||
public HttpRequestMessage MakeHttpRequest(HttpMethod m, string endpoint, bool addToken)
|
public HttpRequestMessage MakeHttpRequest(HttpMethod m, string endpoint, bool addToken)
|
||||||
{
|
{
|
||||||
var request = new HttpRequestMessage(m, endpoint);
|
var request = new HttpRequestMessage(m, endpoint);
|
||||||
int r = rnd.Next(_twitterClients.Count);
|
//(string bearer, string token) = _tokens[r];
|
||||||
(string bearer, string token) = _tokens[r];
|
(string token, string bearer) = _token2.MaxBy(x => rnd.Next());
|
||||||
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer " + bearer);
|
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer " + bearer);
|
||||||
request.Headers.TryAddWithoutValidation("Referer", "https://twitter.com/");
|
request.Headers.TryAddWithoutValidation("Referer", "https://twitter.com/");
|
||||||
request.Headers.TryAddWithoutValidation("x-twitter-active-user", "yes");
|
request.Headers.TryAddWithoutValidation("x-twitter-active-user", "yes");
|
||||||
|
|
|
@ -9,6 +9,7 @@ using BirdsiteLive.DAL.Contracts;
|
||||||
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
|
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
|
||||||
using BirdsiteLive.DAL.Postgres.Settings;
|
using BirdsiteLive.DAL.Postgres.Settings;
|
||||||
using BirdsiteLive.Models;
|
using BirdsiteLive.Models;
|
||||||
|
using BirdsiteLive.Services;
|
||||||
using BirdsiteLive.Twitter;
|
using BirdsiteLive.Twitter;
|
||||||
using BirdsiteLive.Twitter.Tools;
|
using BirdsiteLive.Twitter.Tools;
|
||||||
using Lamar;
|
using Lamar;
|
||||||
|
@ -89,6 +90,8 @@ namespace BirdsiteLive
|
||||||
services.For<ITwitterUserService>().Use<TwitterUserService>().Singleton();
|
services.For<ITwitterUserService>().Use<TwitterUserService>().Singleton();
|
||||||
|
|
||||||
services.For<ITwitterAuthenticationInitializer>().Use<TwitterAuthenticationInitializer>().Singleton();
|
services.For<ITwitterAuthenticationInitializer>().Use<TwitterAuthenticationInitializer>().Singleton();
|
||||||
|
|
||||||
|
services.For<ICachedStatisticsService>().Use<CachedStatisticsService>().Singleton();
|
||||||
|
|
||||||
services.Scan(_ =>
|
services.Scan(_ =>
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue