Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added queue manager #192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static IServiceCollection AddWarcraftClients(this IServiceCollection serv
"ArgentPonyWarcraftClient.WarcraftClient.HttpClient",
client => client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"))
).AddTypedClient(httpClient =>
new WarcraftClient(clientId, clientSecret, region, locale, httpClient)
new WarcraftClient(clientId, clientSecret, region, locale, httpClient, QueueManagerFactory.Instance)
);

services.AddTransientUsingServiceProvider<IWarcraftClient, WarcraftClient>()
Expand Down
10 changes: 8 additions & 2 deletions src/ArgentPonyWarcraftClient/Client/WarcraftClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class WarcraftClient : IWarcraftClient
private static readonly JsonSerializerOptions s_jsonSerializerOptions;

private readonly HttpClient _client;
private readonly IQueueManager _queueManager;
private readonly string _clientId;
private readonly string _clientSecret;
private readonly Region _region;
Expand Down Expand Up @@ -54,7 +55,7 @@ public WarcraftClient(string clientId, string clientSecret) : this(clientId, cli
/// Specifies the language that the result will be in. Visit
/// https://develop.battle.net/documentation/world-of-warcraft/guides/localization to see a list of available locales.
/// </param>
public WarcraftClient(string clientId, string clientSecret, Region region, Locale locale) : this(clientId, clientSecret, region, locale, InternalHttpClient.Instance)
public WarcraftClient(string clientId, string clientSecret, Region region, Locale locale) : this(clientId, clientSecret, region, locale, InternalHttpClient.Instance, QueueManagerFactory.Instance)
{
}

Expand All @@ -69,9 +70,11 @@ public WarcraftClient(string clientId, string clientSecret, Region region, Local
/// https://develop.battle.net/documentation/world-of-warcraft/guides/localization to see a list of available locales.
/// </param>
/// <param name="client">The <see cref="HttpClient"/> that communicates with Blizzard.</param>
public WarcraftClient(string clientId, string clientSecret, Region region, Locale locale, HttpClient client)
/// <param name="queueManager">The <see cref="IQueueManager"/> that manage request sending.</param>
public WarcraftClient(string clientId, string clientSecret, Region region, Locale locale, HttpClient client, IQueueManager queueManager)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_queueManager = queueManager ?? throw new ArgumentNullException(nameof(queueManager));
_clientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
_clientSecret = clientSecret ?? throw new ArgumentNullException(nameof(clientSecret));

Expand Down Expand Up @@ -154,6 +157,9 @@ private async Task<RequestResult<T>> GetAsync<T>(string requestUri, string acces
// Add an authentication header with the token.
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

//Wait for access to send
_queueManager.WaitForAccessToSend();

// Retrieve the response.
HttpResponseMessage response = await _client.GetAsync(requestUri).ConfigureAwait(false);

Expand Down
15 changes: 15 additions & 0 deletions src/ArgentPonyWarcraftClient/Interfaces/IQueueManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace ArgentPonyWarcraftClient
{
/// <summary>
/// Queue manager which grand access not often then 100 times per second
/// </summary>
public interface IQueueManager : IDisposable
{
/// <summary>
/// Wait untill acces granted.
/// </summary>
void WaitForAccessToSend();
}
}
80 changes: 80 additions & 0 deletions src/ArgentPonyWarcraftClient/Utilities/QueueManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ArgentPonyWarcraftClient
{
/// <summary>
/// Queue manager which grand access not often then 100 times per second
/// </summary>
public class QueueManager : IQueueManager
{
private Queue<AutoResetEvent> _queue;
private CancellationTokenSource _cancelator;
private Queue<DateTime> _sentTimes;
private object _syncObject = new object();
private static int _maxRequestPerSecond = 100;
private static int _second = 1000;

/// <summary>
/// Queue manager which grand access not often then 100 times per second
/// </summary>
public QueueManager()
{
_queue = new Queue<AutoResetEvent>();
_cancelator = new CancellationTokenSource();
_sentTimes = new Queue<DateTime>();
Task.Run(CheckAndGrand, _cancelator.Token);
}

/// <summary>
/// Wait untill acces granted.
/// </summary>
public void WaitForAccessToSend()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
lock(_syncObject)
{
_queue.Enqueue(autoEvent);
}
autoEvent.WaitOne();
autoEvent.Dispose();
}

/// <summary>
/// Stop grant access
/// </summary>
public void Dispose()
{

_cancelator.Cancel();
}


private void CheckAndGrand()
{
while (true)
{
lock(_syncObject)
{
if(_queue.Count > 0)
{
if(_sentTimes.Count == _maxRequestPerSecond)
{
double delta = (DateTime.Now - _sentTimes.Dequeue()).TotalMilliseconds;
if(delta <= 1 * _second)
{
Thread.Sleep((int)(1000 - delta));
}
}
_queue.Dequeue().Set();
_sentTimes.Enqueue(DateTime.Now);
}
}
}
}
}
}
33 changes: 33 additions & 0 deletions src/ArgentPonyWarcraftClient/Utilities/QueueManagerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Net.Http;
using System.Net.Http.Headers;

namespace ArgentPonyWarcraftClient
{
/// <summary>
/// QueueManagerFactory
/// </summary>
public static class QueueManagerFactory
{
private static IQueueManager _instance;

/// <summary>
/// Gets the current IQueueManager instance.
/// </summary>
public static IQueueManager Instance
{
get
{
if (_instance != null)
{
return _instance;
}
else
{
_instance = new QueueManager();

return _instance;
}
}
}
}
}