Skip to content

Commit 45a64b9

Browse files
committed
updated queue logic
user now always gets a ticket, but ticket date determines wait behaviour
1 parent 8250979 commit 45a64b9

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/Tetrifact.Web/Controllers/ArchivesController.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using Microsoft.AspNetCore.Http.Headers;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Logging;
5+
using Newtonsoft.Json.Linq;
56
using System;
7+
using System.Collections.Generic;
68
using System.IO;
79
using System.IO.Abstractions;
810
using System.Linq;
@@ -102,9 +104,14 @@ public ActionResult GetArchive(string packageId, [FromQuery(Name = "ticket")] st
102104
if (string.IsNullOrEmpty(ticket))
103105
return Responses.NoTicket();
104106

105-
ProcessItem item = _processManager.GetByCategory(ProcessCategories.ArchiveQueueSlot).FirstOrDefault(i => i.Id == ticket);
106-
if (item == null)
107+
IEnumerable<ProcessItem> userTickets = _processManager.GetByCategory(ProcessCategories.ArchiveQueueSlot);
108+
ProcessItem userTicket = userTickets.FirstOrDefault(i => i.Id == ticket);
109+
if (userTicket == null)
107110
return Responses.NoTicket();
111+
112+
int count = userTickets.Where(t => t.AddedUTC < userTicket.AddedUTC).Count();
113+
if (count > _settings.MaximumSimultaneousDownloads)
114+
return Responses.QueueFull(count, _settings.MaximumSimultaneousDownloads.Value);
108115
}
109116

110117
string archivePath = _archiveService.GetPackageArchivePath(packageId);

src/Tetrifact.Web/Controllers/TicketsController.cs

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.Extensions.Logging;
33
using System;
4-
using System.Collections.Generic;
54
using System.Linq;
65
using Tetrifact.Core;
76

@@ -60,18 +59,9 @@ public ActionResult CreateQueueTicket(string requestIdentifier)
6059
}
6160
});
6261

63-
if (_processManager.GetByCategory(ProcessCategories.ArchiveQueueSlot).Count() >= _settings.MaximumSimultaneousDownloads)
64-
return new JsonResult(new
65-
{
66-
error = new
67-
{
68-
code = 1,
69-
message = $"Queue is full ({_settings.MaximumSimultaneousDownloads} slots allowed), please try again later"
70-
}
71-
});
72-
7362
string ticket = Guid.NewGuid().ToString();
7463
_processManager.AddUnique(ProcessCategories.ArchiveQueueSlot, ticket, requestIdentifier, new TimeSpan(0, 0, _settings.DownloadQueueTicketLifespan));
64+
7565
return new JsonResult(new
7666
{
7767
success = new
@@ -80,7 +70,7 @@ public ActionResult CreateQueueTicket(string requestIdentifier)
8070
clientIdentifier = requestIdentifier,
8171
required = true
8272
}
83-
}); ;
73+
});
8474
}
8575
catch (Exception ex)
8676
{

src/Tetrifact.Web/Core/Responses.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ public static NotFoundObjectResult NotFoundError(Controller controller, string d
2323
});
2424
}
2525

26+
public static ObjectResult QueueFull(int position, int total)
27+
{
28+
return new ObjectResult(new
29+
{
30+
error = new
31+
{
32+
description = $"Queue is full, you are {position} of {total}."
33+
}
34+
})
35+
{
36+
StatusCode = 432 // http std code, but way too vague
37+
};
38+
}
39+
2640
public static ObjectResult NoTicket()
2741
{
2842
return new ObjectResult(new
@@ -33,7 +47,7 @@ public static ObjectResult NoTicket()
3347
}
3448
})
3549
{
36-
StatusCode = 432 // http standard code for "resource locked for now"
50+
StatusCode = 403 // http std code, but way too vague
3751
};
3852
}
3953

0 commit comments

Comments
 (0)