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

Set a custom API limit if specified in the config. #1043

Open
wants to merge 3 commits into
base: master
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
10 changes: 10 additions & 0 deletions POEApi.Model/POEModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ void instance_Throttled(object sender, ThottledEventArgs e)

private ITransport GetTransport(string email, bool offline)
{
int customRequestLimit;
if (Settings.UserSettings.Keys.Contains("ApiRequestLimit") &&
int.TryParse(Settings.UserSettings["ApiRequestLimit"], out customRequestLimit))
{
string resultIndication = HttpTransport.AdjustThrottleWindowLimit(customRequestLimit)
? "Successfully" : "Failed to";
Logger.Log(string.Format("{0} set throttle window limit to custom size of '{1}'.",
resultIndication, customRequestLimit));
}

if (Settings.ProxySettings["Enabled"] != bool.TrueString)
return new CachedTransport(email, new HttpTransport(email), offline);

Expand Down
14 changes: 13 additions & 1 deletion POEApi.Transport/HttpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ protected enum HttpMethod { GET, POST }
private const string UpdateShopURL = @"https://www.pathofexile.com/forum/edit-thread/{0}";
private const string BumpShopURL = @"https://www.pathofexile.com/forum/post-reply/{0}";

private const int _maximumWindowLimit = 42;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be brought in as an application setting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather not, for two reasons.

  1. This value will likely very rarely -- if ever -- change, so the user will be unlikely to modify the setting. He can also easily make Procurement work very poorly by increasing the value.
  2. This class can't reference the Settings directly, since that would introduce a circular dependency between the projects. Thus -- barring more substantial refactoring I'm not interested in doing now -- it would then have to be set after the static constructor finishes, potentially after it started handling requests.


protected const string UserAgent =
@"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; " +
@".NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; " +
Expand All @@ -44,7 +46,17 @@ protected enum HttpMethod { GET, POST }

public event ThottledEventHandler Throttled;

private static TaskThrottle _taskThrottle = new TaskThrottle(TimeSpan.FromMinutes(1), 42, 42);
private static TaskThrottle _taskThrottle =
new TaskThrottle(TimeSpan.FromMinutes(1), _maximumWindowLimit, _maximumWindowLimit);

public static bool AdjustThrottleWindowLimit(int newWindowLimit)
{
if (newWindowLimit < 1 || newWindowLimit > _maximumWindowLimit)
return false;

_taskThrottle.AdjustWindowLimit(newWindowLimit, newWindowLimit);
return true;
}

public HttpTransport(string login)
{
Expand Down
26 changes: 16 additions & 10 deletions POEApi.Transport/TaskThrottle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TaskThrottle
protected Queue<DateTime> CurrentTasks { get; set; }
public TimeSpan WindowSize { get; set; }
public int WindowLimit { get; protected set; }
public int SimultaneiousTasksLimit { get; protected set; }
public int SimultaneousTasksLimit { get; protected set; }

private int _numberOfOutstandingTasks;
public int NumberOfOutstandingTasks
Expand All @@ -28,34 +28,40 @@ protected set

private Object _lockObject = new Object();

public TaskThrottle(TimeSpan windowSize, int windowLimit, int simultaneiousTasksLimit)
public TaskThrottle(TimeSpan windowSize, int windowLimit, int simultaneousTasksLimit)
{
CurrentTasks = new Queue<DateTime>(simultaneiousTasksLimit);
CurrentTasks = new Queue<DateTime>(simultaneousTasksLimit);
WindowSize = windowSize;
WindowLimit = windowLimit;
SimultaneiousTasksLimit = simultaneiousTasksLimit;
SimultaneousTasksLimit = simultaneousTasksLimit;
}

public void AdjustWindowLimit(int newWindowLimit, int newSimultaneousTasksLimit)
{
WindowLimit = newWindowLimit;
SimultaneousTasksLimit = newSimultaneousTasksLimit;
}

public void StartTask()
{
bool finished = false;
while (!finished)
{
while (NumberOfOutstandingTasks >= SimultaneiousTasksLimit)
while (NumberOfOutstandingTasks >= SimultaneousTasksLimit)
{
System.Threading.Thread.Sleep(100);
}

lock(_lockObject)
{
if (NumberOfOutstandingTasks >= SimultaneiousTasksLimit)
if (NumberOfOutstandingTasks >= SimultaneousTasksLimit)
{
// Another thread added a task to the queue before we could get the lock.
continue;
}

RemvoeExpiredTasks();
if (CurrentTasks.Count == WindowLimit)
RemoveExpiredTasks();
while (CurrentTasks.Count >= WindowLimit)
{
TimeSpan waitTime = CurrentTasks.Dequeue() - DateTime.Now;
if (waitTime.TotalMilliseconds > 0)
Expand All @@ -77,11 +83,11 @@ public void CompleteTask()
lock(_lockObject)
{
System.Threading.Interlocked.Decrement(ref _numberOfOutstandingTasks);
RemvoeExpiredTasks();
RemoveExpiredTasks();
}
}

protected void RemvoeExpiredTasks()
protected void RemoveExpiredTasks()
{
while (CurrentTasks.Count > 0 && CurrentTasks.Peek() <= DateTime.Now)
{
Expand Down