Compare commits
1 Commits
main
...
calculate-
Author | SHA1 | Date |
---|---|---|
|
b1b1b44e0f |
|
@ -1,33 +1,6 @@
|
|||
@inherits LayoutComponentBase
|
||||
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="/">AccessQueue Playground</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/about">About</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/config">Config</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="https://git.hobbs.zone/henry/AccessQueueService" target="_blank" rel="noopener">Source</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container-body">
|
||||
@Body
|
||||
</div>
|
||||
@Body
|
||||
|
||||
<div id="blazor-error-ui">
|
||||
An unhandled error has occurred.
|
||||
|
|
|
@ -16,10 +16,3 @@
|
|||
right: 0.75rem;
|
||||
top: 0.5rem;
|
||||
}
|
||||
|
||||
.container-body {
|
||||
width: 90%;
|
||||
max-width: 1000px;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
@page "/about"
|
||||
|
||||
<PageTitle>About - AccessQueue Playground</PageTitle>
|
||||
|
||||
<h2>About AccessQueue Playground</h2>
|
||||
<p>
|
||||
<b>AccessQueue Playground</b> is a demo Blazor application for testing and visualizing the <b>AccessQueueService</b> system. It allows you to simulate users requesting access, manage a queue, and experiment with configuration options such as expiration, activity, and capacity limits.
|
||||
</p>
|
||||
<ul>
|
||||
<li>Project: <b>AccessQueueService</b></li>
|
||||
<li>Frontend: <b>Blazor (BlazorBootstrap)</b></li>
|
||||
<li>Backend: <b>.NET 8</b></li>
|
||||
</ul>
|
||||
|
||||
<h3>How AccessQueueService Works</h3>
|
||||
<ol>
|
||||
<li><b>Requesting Access:</b> Users are granted access if capacity is available, otherwise they are queued. Expiration and activity timeouts are enforced.</li>
|
||||
<li><b>Queueing:</b> Users in the queue must remain active to keep their spot. The queue is managed FIFO (first-in, first-out).</li>
|
||||
<li><b>Dequeuing:</b> When capacity is available, users are dequeued and granted access if still active.</li>
|
||||
<li><b>Maintaining Access:</b> Users must re-request access to remain active. Expiration can be rolling or fixed.</li>
|
||||
<li><b>Revoking Access:</b> Users can revoke their access, freeing up capacity for others.</li>
|
||||
</ol>
|
||||
|
||||
<h3>Configuration Options</h3>
|
||||
<ul>
|
||||
<li><b>CapacityLimit:</b> Max concurrent users with access.</li>
|
||||
<li><b>ActivitySeconds:</b> How long a user can be inactive before losing their spot.</li>
|
||||
<li><b>ExpirationSeconds:</b> How long before an access ticket expires.</li>
|
||||
<li><b>RollingExpiration:</b> If true, expiration resets on activity.</li>
|
||||
<li><b>RefreshRateMilliseconds:</b> How often the playground requests access for active users and updates the UI.</li>
|
||||
</ul>
|
||||
<p>For now these options can only be set via appsettings.json. The Playground UI does not yet support changing these values.</p>
|
||||
|
||||
<h3>About the Playground UI</h3>
|
||||
<p>
|
||||
The Playground UI lets you:
|
||||
<ul>
|
||||
<li>Add users to the queue and grant access.</li>
|
||||
<li>Revoke access for individual users or all users.</li>
|
||||
<li>Reset all data for testing.</li>
|
||||
<li>See real-time updates of users with access, in queue, and inactive.</li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<h3>Source Code & Documentation</h3>
|
||||
<p>
|
||||
See the <a href="https://git.hobbs.zone/henry/AccessQueueService" target="_blank" rel="noopener">project repository</a> for full documentation, source code, and usage instructions.
|
||||
</p>
|
|
@ -1,109 +0,0 @@
|
|||
@page "/config"
|
||||
@inject AccessQueuePlayground.Services.IAccessQueueManager QueueManager
|
||||
@using BlazorBootstrap
|
||||
|
||||
<h3>Access Queue Configuration</h3>
|
||||
|
||||
<EditForm Model="config" OnValidSubmit="HandleValidSubmit">
|
||||
<DataAnnotationsValidator />
|
||||
<ValidationSummary />
|
||||
<div class="mb-3">
|
||||
<label for="capacityLimit">Capacity Limit</label>
|
||||
<TextInput Id="capacityLimit" @bind-Value="config.CapacityLimit" Type="TextInputType.Number" />
|
||||
@if (!isCapacityLimitValid)
|
||||
{
|
||||
<div class="text-danger">Please enter a positive integer.</div>
|
||||
}
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="activitySeconds">Activity Seconds</label>
|
||||
<TextInput Id="activitySeconds" @bind-Value="config.ActivitySeconds" Type="TextInputType.Number" />
|
||||
@if (!isActivitySecondsValid)
|
||||
{
|
||||
<div class="text-danger">Please enter a positive integer.</div>
|
||||
}
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="expirationSeconds">Expiration Seconds</label>
|
||||
<TextInput Id="expirationSeconds" @bind-Value="config.ExpirationSeconds" Type="TextInputType.Number" />
|
||||
@if (!isExpirationSecondsValid)
|
||||
{
|
||||
<div class="text-danger">Please enter a positive integer.</div>
|
||||
}
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="cacheMillis">Cache Milliseconds</label>
|
||||
<TextInput Id="cacheMillis" @bind-Value="config.CacheMilliseconds" Type="TextInputType.Number" />
|
||||
@if (!isCacheMillisValid)
|
||||
{
|
||||
<div class="text-danger">Please enter a positive integer or zero.</div>
|
||||
}
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<Switch Id="rollingExpiration" @bind-Value="config.RollingExpiration" Label="Rolling Expiration" />
|
||||
</div>
|
||||
<Button Type="ButtonType.Submit" Color="ButtonColor.Primary">Save</Button>
|
||||
@if (successMessage != null)
|
||||
{
|
||||
<Alert Color="AlertColor.Success" Class="mt-3">@successMessage</Alert>
|
||||
}
|
||||
</EditForm>
|
||||
|
||||
@code {
|
||||
private ConfigModel config = new();
|
||||
private bool isCapacityLimitValid = true;
|
||||
private bool isActivitySecondsValid = true;
|
||||
private bool isExpirationSecondsValid = true;
|
||||
private bool isCacheMillisValid = true;
|
||||
private string? successMessage;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
var current = QueueManager.GetConfig();
|
||||
config = new ConfigModel
|
||||
{
|
||||
ActivitySeconds = (current.ActivitySeconds ?? 0).ToString(),
|
||||
CapacityLimit = (current.CapacityLimit ?? 0).ToString(),
|
||||
ExpirationSeconds = (current.ExpirationSeconds ?? 0).ToString(),
|
||||
CacheMilliseconds = (current.CacheMilliseconds ?? 0).ToString(),
|
||||
RollingExpiration = current.RollingExpiration ?? false
|
||||
};
|
||||
ValidateInputs();
|
||||
}
|
||||
|
||||
private bool IsFormValid => isCapacityLimitValid && isActivitySecondsValid && isExpirationSecondsValid && isCacheMillisValid;
|
||||
|
||||
private void ValidateInputs()
|
||||
{
|
||||
isCapacityLimitValid = int.TryParse(config.CapacityLimit, out var cap) && cap > 0;
|
||||
isActivitySecondsValid = int.TryParse(config.ActivitySeconds, out var act) && act > 0;
|
||||
isExpirationSecondsValid = int.TryParse(config.ExpirationSeconds, out var exp) && exp > 0;
|
||||
isCacheMillisValid = int.TryParse(config.CacheMilliseconds, out var cache) && cache >= 0;
|
||||
}
|
||||
|
||||
private async Task HandleValidSubmit()
|
||||
{
|
||||
successMessage = null;
|
||||
ValidateInputs();
|
||||
if (!IsFormValid)
|
||||
return;
|
||||
await Task.Run(() => QueueManager.UpdateConfig(new ()
|
||||
{
|
||||
ActivitySeconds = int.Parse(config.ActivitySeconds),
|
||||
CapacityLimit = int.Parse(config.CapacityLimit),
|
||||
ExpirationSeconds = int.Parse(config.ExpirationSeconds),
|
||||
CacheMilliseconds = int.Parse(config.CacheMilliseconds),
|
||||
RollingExpiration = config.RollingExpiration
|
||||
}));
|
||||
successMessage = "Configuration updated successfully.";
|
||||
}
|
||||
|
||||
public class ConfigModel
|
||||
{
|
||||
public string CapacityLimit { get; set; } = "";
|
||||
public string ActivitySeconds { get; set; } = "";
|
||||
public string ExpirationSeconds { get; set; } = "";
|
||||
public string CacheMilliseconds { get; set; } = "";
|
||||
public bool RollingExpiration { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
@page "/"
|
||||
@using AccessQueuePlayground.Models
|
||||
@using AccessQueuePlayground.Services
|
||||
@using AccessQueueService.Models;
|
||||
@using BlazorBootstrap
|
||||
|
||||
@inject IAccessQueueManager Manager
|
||||
|
@ -13,19 +12,17 @@
|
|||
<p>
|
||||
<b>Expiration Seconds:</b> @Config.ExpirationSeconds,
|
||||
<b>Activity Seconds:</b> @Config.ActivitySeconds,
|
||||
<b>Capacity Limit:</b> @Config.CapacityLimit,
|
||||
<b>Rolling Expiration:</b> @Config.RollingExpiration
|
||||
<b>Capacity Limit:</b> @Config.CapacityLimit
|
||||
</p>
|
||||
}
|
||||
<p>
|
||||
<Button Color="ButtonColor.Success" @onclick="() => AddUser(true)">Add Active User</Button>
|
||||
<Button Color="ButtonColor.Success" Outline @onclick="() => AddUser(false)">Add Inctive User</Button>
|
||||
<Button Color="ButtonColor.Success" @onclick="AddUser">Add User</Button>
|
||||
<Button Color="ButtonColor.Danger" @onclick="RevokeAllAccess">Revoke All</Button>
|
||||
<Button Color="ButtonColor.Warning" @onclick="Reset">Reset Data</Button>
|
||||
</p>
|
||||
@if (Status != null)
|
||||
{
|
||||
<h4>Users with access</h4>
|
||||
<h2>Users with access</h2>
|
||||
<Grid TItem="User" Data="Status.AccessUsers" Class="table table-bordered mt-3" AllowSorting>
|
||||
<GridColumns>
|
||||
<GridColumn TItem="User" HeaderText="Id" PropertyName="Id" SortKeySelector="item => item.Id">
|
||||
|
@ -46,7 +43,7 @@
|
|||
</GridColumn>
|
||||
</GridColumns>
|
||||
</Grid>
|
||||
<h4>Users in queue</h4>
|
||||
<h2>Users in queue</h2>
|
||||
<Grid TItem="User" Data="Status.QueuedUsers" Class="table table-bordered mt-3">
|
||||
<GridColumns>
|
||||
<GridColumn TItem="User" HeaderText="Id" PropertyName="Id">
|
||||
|
@ -67,7 +64,7 @@
|
|||
</GridColumn>
|
||||
</GridColumns>
|
||||
</Grid>
|
||||
<h4>Inactive users</h4>
|
||||
<h2>Inactive users</h2>
|
||||
<Grid TItem="User" Data="Status.InactiveUsers" Class="table table-bordered mt-3" AllowSorting>
|
||||
<GridColumns>
|
||||
<GridColumn TItem="User" HeaderText="Id" PropertyName="Id" SortKeySelector="item => item.Id">
|
||||
|
@ -83,7 +80,7 @@
|
|||
}
|
||||
|
||||
@code {
|
||||
public AccessQueueManagerStatus? Status;
|
||||
public AccessQueueStatus? Status;
|
||||
public AccessQueueConfig? Config;
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
|
@ -101,9 +98,9 @@
|
|||
});
|
||||
}
|
||||
|
||||
public void AddUser(bool isActive)
|
||||
public void AddUser()
|
||||
{
|
||||
Manager.AddUser(isActive);
|
||||
Manager.AddUser();
|
||||
Status = Manager.GetStatus();
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
namespace AccessQueuePlayground.Models
|
||||
{
|
||||
public class AccessQueueConfig
|
||||
{
|
||||
public int ActivitySeconds { get; set; }
|
||||
public int ExpirationSeconds { get; set; }
|
||||
public int CapacityLimit { get; set; }
|
||||
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace AccessQueuePlayground.Models
|
||||
{
|
||||
public class AccessQueueManagerStatus
|
||||
public class AccessQueueStatus
|
||||
{
|
||||
public List<User> AccessUsers { get; set; } = [];
|
||||
public List<User> QueuedUsers { get; set; } = [];
|
|
@ -19,21 +19,26 @@ builder.Host.UseSerilog((context, services, configuration) =>
|
|||
// Add services to the container.
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
if (string.IsNullOrEmpty(builder.Configuration["AccessQueuePlayground:ServiceUrl"]))
|
||||
{
|
||||
builder.Services.AddSingleton<IAccessService, AccessService>();
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Services.AddHttpClient();
|
||||
builder.Services.AddSingleton<IAccessService, AccessQueueApiService>();
|
||||
}
|
||||
builder.Services.AddSingleton<IAccessService, AccessService>();
|
||||
builder.Services.AddSingleton<IAccessQueueRepo, TakeANumberAccessQueueRepo>();
|
||||
builder.Services.AddSingleton<IAccessQueueManager, AccessQueueManager>();
|
||||
builder.Services.AddHostedService<AccessQueueBackgroundService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure TakeANumberAccessQueueRepo active users cache
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var config = scope.ServiceProvider.GetRequiredService<IConfiguration>();
|
||||
var repo = scope.ServiceProvider.GetRequiredService<IAccessQueueRepo>() as TakeANumberAccessQueueRepo;
|
||||
if (repo != null)
|
||||
{
|
||||
int activeSeconds = config.GetValue<int>("AccessQueue:ActivitySeconds", 2);
|
||||
int updateInterval = config.GetValue<int>("AccessQueue:ActiveUsersUpdateIntervalSeconds", 2);
|
||||
repo.ConfigureActiveUsersCache(activeSeconds, updateInterval);
|
||||
}
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using AccessQueueService.Models;
|
||||
using AccessQueueService.Services;
|
||||
|
||||
namespace AccessQueuePlayground.Services
|
||||
{
|
||||
public class AccessQueueApiService : IAccessService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly string _serviceUrl;
|
||||
|
||||
public AccessQueueStatus Status => throw new NotImplementedException();
|
||||
|
||||
public AccessQueueApiService(HttpClient httpClient, IConfiguration config)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_serviceUrl = config["AccessQueuePlayground:ServiceUrl"]?.TrimEnd('/') ?? "https://localhost:7291";
|
||||
}
|
||||
|
||||
public async Task<AccessResponse> RequestAccess(string userId)
|
||||
{
|
||||
return await _httpClient.GetFromJsonAsync<AccessResponse>($"{_serviceUrl}/access/{userId}");
|
||||
}
|
||||
|
||||
public async Task<bool> RevokeAccess(string userId)
|
||||
{
|
||||
var response = await _httpClient.DeleteAsync($"{_serviceUrl}/access/{userId}");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var result = await response.Content.ReadFromJsonAsync<bool>();
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Task<int> DeleteExpiredTickets()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AccessQueueConfig GetConfiguration()
|
||||
{
|
||||
return _httpClient.GetFromJsonAsync<AccessQueueConfig>($"{_serviceUrl}/config").Result;
|
||||
}
|
||||
|
||||
public void UpdateConfiguration(AccessQueueConfig config)
|
||||
{
|
||||
_ = _httpClient.PostAsJsonAsync($"{_serviceUrl}/config", config).Result;
|
||||
}
|
||||
|
||||
public void PatchConfiguration(AccessQueueConfig partialConfig)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ namespace AccessQueuePlayground.Services
|
|||
private readonly IAccessService _accessService;
|
||||
private readonly IConfiguration _config;
|
||||
private ConcurrentDictionary<Guid, User> _users;
|
||||
private AccessQueueManagerStatus _status;
|
||||
private AccessQueueStatus _status;
|
||||
public event Action? StatusUpdated;
|
||||
|
||||
private void NotifyStatusUpdated()
|
||||
|
@ -23,26 +23,26 @@ namespace AccessQueuePlayground.Services
|
|||
{
|
||||
_accessService = accessService;
|
||||
_users = new ConcurrentDictionary<Guid, User>();
|
||||
_status = new AccessQueueManagerStatus();
|
||||
_status = new AccessQueueStatus();
|
||||
_config = config;
|
||||
}
|
||||
|
||||
public AccessQueueManagerStatus GetStatus() => _status;
|
||||
public AccessQueueStatus GetStatus() => _status;
|
||||
|
||||
public AccessQueueConfig GetConfig() => _accessService.GetConfiguration();
|
||||
|
||||
public void UpdateConfig(AccessQueueConfig config)
|
||||
public AccessQueueConfig GetConfig() => new AccessQueueConfig
|
||||
{
|
||||
_accessService.UpdateConfiguration(config);
|
||||
}
|
||||
ActivitySeconds = _config.GetValue<int>("AccessQueue:ActivitySeconds"),
|
||||
ExpirationSeconds = _config.GetValue<int>("AccessQueue:ExpirationSeconds"),
|
||||
CapacityLimit = _config.GetValue<int>("AccessQueue:CapacityLimit")
|
||||
};
|
||||
|
||||
public Guid AddUser(bool isActive)
|
||||
public Guid AddUser()
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
_users[id] = new User
|
||||
{
|
||||
Id = id,
|
||||
Active = isActive,
|
||||
Active = false,
|
||||
};
|
||||
return id;
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ namespace AccessQueuePlayground.Services
|
|||
public async Task RecalculateStatus()
|
||||
{
|
||||
var userList = _users.Values.ToList();
|
||||
var newStatus = new AccessQueueManagerStatus();
|
||||
var newStatus = new AccessQueueStatus();
|
||||
foreach (var user in userList)
|
||||
{
|
||||
if (user.Active)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using AccessQueuePlayground.Models;
|
||||
using AccessQueueService.Models;
|
||||
|
||||
namespace AccessQueuePlayground.Services
|
||||
{
|
||||
|
@ -7,10 +6,9 @@ namespace AccessQueuePlayground.Services
|
|||
{
|
||||
public event Action? StatusUpdated;
|
||||
public AccessQueueConfig GetConfig();
|
||||
public void UpdateConfig(AccessQueueConfig config);
|
||||
public Task RecalculateStatus();
|
||||
public AccessQueueManagerStatus GetStatus();
|
||||
public Guid AddUser(bool isActive);
|
||||
public AccessQueueStatus GetStatus();
|
||||
public Guid AddUser();
|
||||
public void SetUserActive(Guid userId, bool isActive);
|
||||
public void RevokeAccess(Guid userId);
|
||||
public void RevokeAllAccess();
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,11 +9,11 @@
|
|||
"CapacityLimit": 3,
|
||||
"ActivitySeconds": 2,
|
||||
"ExpirationSeconds": 10,
|
||||
"RollingExpiration": true
|
||||
"RollingExpiration": true,
|
||||
"ActiveUsersUpdateIntervalSeconds": 2
|
||||
},
|
||||
"AccessQueuePlayground": {
|
||||
"RefreshRateMilliseconds": 200,
|
||||
"ServiceUrl": "https://localhost:7291/"
|
||||
"RefreshRateMilliseconds": 200
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
|
|
@ -1,21 +1,17 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="AccessQueueServiceTests" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
@AccessQueueService_HostAddress = http://localhost:5199
|
||||
|
||||
GET {{AccessQueueService_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
|
@ -1,31 +0,0 @@
|
|||
using AccessQueueService.Models;
|
||||
using AccessQueueService.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace AccessQueueService.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("config")]
|
||||
public class ConfigurationController : ControllerBase
|
||||
{
|
||||
private readonly IAccessService _accessService;
|
||||
|
||||
public ConfigurationController(IAccessService accessService)
|
||||
{
|
||||
_accessService = accessService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<AccessQueueConfig> GetConfiguration()
|
||||
{
|
||||
return Ok(_accessService.GetConfiguration());
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult UpdateConfiguration([FromBody] AccessQueueConfig config)
|
||||
{
|
||||
_accessService.PatchConfiguration(config);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
using AccessQueueService.Models;
|
||||
using AccessQueueService.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace AccessQueueService.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("status")]
|
||||
public class StatusController : ControllerBase
|
||||
{
|
||||
private readonly IAccessService _accessService;
|
||||
|
||||
public StatusController(IAccessService accessService)
|
||||
{
|
||||
_accessService = accessService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<AccessQueueStatus> GetStatus()
|
||||
{
|
||||
return Ok(_accessService.Status);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,10 @@
|
|||
using System.Runtime.Serialization;
|
||||
using AccessQueueService.Models;
|
||||
using AccessQueueService.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace AccessQueueService.Data
|
||||
{
|
||||
public interface IAccessQueueRepo
|
||||
{
|
||||
public string ToState();
|
||||
public int GetUnexpiredTicketsCount();
|
||||
public int GetActiveTicketsCount(DateTime activeCutoff);
|
||||
public int GetQueueCount();
|
||||
|
@ -16,6 +14,6 @@ namespace AccessQueueService.Data
|
|||
public void Enqueue(AccessTicket ticket);
|
||||
public int DeleteExpiredTickets();
|
||||
public bool RemoveUser(string userId);
|
||||
public bool DidDequeueUntilFull(AccessQueueConfig config);
|
||||
public bool DidDequeueUntilFull(int activeSeconds, int expirationSeconds, int capacityLimit);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,23 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text.Json;
|
||||
using AccessQueueService.Models;
|
||||
using AccessQueueService.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Threading;
|
||||
|
||||
namespace AccessQueueService.Data
|
||||
{
|
||||
public class TakeANumberAccessQueueRepo : IAccessQueueRepo
|
||||
{
|
||||
private ConcurrentDictionary<string, AccessTicket> _accessTickets = new();
|
||||
private ConcurrentDictionary<string, ulong> _queueNumbers = new();
|
||||
private ConcurrentDictionary<ulong, AccessTicket> _accessQueue = new();
|
||||
private readonly Dictionary<string, AccessTicket> _accessTickets = [];
|
||||
private readonly Dictionary<string, ulong> _queueNumbers = [];
|
||||
private readonly Dictionary<ulong, AccessTicket> _accessQueue = [];
|
||||
|
||||
internal ulong _nowServing = 0;
|
||||
internal ulong _nextUnusedTicket = 0;
|
||||
private ulong _nowServing = 0;
|
||||
private ulong _nextUnusedTicket = 0;
|
||||
|
||||
private int? _cachedActiveUsers = null;
|
||||
private DateTime _cachedActiveUsersTime = DateTime.MinValue;
|
||||
private int _cachedActiveUsers = 0;
|
||||
private int _activeSeconds = 60; // default, can be set from config
|
||||
private Timer? _activeUsersUpdateTimer;
|
||||
private int _activeUsersUpdateIntervalSeconds = 10; // default, can be set from config
|
||||
private readonly object _activeUsersLock = new();
|
||||
|
||||
public int GetUnexpiredTicketsCount() => _accessTickets.Count(t => t.Value.ExpiresOn > DateTime.UtcNow);
|
||||
public int GetActiveTicketsCount(DateTime activeCutoff) => _accessTickets
|
||||
|
@ -24,25 +25,20 @@ namespace AccessQueueService.Data
|
|||
public int GetQueueCount() => (int)(_nextUnusedTicket - _nowServing);
|
||||
public int GetRequestsAhead(string userId)
|
||||
{
|
||||
if (_queueNumbers.TryGetValue(userId, out var queueNumber))
|
||||
if(_queueNumbers.TryGetValue(userId, out var queueNumber))
|
||||
{
|
||||
if (_accessQueue.TryGetValue(queueNumber, out var ticket))
|
||||
if(_accessQueue.TryGetValue(queueNumber, out var ticket))
|
||||
{
|
||||
ticket.LastActive = DateTime.UtcNow;
|
||||
return queueNumber >= _nowServing ? (int)(queueNumber - _nowServing) : -1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void Enqueue(AccessTicket ticket)
|
||||
{
|
||||
if (_nextUnusedTicket >= long.MaxValue)
|
||||
{
|
||||
// Prevent overflow
|
||||
Optimize();
|
||||
}
|
||||
_queueNumbers[ticket.UserId] = _nextUnusedTicket;
|
||||
_accessQueue[_nextUnusedTicket] = ticket;
|
||||
_nextUnusedTicket++;
|
||||
|
@ -51,41 +47,34 @@ namespace AccessQueueService.Data
|
|||
public int DeleteExpiredTickets()
|
||||
{
|
||||
var cutoff = DateTime.UtcNow;
|
||||
var expiredTickets = _accessTickets.Where(t => t.Value.ExpiresOn < cutoff).ToList();
|
||||
var expiredTickets = _accessTickets.Where(t => t.Value.ExpiresOn < cutoff);
|
||||
int count = 0;
|
||||
foreach (var ticket in expiredTickets)
|
||||
{
|
||||
count++;
|
||||
_accessTickets.TryRemove(ticket.Key, out _);
|
||||
_accessTickets.Remove(ticket.Key);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private bool HasValidActiveUsersCache(AccessQueueConfig config, DateTime now)
|
||||
{
|
||||
return config.CacheMilliseconds.HasValue && _cachedActiveUsers.HasValue && (now - _cachedActiveUsersTime).TotalMilliseconds < config.CacheMilliseconds.Value;
|
||||
}
|
||||
|
||||
private int GetOpenSpots(AccessQueueConfig config, int activeUsers)
|
||||
{
|
||||
return config.CapacityLimit.Value - activeUsers;
|
||||
}
|
||||
|
||||
private int UpdateActiveUsersCache(DateTime now, DateTime activeCutoff)
|
||||
{
|
||||
_cachedActiveUsers = _accessTickets.Count(t => t.Value.ExpiresOn > now && t.Value.LastActive > activeCutoff);
|
||||
_cachedActiveUsersTime = now;
|
||||
return _cachedActiveUsers.GetValueOrDefault();
|
||||
}
|
||||
|
||||
private int DequeueUsersUntilFull(int openSpots, DateTime now, DateTime activeCutoff, AccessQueueConfig config)
|
||||
public bool DidDequeueUntilFull(int activeSeconds, int expirationSeconds, int capacityLimit)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var activeCutoff = now.AddSeconds(-activeSeconds);
|
||||
// Use cached value instead of recalculating
|
||||
var numberOfActiveUsers = GetCachedActiveUsers();
|
||||
var openSpots = capacityLimit - numberOfActiveUsers;
|
||||
if(openSpots <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
int filledSpots = 0;
|
||||
while (filledSpots < openSpots && _nowServing < _nextUnusedTicket)
|
||||
{
|
||||
if (_accessQueue.TryRemove(_nowServing++, out var nextUser))
|
||||
if (_accessQueue.TryGetValue(_nowServing, out var nextUser))
|
||||
{
|
||||
_queueNumbers.TryRemove(nextUser.UserId, out _);
|
||||
_accessQueue.Remove(_nowServing);
|
||||
_queueNumbers.Remove(nextUser.UserId);
|
||||
if (nextUser.LastActive < activeCutoff)
|
||||
{
|
||||
// User is inactive, throw away their ticket
|
||||
|
@ -94,47 +83,12 @@ namespace AccessQueueService.Data
|
|||
_accessTickets[nextUser.UserId] = new AccessTicket
|
||||
{
|
||||
UserId = nextUser.UserId,
|
||||
ExpiresOn = now.AddSeconds(config.ExpirationSeconds ?? 0),
|
||||
ExpiresOn = now.AddSeconds(expirationSeconds),
|
||||
LastActive = now
|
||||
};
|
||||
filledSpots++;
|
||||
}
|
||||
}
|
||||
return filledSpots;
|
||||
}
|
||||
|
||||
public bool DidDequeueUntilFull(AccessQueueConfig config)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
if (!config.ActivitySeconds.HasValue || !config.ExpirationSeconds.HasValue || !config.CapacityLimit.HasValue)
|
||||
{
|
||||
throw new Exception("Required config values are not defined.");
|
||||
}
|
||||
var activeCutoff = now.AddSeconds(-config.ActivitySeconds.Value);
|
||||
|
||||
int activeUsers;
|
||||
if (HasValidActiveUsersCache(config, now))
|
||||
{
|
||||
activeUsers = _cachedActiveUsers.GetValueOrDefault();
|
||||
}
|
||||
else
|
||||
{
|
||||
activeUsers = UpdateActiveUsersCache(now, activeCutoff);
|
||||
}
|
||||
|
||||
var openSpots = GetOpenSpots(config, activeUsers);
|
||||
if (openSpots <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int filledSpots = DequeueUsersUntilFull(openSpots, now, activeCutoff, config);
|
||||
|
||||
// Invalidate cache if any users were granted access
|
||||
if (filledSpots > 0)
|
||||
{
|
||||
_cachedActiveUsers = null;
|
||||
_cachedActiveUsersTime = DateTime.MinValue;
|
||||
_nowServing++;
|
||||
}
|
||||
return filledSpots == openSpots;
|
||||
}
|
||||
|
@ -147,81 +101,44 @@ namespace AccessQueueService.Data
|
|||
public void UpsertTicket(AccessTicket ticket)
|
||||
{
|
||||
_accessTickets[ticket.UserId] = ticket;
|
||||
_cachedActiveUsers = null;
|
||||
_cachedActiveUsersTime = DateTime.MinValue;
|
||||
}
|
||||
|
||||
public bool RemoveUser(string userId)
|
||||
{
|
||||
if (_queueNumbers.TryRemove(userId, out var queueNumber))
|
||||
if(_queueNumbers.TryGetValue(userId, out var queueNumber))
|
||||
{
|
||||
_accessQueue.TryRemove(queueNumber, out _);
|
||||
_accessQueue.Remove(queueNumber);
|
||||
_queueNumbers.Remove(userId);
|
||||
}
|
||||
return _accessTickets.TryRemove(userId, out _);
|
||||
return _accessTickets.Remove(userId);
|
||||
}
|
||||
|
||||
internal void Optimize()
|
||||
public void ConfigureActiveUsersCache(int activeSeconds, int updateIntervalSeconds)
|
||||
{
|
||||
var newQueue = new ConcurrentDictionary<ulong, AccessTicket>();
|
||||
var newQueueNumbers = new ConcurrentDictionary<string, ulong>();
|
||||
ulong newIndex = 0;
|
||||
for (ulong i = _nowServing; i < _nextUnusedTicket; i++)
|
||||
{
|
||||
if (_accessQueue.TryGetValue(i, out var user))
|
||||
{
|
||||
newQueue[newIndex] = user;
|
||||
newQueueNumbers[user.UserId] = newIndex++;
|
||||
}
|
||||
}
|
||||
_accessQueue = newQueue;
|
||||
_queueNumbers = newQueueNumbers;
|
||||
_nowServing = 0;
|
||||
_nextUnusedTicket = newIndex;
|
||||
_activeSeconds = activeSeconds;
|
||||
_activeUsersUpdateIntervalSeconds = updateIntervalSeconds;
|
||||
_activeUsersUpdateTimer?.Dispose();
|
||||
_activeUsersUpdateTimer = new Timer(_ => UpdateActiveUsersCache(), null, 0, _activeUsersUpdateIntervalSeconds * 1000);
|
||||
}
|
||||
|
||||
public string ToState()
|
||||
private void UpdateActiveUsersCache()
|
||||
{
|
||||
var state = new TakeANumberAccessQueueRepoState
|
||||
var now = DateTime.UtcNow;
|
||||
var activeCutoff = now.AddSeconds(-_activeSeconds);
|
||||
int count = 0;
|
||||
lock (_activeUsersLock)
|
||||
{
|
||||
AccessTickets = new Dictionary<string, AccessTicket>(_accessTickets),
|
||||
AccessQueue = new Dictionary<ulong, AccessTicket>(_accessQueue),
|
||||
};
|
||||
return JsonSerializer.Serialize(state);
|
||||
count = _accessTickets.Count(t => t.Value.ExpiresOn > now && t.Value.LastActive > activeCutoff);
|
||||
_cachedActiveUsers = count;
|
||||
}
|
||||
}
|
||||
|
||||
public static TakeANumberAccessQueueRepo FromState(string stateJson)
|
||||
public int GetCachedActiveUsers()
|
||||
{
|
||||
var state = JsonSerializer.Deserialize<TakeANumberAccessQueueRepoState?>(stateJson);
|
||||
if (state?.AccessTickets == null || state?.AccessQueue == null)
|
||||
lock (_activeUsersLock)
|
||||
{
|
||||
return new();
|
||||
return _cachedActiveUsers;
|
||||
}
|
||||
|
||||
var _accessTickets = new ConcurrentDictionary<string, AccessTicket>(state.AccessTickets);
|
||||
var _accessQueue = new ConcurrentDictionary<ulong, AccessTicket>(state.AccessQueue);
|
||||
var _nextUnusedTicket = 0ul;
|
||||
var _nowServing = ulong.MaxValue;
|
||||
var _queueNumbers = new ConcurrentDictionary<string, ulong>();
|
||||
foreach (var queueItem in state.AccessQueue)
|
||||
{
|
||||
_queueNumbers[queueItem.Value.UserId] = queueItem.Key;
|
||||
_nextUnusedTicket = Math.Max(_nextUnusedTicket, queueItem.Key + 1);
|
||||
_nowServing = Math.Min(_nowServing, queueItem.Key);
|
||||
}
|
||||
|
||||
if (_nowServing == ulong.MaxValue)
|
||||
{
|
||||
_nowServing = 0;
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
_accessQueue = _accessQueue,
|
||||
_accessTickets = _accessTickets,
|
||||
_nextUnusedTicket = _nextUnusedTicket,
|
||||
_nowServing = _nowServing,
|
||||
_queueNumbers = _queueNumbers
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
namespace AccessQueueService.Models
|
||||
{
|
||||
public class AccessQueueConfig
|
||||
{
|
||||
public int? CapacityLimit { get; set; }
|
||||
public int? ActivitySeconds { get; set; }
|
||||
public int? ExpirationSeconds { get; set; }
|
||||
public bool? RollingExpiration { get; set; }
|
||||
public int? CacheMilliseconds { get; set; }
|
||||
|
||||
public AccessQueueConfig Clone()
|
||||
{
|
||||
return (AccessQueueConfig)this.MemberwiseClone();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
using AccessQueueService.Data;
|
||||
|
||||
namespace AccessQueueService.Models
|
||||
{
|
||||
public class AccessQueueStatus
|
||||
{
|
||||
public int UnexpiredTicketsCount { get; internal set; }
|
||||
public int ActiveTicketsCount { get; internal set; }
|
||||
public int QueueCount { get; internal set; }
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace AccessQueueService.Models
|
||||
{
|
||||
public class TakeANumberAccessQueueRepoState
|
||||
{
|
||||
public Dictionary<string, AccessTicket> AccessTickets { get; set; } = [];
|
||||
public Dictionary<ulong, AccessTicket> AccessQueue { get; set; } = [];
|
||||
}
|
||||
}
|
|
@ -17,28 +17,24 @@ builder.Services.AddControllers();
|
|||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSingleton<IAccessService, AccessService>();
|
||||
builder.Services.AddSingleton<IAccessQueueRepo>(sp =>
|
||||
{
|
||||
string? filePath = builder.Configuration.GetValue<string>("AccessQueue:BackupFilePath");
|
||||
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
return TakeANumberAccessQueueRepo.FromState(json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to load state from {filePath}. Error message: {ex.Message}");
|
||||
}
|
||||
}
|
||||
return new TakeANumberAccessQueueRepo();
|
||||
});
|
||||
builder.Services.AddSingleton<IAccessQueueRepo, TakeANumberAccessQueueRepo>();
|
||||
builder.Services.AddHostedService<AccessCleanupBackgroundService>();
|
||||
builder.Services.AddHostedService<AccessQueueSerializerService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure TakeANumberAccessQueueRepo active users cache
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var config = scope.ServiceProvider.GetRequiredService<IConfiguration>();
|
||||
var repo = scope.ServiceProvider.GetRequiredService<IAccessQueueRepo>() as TakeANumberAccessQueueRepo;
|
||||
if (repo != null)
|
||||
{
|
||||
int activeSeconds = config.GetValue<int>("AccessQueue:ActivitySeconds", 900);
|
||||
int updateInterval = config.GetValue<int>("AccessQueue:ActiveUsersUpdateIntervalSeconds", 10);
|
||||
repo.ConfigureActiveUsersCache(activeSeconds, updateInterval);
|
||||
}
|
||||
}
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
using System.Text.Json;
|
||||
using AccessQueueService.Data;
|
||||
|
||||
namespace AccessQueueService.Services
|
||||
{
|
||||
public class AccessQueueSerializerService : BackgroundService
|
||||
{
|
||||
private readonly IAccessQueueRepo _accessRepo;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly ILogger<AccessQueueSerializerService> _logger;
|
||||
|
||||
public AccessQueueSerializerService(IAccessQueueRepo accessRepo, IConfiguration config, ILogger<AccessQueueSerializerService> logger)
|
||||
{
|
||||
_accessRepo = accessRepo;
|
||||
_config = config;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var backupIntervalSeconds = _config.GetValue<int>("AccessQueue:BackupIntervalSeconds") * 1000;
|
||||
var backupPath = _config.GetValue<string>("AccessQueue:BackupFilePath");
|
||||
if (backupIntervalSeconds == 0 || string.IsNullOrEmpty(backupPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"Writing backup to {backupPath}");
|
||||
var stateJson = _accessRepo.ToState();
|
||||
File.WriteAllText(backupPath, stateJson);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception occurred while trying to write state in background backup service.");
|
||||
}
|
||||
await Task.Delay(backupIntervalSeconds, stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -12,57 +12,37 @@ namespace AccessQueueService.Services
|
|||
private readonly ILogger<AccessService> _logger;
|
||||
|
||||
private readonly SemaphoreSlim _queueLock = new(1, 1);
|
||||
private AccessQueueConfig _config;
|
||||
private readonly int EXP_SECONDS;
|
||||
private readonly int ACT_SECONDS;
|
||||
private readonly int CAPACITY_LIMIT;
|
||||
private readonly bool ROLLING_EXPIRATION;
|
||||
public AccessService(IConfiguration configuration, IAccessQueueRepo accessQueueRepo, ILogger<AccessService> logger)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_accessQueueRepo = accessQueueRepo;
|
||||
_logger = logger;
|
||||
_config = new AccessQueueConfig
|
||||
{
|
||||
ExpirationSeconds = _configuration.GetValue<int>("AccessQueue:ExpirationSeconds"),
|
||||
ActivitySeconds = _configuration.GetValue<int>("AccessQueue:ActivitySeconds"),
|
||||
CapacityLimit = _configuration.GetValue<int>("AccessQueue:CapacityLimit"),
|
||||
RollingExpiration = _configuration.GetValue<bool>("AccessQueue:RollingExpiration"),
|
||||
CacheMilliseconds = _configuration.GetValue<int>("AccessQueue:CacheMilliseconds")
|
||||
};
|
||||
}
|
||||
public AccessQueueConfig GetConfiguration() => _config.Clone();
|
||||
public void UpdateConfiguration(AccessQueueConfig config)
|
||||
{
|
||||
_config = config.Clone();
|
||||
}
|
||||
public void PatchConfiguration(AccessQueueConfig partialConfig)
|
||||
{
|
||||
if (partialConfig.CapacityLimit.HasValue) _config.CapacityLimit = partialConfig.CapacityLimit.Value;
|
||||
if (partialConfig.ActivitySeconds.HasValue) _config.ActivitySeconds = partialConfig.ActivitySeconds.Value;
|
||||
if (partialConfig.ExpirationSeconds.HasValue) _config.ExpirationSeconds = partialConfig.ExpirationSeconds.Value;
|
||||
if (partialConfig.RollingExpiration.HasValue) _config.RollingExpiration = partialConfig.RollingExpiration.Value;
|
||||
EXP_SECONDS = _configuration.GetValue<int>("AccessQueue:ExpirationSeconds");
|
||||
ACT_SECONDS = _configuration.GetValue<int>("AccessQueue:ActivitySeconds");
|
||||
CAPACITY_LIMIT = _configuration.GetValue<int>("AccessQueue:CapacityLimit");
|
||||
ROLLING_EXPIRATION = _configuration.GetValue<bool>("AccessQueue:RollingExpiration");
|
||||
}
|
||||
public int UnexpiredTicketsCount => _accessQueueRepo.GetUnexpiredTicketsCount();
|
||||
public int ActiveTicketsCount => _accessQueueRepo.GetActiveTicketsCount(DateTime.UtcNow.AddSeconds(-_config.ActivitySeconds.Value));
|
||||
public int ActiveTicketsCount => _accessQueueRepo.GetActiveTicketsCount(DateTime.UtcNow.AddSeconds(-_configuration.GetValue<int>("AccessQueue:ActivitySeconds")));
|
||||
public int QueueCount => _accessQueueRepo.GetQueueCount();
|
||||
public AccessQueueStatus Status => new()
|
||||
{
|
||||
ActiveTicketsCount = this.ActiveTicketsCount,
|
||||
QueueCount = this.QueueCount,
|
||||
UnexpiredTicketsCount = this.UnexpiredTicketsCount,
|
||||
};
|
||||
|
||||
public async Task<AccessResponse> RequestAccess(string userId)
|
||||
{
|
||||
await _queueLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var hasCapacity = !_accessQueueRepo.DidDequeueUntilFull(_config);
|
||||
var hasCapacity = !_accessQueueRepo.DidDequeueUntilFull(ACT_SECONDS, EXP_SECONDS, CAPACITY_LIMIT);
|
||||
var existingTicket = _accessQueueRepo.GetTicket(userId);
|
||||
if (existingTicket != null && existingTicket.ExpiresOn > DateTime.UtcNow)
|
||||
{
|
||||
// Already has access
|
||||
var expiresOn = existingTicket.ExpiresOn;
|
||||
if (_config.RollingExpiration.Value)
|
||||
if (ROLLING_EXPIRATION)
|
||||
{
|
||||
expiresOn = DateTime.UtcNow.AddSeconds(_config.ExpirationSeconds.Value);
|
||||
expiresOn = DateTime.UtcNow.AddSeconds(EXP_SECONDS);
|
||||
}
|
||||
_accessQueueRepo.UpsertTicket(new AccessTicket
|
||||
{
|
||||
|
@ -82,7 +62,7 @@ namespace AccessQueueService.Services
|
|||
var accessTicket = new AccessTicket
|
||||
{
|
||||
UserId = userId,
|
||||
ExpiresOn = DateTime.UtcNow.AddSeconds(_config.ExpirationSeconds.Value),
|
||||
ExpiresOn = DateTime.UtcNow.AddSeconds(EXP_SECONDS),
|
||||
LastActive = DateTime.UtcNow
|
||||
};
|
||||
_accessQueueRepo.UpsertTicket(accessTicket);
|
||||
|
@ -107,10 +87,6 @@ namespace AccessQueueService.Services
|
|||
});
|
||||
_logger.LogInformation("User {UserId} added to queue. Requests ahead: {RequestsAhead}.", userId, requestsAhead);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("User {UserId} already in queue. Requests ahead: {RequestsAhead}.", userId, requestsAhead);
|
||||
}
|
||||
return new AccessResponse
|
||||
{
|
||||
ExpiresOn = null,
|
||||
|
|
|
@ -7,9 +7,5 @@ namespace AccessQueueService.Services
|
|||
public Task<AccessResponse> RequestAccess(string userId);
|
||||
public Task<bool> RevokeAccess(string userId);
|
||||
public Task<int> DeleteExpiredTickets();
|
||||
public AccessQueueConfig GetConfiguration();
|
||||
public void UpdateConfiguration(AccessQueueConfig config);
|
||||
public void PatchConfiguration(AccessQueueConfig partialConfig);
|
||||
public AccessQueueStatus Status { get; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,9 +29,7 @@
|
|||
"ExpirationSeconds": 43200,
|
||||
"RollingExpiration": true,
|
||||
"CleanupIntervalSeconds": 60,
|
||||
"BackupFilePath": "Logs\\backup.json",
|
||||
"BackupIntervalSeconds": 5,
|
||||
"CacheMilliseconds": 1000
|
||||
"ActiveUsersUpdateIntervalSeconds": 10
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using AccessQueueService.Data;
|
||||
using AccessQueueService.Models;
|
||||
|
@ -15,46 +13,16 @@ namespace AccessQueueServiceTests
|
|||
public class AccessQueueRepoTests
|
||||
{
|
||||
private readonly TakeANumberAccessQueueRepo _repo;
|
||||
private readonly AccessQueueConfig _simpleConfig = new()
|
||||
{
|
||||
ExpirationSeconds = 60,
|
||||
ActivitySeconds = 60,
|
||||
CapacityLimit = 1,
|
||||
RollingExpiration = false,
|
||||
CacheMilliseconds = null
|
||||
};
|
||||
private readonly AccessQueueConfig _configWithCache = new()
|
||||
{
|
||||
ExpirationSeconds = 60,
|
||||
ActivitySeconds = 60,
|
||||
CapacityLimit = 1,
|
||||
RollingExpiration = false,
|
||||
CacheMilliseconds = 100
|
||||
};
|
||||
private AccessQueueConfig SimpleConfigWithCapacity(int capacity) => new()
|
||||
{
|
||||
ExpirationSeconds = 60,
|
||||
ActivitySeconds = 60,
|
||||
CapacityLimit = capacity,
|
||||
RollingExpiration = false,
|
||||
CacheMilliseconds = null
|
||||
};
|
||||
|
||||
public AccessQueueRepoTests()
|
||||
{
|
||||
_repo = new TakeANumberAccessQueueRepo();
|
||||
}
|
||||
|
||||
private List<AccessTicket> GetSimpleTicketList() => new()
|
||||
{
|
||||
new() { UserId = "first", ExpiresOn = DateTime.UtcNow, LastActive = DateTime.UtcNow },
|
||||
new() { UserId = "second", ExpiresOn = DateTime.UtcNow, LastActive = DateTime.UtcNow },
|
||||
new() { UserId = "third", ExpiresOn = DateTime.UtcNow, LastActive = DateTime.UtcNow }
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void GetUnexpiredTicketsCount_ReturnsCorrectCount()
|
||||
{
|
||||
|
||||
_repo.UpsertTicket(new AccessTicket { UserId = "a", ExpiresOn = DateTime.UtcNow.AddMinutes(1), LastActive = DateTime.UtcNow });
|
||||
_repo.UpsertTicket(new AccessTicket { UserId = "b", ExpiresOn = DateTime.UtcNow.AddMinutes(-1), LastActive = DateTime.UtcNow });
|
||||
Assert.Equal(1, _repo.GetUnexpiredTicketsCount());
|
||||
|
@ -123,7 +91,7 @@ namespace AccessQueueServiceTests
|
|||
|
||||
var ticket = new AccessTicket { UserId = "a", ExpiresOn = DateTime.UtcNow.AddMinutes(1), LastActive = DateTime.UtcNow };
|
||||
_repo.Enqueue(ticket);
|
||||
bool result = _repo.DidDequeueUntilFull(_simpleConfig);
|
||||
bool result = _repo.DidDequeueUntilFull(60, 60, 1);
|
||||
Assert.True(result);
|
||||
Assert.NotNull(_repo.GetTicket("a"));
|
||||
}
|
||||
|
@ -133,7 +101,7 @@ namespace AccessQueueServiceTests
|
|||
{
|
||||
|
||||
_repo.UpsertTicket(new AccessTicket { UserId = "a", ExpiresOn = DateTime.UtcNow.AddMinutes(1), LastActive = DateTime.UtcNow });
|
||||
bool result = _repo.DidDequeueUntilFull(SimpleConfigWithCapacity(0));
|
||||
bool result = _repo.DidDequeueUntilFull(60, 60, 0);
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
|
@ -187,7 +155,7 @@ namespace AccessQueueServiceTests
|
|||
var active = new AccessTicket { UserId = "active", ExpiresOn = DateTime.UtcNow.AddMinutes(1), LastActive = DateTime.UtcNow };
|
||||
_repo.Enqueue(inactive);
|
||||
_repo.Enqueue(active);
|
||||
bool result = _repo.DidDequeueUntilFull(_simpleConfig);
|
||||
bool result = _repo.DidDequeueUntilFull(5 * 60, 60, 1);
|
||||
Assert.True(result);
|
||||
Assert.Null(_repo.GetTicket("inactive"));
|
||||
Assert.NotNull(_repo.GetTicket("active"));
|
||||
|
@ -196,10 +164,12 @@ namespace AccessQueueServiceTests
|
|||
[Fact]
|
||||
public void Enqueue_QueuesUsersInOrder()
|
||||
{
|
||||
foreach (var ticket in GetSimpleTicketList())
|
||||
{
|
||||
_repo.Enqueue(ticket);
|
||||
}
|
||||
var ticket1 = new AccessTicket { UserId = "first", ExpiresOn = DateTime.UtcNow, LastActive = DateTime.UtcNow };
|
||||
var ticket2 = new AccessTicket { UserId = "second", ExpiresOn = DateTime.UtcNow, LastActive = DateTime.UtcNow };
|
||||
var ticket3 = new AccessTicket { UserId = "third", ExpiresOn = DateTime.UtcNow, LastActive = DateTime.UtcNow };
|
||||
_repo.Enqueue(ticket1);
|
||||
_repo.Enqueue(ticket2);
|
||||
_repo.Enqueue(ticket3);
|
||||
Assert.Equal(0, _repo.GetRequestsAhead("first"));
|
||||
Assert.Equal(1, _repo.GetRequestsAhead("second"));
|
||||
Assert.Equal(2, _repo.GetRequestsAhead("third"));
|
||||
|
@ -208,119 +178,19 @@ namespace AccessQueueServiceTests
|
|||
[Fact]
|
||||
public void DidDequeueUntilFull_DequeuesUsersInOrder()
|
||||
{
|
||||
foreach (var ticket in GetSimpleTicketList())
|
||||
{
|
||||
_repo.Enqueue(ticket);
|
||||
}
|
||||
var ticket1 = new AccessTicket { UserId = "first", ExpiresOn = DateTime.UtcNow.AddMinutes(1), LastActive = DateTime.UtcNow };
|
||||
var ticket2 = new AccessTicket { UserId = "second", ExpiresOn = DateTime.UtcNow.AddMinutes(1), LastActive = DateTime.UtcNow };
|
||||
var ticket3 = new AccessTicket { UserId = "third", ExpiresOn = DateTime.UtcNow.AddMinutes(1), LastActive = DateTime.UtcNow };
|
||||
_repo.Enqueue(ticket1);
|
||||
_repo.Enqueue(ticket2);
|
||||
_repo.Enqueue(ticket3);
|
||||
|
||||
bool result = _repo.DidDequeueUntilFull(_simpleConfig);
|
||||
bool result = _repo.DidDequeueUntilFull(60 * 60, 60, 1);
|
||||
|
||||
Assert.True(result);
|
||||
Assert.NotNull(_repo.GetTicket("first"));
|
||||
Assert.Null(_repo.GetTicket("second"));
|
||||
Assert.Null(_repo.GetTicket("third"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DidDequeuedUntilFull_CachesActiveTickets_WhenCacheMillisSet()
|
||||
{
|
||||
foreach (var ticket in GetSimpleTicketList())
|
||||
{
|
||||
_repo.Enqueue(ticket);
|
||||
}
|
||||
|
||||
Assert.True(_repo.DidDequeueUntilFull(_configWithCache));
|
||||
Assert.NotNull(_repo.GetTicket("first"));
|
||||
Assert.Null(_repo.GetTicket("second"));
|
||||
Assert.Null(_repo.GetTicket("third"));
|
||||
|
||||
Assert.True(_repo.DidDequeueUntilFull(_configWithCache));
|
||||
Assert.Null(_repo.GetTicket("second"));
|
||||
Assert.Null(_repo.GetTicket("third"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Optimize_MaintainsQueueOrder()
|
||||
{
|
||||
foreach (var ticket in GetSimpleTicketList())
|
||||
{
|
||||
_repo.Enqueue(ticket);
|
||||
}
|
||||
|
||||
_repo.DidDequeueUntilFull(_simpleConfig);
|
||||
_repo.Optimize();
|
||||
|
||||
Assert.NotNull(_repo.GetTicket("first"));
|
||||
Assert.Equal(0, _repo.GetRequestsAhead("second"));
|
||||
Assert.Equal(1, _repo.GetRequestsAhead("third"));
|
||||
Assert.Equal(0ul, _repo._nowServing);
|
||||
|
||||
_repo.DidDequeueUntilFull(SimpleConfigWithCapacity(2));
|
||||
Assert.NotNull(_repo.GetTicket("second"));
|
||||
Assert.Equal(0, _repo.GetRequestsAhead("third"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Enqueue_MaintainsQueueOrder_WhenMaxValueExceeded()
|
||||
{
|
||||
_repo._nowServing = long.MaxValue - 1;
|
||||
_repo._nextUnusedTicket = long.MaxValue - 1;
|
||||
|
||||
foreach (var ticket in GetSimpleTicketList())
|
||||
{
|
||||
_repo.Enqueue(ticket);
|
||||
}
|
||||
|
||||
Assert.Equal(0ul, _repo._nowServing);
|
||||
Assert.Equal(3ul, _repo._nextUnusedTicket);
|
||||
|
||||
Assert.Equal(0, _repo.GetRequestsAhead("first"));
|
||||
Assert.Equal(2, _repo.GetRequestsAhead("third"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToState_ReturnsAccurateJson()
|
||||
{
|
||||
var ticketWithAccess = new AccessTicket { UserId = "access", ExpiresOn = DateTime.UtcNow.AddMinutes(1), LastActive = DateTime.UtcNow };
|
||||
var ticketWithoutAccess = new AccessTicket { UserId = "noAccess", LastActive = DateTime.UtcNow };
|
||||
|
||||
_repo.UpsertTicket(ticketWithAccess);
|
||||
_repo.Enqueue(ticketWithoutAccess);
|
||||
|
||||
string stateJson = _repo.ToState();
|
||||
var state = JsonSerializer.Deserialize<TakeANumberAccessQueueRepoState>(stateJson);
|
||||
|
||||
Assert.NotNull(state?.AccessQueue);
|
||||
Assert.NotNull(state?.AccessTickets);
|
||||
Assert.Single(state!.AccessTickets);
|
||||
Assert.Single(state!.AccessQueue);
|
||||
|
||||
Assert.Equal(ticketWithAccess.UserId, state.AccessTickets.First().Key);
|
||||
Assert.Equal(ticketWithAccess.ExpiresOn, state.AccessTickets.First().Value.ExpiresOn);
|
||||
Assert.Equal(ticketWithAccess.LastActive, state.AccessTickets.First().Value.LastActive);
|
||||
|
||||
Assert.Equal(ticketWithoutAccess.UserId, state.AccessQueue.First().Value.UserId);
|
||||
Assert.Equal(ticketWithoutAccess.LastActive, state.AccessQueue.First().Value.LastActive);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FromState_DeserializesJsonCorrectly()
|
||||
{
|
||||
var ticketWithAccess = new AccessTicket { UserId = "access", ExpiresOn = DateTime.UtcNow.AddMinutes(1), LastActive = DateTime.UtcNow };
|
||||
var ticketWithoutAccess = new AccessTicket { UserId = "noAccess", LastActive = DateTime.UtcNow };
|
||||
|
||||
_repo.UpsertTicket(ticketWithAccess);
|
||||
_repo.Enqueue(ticketWithoutAccess);
|
||||
|
||||
string stateJson = _repo.ToState();
|
||||
var deserializedRepo = TakeANumberAccessQueueRepo.FromState(stateJson);
|
||||
|
||||
Assert.Equal(1, deserializedRepo.GetUnexpiredTicketsCount());
|
||||
Assert.Equal(1, deserializedRepo.GetQueueCount());
|
||||
|
||||
Assert.Equal(deserializedRepo.GetTicket("access")!.ExpiresOn, ticketWithAccess.ExpiresOn);
|
||||
Assert.Null(deserializedRepo.GetTicket("noAccess"));
|
||||
Assert.Equal(0, deserializedRepo.GetRequestsAhead("noAccess"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,8 +15,7 @@ namespace AccessQueueServiceTests
|
|||
const int ACT_SECONDS = 1;
|
||||
const int ACT_MILLIS = 1000 * ACT_SECONDS;
|
||||
const int CAP_LIMIT = 5;
|
||||
const int BULK_COUNT = 10000;
|
||||
const int CACHE_MILLIS = 1000;
|
||||
const int BULK_COUNT = 50000;
|
||||
private readonly AccessService _accessService;
|
||||
|
||||
public AccessServiceTests()
|
||||
|
@ -26,8 +25,7 @@ namespace AccessQueueServiceTests
|
|||
{ "AccessQueue:ExpirationSeconds", $"{EXP_SECONDS}" },
|
||||
{ "AccessQueue:ActivitySeconds", $"{ACT_SECONDS}" },
|
||||
{ "AccessQueue:CapacityLimit", $"{CAP_LIMIT}" },
|
||||
{ "AccessQueue:RollingExpiration", "true" },
|
||||
{ "AccessQueue:CacheMilliseconds", $"{CACHE_MILLIS}" }
|
||||
{ "AccessQueue:RollingExpiration", "true" }
|
||||
};
|
||||
|
||||
var configuration = new ConfigurationBuilder()
|
||||
|
@ -39,14 +37,6 @@ namespace AccessQueueServiceTests
|
|||
_accessService = new AccessService(configuration, accessQueueRepo, logger);
|
||||
}
|
||||
|
||||
private void FillSlots(int usersToAdd = CAP_LIMIT)
|
||||
{
|
||||
for (int i = 0; i < usersToAdd; i++)
|
||||
{
|
||||
_ = _accessService.RequestAccess(Guid.NewGuid().ToString());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RequestAccess_ShouldGrantAccess_WhenCapacityIsAvailable()
|
||||
{
|
||||
|
@ -81,7 +71,10 @@ namespace AccessQueueServiceTests
|
|||
[Fact]
|
||||
public async Task RequestAccess_ShouldQueueUser_WhenCapacityIsFull()
|
||||
{
|
||||
FillSlots(CAP_LIMIT * 2);
|
||||
for (int i = 0; i < CAP_LIMIT * 2; i++) // Fill double capacity
|
||||
{
|
||||
await _accessService.RequestAccess(Guid.NewGuid().ToString());
|
||||
}
|
||||
var userId = "user";
|
||||
|
||||
var response = await _accessService.RequestAccess(userId);
|
||||
|
@ -89,9 +82,9 @@ namespace AccessQueueServiceTests
|
|||
Assert.NotNull(response);
|
||||
Assert.Null(response.ExpiresOn);
|
||||
Assert.True(response.RequestsAhead == CAP_LIMIT);
|
||||
Assert.Equal(CAP_LIMIT, _accessService.UnexpiredTicketsCount);
|
||||
Assert.Equal(CAP_LIMIT, _accessService.ActiveTicketsCount);
|
||||
Assert.Equal(CAP_LIMIT + 1, _accessService.QueueCount);
|
||||
Assert.Equal(5, _accessService.UnexpiredTicketsCount);
|
||||
Assert.Equal(5, _accessService.ActiveTicketsCount);
|
||||
Assert.Equal(6, _accessService.QueueCount);
|
||||
}
|
||||
|
||||
|
||||
|
@ -122,7 +115,10 @@ namespace AccessQueueServiceTests
|
|||
var userId = "user";
|
||||
await _accessService.RequestAccess(userId);
|
||||
|
||||
FillSlots();
|
||||
for (int i = 0; i < CAP_LIMIT; i++) // Fill remaining slots
|
||||
{
|
||||
await _accessService.RequestAccess(Guid.NewGuid().ToString());
|
||||
}
|
||||
|
||||
var response = await _accessService.RequestAccess(userId); // Request access before revoking
|
||||
Assert.NotNull(response);
|
||||
|
@ -137,7 +133,10 @@ namespace AccessQueueServiceTests
|
|||
[Fact]
|
||||
public async Task RequestAccess_ShouldNotQueueUser_WhenMultipleRequestsForOtherUsersMade()
|
||||
{
|
||||
FillSlots();
|
||||
for (int i = 0; i < CAP_LIMIT; i++) // Fill slots without awaiting
|
||||
{
|
||||
_ = _accessService.RequestAccess(Guid.NewGuid().ToString());
|
||||
}
|
||||
var response = await _accessService.RequestAccess(Guid.NewGuid().ToString()); // Request access before revoking
|
||||
Assert.NotNull(response);
|
||||
Assert.False(response.HasAccess);
|
||||
|
@ -156,7 +155,10 @@ namespace AccessQueueServiceTests
|
|||
[Fact]
|
||||
public async Task RequestAccess_ShouldGrantAccess_WhenUsersWithAccessInactive()
|
||||
{
|
||||
FillSlots();
|
||||
for (int i = 0; i < CAP_LIMIT; i++)
|
||||
{
|
||||
await _accessService.RequestAccess(Guid.NewGuid().ToString());
|
||||
}
|
||||
var userId = "user";
|
||||
var response = await _accessService.RequestAccess(userId);
|
||||
Assert.False(response.HasAccess);
|
||||
|
@ -172,7 +174,10 @@ namespace AccessQueueServiceTests
|
|||
var response = await _accessService.RequestAccess(userId);
|
||||
Assert.True(response.HasAccess);
|
||||
await Task.Delay(EXP_MILLIS);
|
||||
FillSlots();
|
||||
for (int i = 0; i < CAP_LIMIT; i++)
|
||||
{
|
||||
await _accessService.RequestAccess(Guid.NewGuid().ToString());
|
||||
}
|
||||
response = await _accessService.RequestAccess(userId);
|
||||
Assert.False(response.HasAccess);
|
||||
}
|
||||
|
@ -198,7 +203,10 @@ namespace AccessQueueServiceTests
|
|||
{
|
||||
var userId = "user";
|
||||
await _accessService.RequestAccess(userId);
|
||||
FillSlots(BULK_COUNT);
|
||||
for (int i = 0; i < BULK_COUNT; i++)
|
||||
{
|
||||
_ = _accessService.RequestAccess(Guid.NewGuid().ToString());
|
||||
}
|
||||
var response = await _accessService.RequestAccess(userId);
|
||||
Assert.NotNull(response);
|
||||
Assert.True(response.HasAccess);
|
||||
|
@ -238,7 +246,10 @@ namespace AccessQueueServiceTests
|
|||
[Fact]
|
||||
public async Task RequestAccess_ShouldShowCorrectRequestsAhead_WhenAccessRerequested()
|
||||
{
|
||||
FillSlots();
|
||||
for (int i = 0; i < CAP_LIMIT; i++)
|
||||
{
|
||||
await _accessService.RequestAccess(Guid.NewGuid().ToString());
|
||||
}
|
||||
|
||||
var id1 = "user1";
|
||||
var id2 = "user2";
|
||||
|
@ -261,19 +272,6 @@ namespace AccessQueueServiceTests
|
|||
Assert.Equal(2, response3.RequestsAhead);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Status_ShouldReturnCorrectCounts()
|
||||
{
|
||||
FillSlots();
|
||||
await _accessService.RequestAccess("user");
|
||||
var status = _accessService.Status;
|
||||
|
||||
Assert.NotNull(status);
|
||||
Assert.Equal(CAP_LIMIT, status.UnexpiredTicketsCount);
|
||||
Assert.Equal(CAP_LIMIT, status.ActiveTicketsCount);
|
||||
Assert.Equal(1, status.QueueCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2025 Henry Hobbs
|
||||
Copyright (c) [year] [fullname]
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
|
213
README.md
213
README.md
|
@ -1,212 +1 @@
|
|||
**NOTE:** I have added github as a remote but for the latest commits and issue tracking please visit https://git.hobbs.zone/henry/AccessQueueService
|
||||
|
||||
# AccessQueueService
|
||||
|
||||
AccessQueueService is a microservice API designed to control access to a resource with a limited number of concurrent users. It ensures fair access by:
|
||||
|
||||
- Granting immediate access if capacity is available.
|
||||
- Placing users in a queue when the resource is full.
|
||||
- Automatically managing the queue in a first-in, first-out (FIFO) order.
|
||||
- Allowing users to revoke their access, freeing up capacity for others.
|
||||
|
||||
This service is ideal for scenarios where you need to limit the number of users accessing a resource at the same time, such as online ticket sale platforms that control how many users can purchase tickets concurrently.
|
||||
|
||||
Note: This service is not intended to be called directly from end-user client applications, as it could be easily bypassed. Instead, it should be integrated as middleware within your own APIs or backend services.
|
||||
|
||||
## How the Service Works
|
||||
|
||||
1. **Requesting Access:**
|
||||
- When a user requests access, the service checks if the current number of active users is below `CapacityLimit`.
|
||||
- If there is capacity, the user is granted access immediately and receives an expiration date set by `ExpirationSeconds`.
|
||||
- If not, the user is added to a queue and receives their position in the queue.
|
||||
|
||||
2. **Queueing:**
|
||||
- If a user is placed in the queue, subsequent access requests will return the number of users ahead.
|
||||
- Users must continually re-request access to remain active in the queue; inactivity may result in losing their spot.
|
||||
|
||||
3. **Dequeuing:**
|
||||
- Users in the queue are managed in a FIFO (first-in, first-out) order.
|
||||
- Whenever an access request is made, if there is capacity, the service attempts to dequeue users until capacity is met.
|
||||
- If a user is dequeued but the time since their last activity is greater than `ActivitySeconds`, they are not granted access and lose their spot in the queue.
|
||||
|
||||
4. **Maintaining Access:**
|
||||
- Users should continually re-request access while they are active to avoid being considered inactive.
|
||||
- If `RollingExpiration` is enabled, the expiration is reset whenever access is re-requested.
|
||||
|
||||
5. **Revoking Access:**
|
||||
- If a user requests access after their expiration date, they must restart the process and re-join the queue if there isn't capacity.
|
||||
- When a user revokes access (or their access times out), their access expires immediately.
|
||||
|
||||
### Note on inactivity vs expiration
|
||||
|
||||
It is possible for the number of users with access to temporarily exceed the `CapacityLimit` if `ActivitySeconds` is less than `ExpirationSeconds`. This happens because:
|
||||
|
||||
- The number of available slots is determined by the time since a user's last activity (`ActivitySeconds`), not by their access expiration (`ExpirationSeconds`).
|
||||
- If a user is inactive for longer than `ActivitySeconds`, they no longer count toward the capacity, allowing another user to gain access.
|
||||
- However, the original user still technically has access until their `ExpirationSeconds` elapses.
|
||||
|
||||
**To ensure the number of users with access never exceeds `CapacityLimit`, set `ActivitySeconds` equal to `ExpirationSeconds`.**
|
||||
|
||||
## API Routes
|
||||
|
||||
### Request Access
|
||||
- **GET /access/{id}**
|
||||
- **Description:** Request access for a user with the specified `id`.
|
||||
- **Response:** Returns an `AccessResponse` object indicating whether access was granted or the user's position in the queue.
|
||||
|
||||
### Revoke Access
|
||||
- **DELETE /access/{id}**
|
||||
- **Description:** Revoke access for a user with the specified `id`. This will remove the user from the active list or queue and may allow the next user in the queue to gain access.
|
||||
- **Response:** Returns a boolean indicating success.
|
||||
|
||||
### Get Service Status
|
||||
- **GET /status**
|
||||
- **Description:** Returns the current status of the access queue, including active users, queue length, and other statistics.
|
||||
- **Response:** Returns an `AccessQueueStatus` object.
|
||||
|
||||
### Get Configuration
|
||||
- **GET /config**
|
||||
- **Description:** Returns the current configuration of the access queue service.
|
||||
- **Response:** Returns an `AccessQueueConfig` object.
|
||||
|
||||
### Update Configuration
|
||||
- **POST /config**
|
||||
- **Description:** Updates the configuration of the access queue service. Accepts a partial or full `AccessQueueConfig` object in the request body.
|
||||
- **Response:** Returns no content on success (HTTP 204).
|
||||
|
||||
## Configuration Variables
|
||||
|
||||
Configuration is set in `appsettings.json` or via environment variables. The main configuration section is `AccessQueue`:
|
||||
|
||||
**Required variables:**
|
||||
|
||||
- **CapacityLimit**: The maximum number of users that can have access at the same time.
|
||||
- **ActivitySeconds**: How long (in seconds) a user can remain active before being considered inactive.
|
||||
- **ExpirationSeconds**: How long (in seconds) before an access ticket expires.
|
||||
- **RollingExpiration**: If true, the expiration timer resets on activity.
|
||||
- **CleanupIntervalSeconds**: How often (in seconds) the background cleanup runs to remove expired/inactive users.
|
||||
|
||||
**Optional variables:**
|
||||
|
||||
- **BackupFilePath**: The file path where the access queue state will be periodically saved.
|
||||
- **BackupIntervalSeconds**: How often (in seconds) the state is backed up to disk.
|
||||
- **CacheMilliseconds**: How long (in milliseconds) to cache the active user count before recalculating. Lower values mean more frequent recalculation (more accurate, higher CPU usage). Higher values improve performance but may cause a slight delay in recognizing open spots.
|
||||
|
||||
Example `appsettings.json`:
|
||||
```json
|
||||
{
|
||||
"AccessQueue": {
|
||||
"CapacityLimit": 100,
|
||||
"ActivitySeconds": 900,
|
||||
"ExpirationSeconds": 43200,
|
||||
"RollingExpiration": true,
|
||||
"CleanupIntervalSeconds": 60,
|
||||
"CacheMilliseconds": 1000
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## State Persistence and Backup
|
||||
|
||||
AccessQueueService automatically saves its in-memory state (active users and queue) to disk at regular intervals and restores it on startup. This helps prevent data loss in case of unexpected shutdowns or restarts.
|
||||
|
||||
- **Backup Location:** The backup file path is set via the `AccessQueue:BackupFilePath` configuration variable. If this is not set, no backup will be performed.
|
||||
- **Backup Interval:** The frequency of backups is controlled by `AccessQueue:BackupIntervalSeconds` (in seconds). If this is not set or is zero, backups are disabled.
|
||||
- **Startup Restore:** On startup, if a backup file exists at the specified path, the service will attempt to restore the previous state from this file. If the file is missing or corrupted, the service starts with an empty queue and access list.
|
||||
- **Failure Handling:** Any errors during backup or restore are logged, but do not prevent the service from running.
|
||||
- **Backup Format:** The backup is saved as a JSON file containing the current state of the access queue and active users.
|
||||
|
||||
Example configuration:
|
||||
```json
|
||||
{
|
||||
"AccessQueue": {
|
||||
"BackupFilePath": "Logs/backup.json",
|
||||
"BackupIntervalSeconds": 60
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> **Note:** Ensure the backup file path is writable by the service. Regular backups help prevent data loss in case of unexpected shutdowns.
|
||||
|
||||
## AccessResponse Object
|
||||
|
||||
The `AccessResponse` object returned by the API contains the following properties:
|
||||
|
||||
- **ExpiresOn** (`DateTime?`): The UTC timestamp when the user's access will expire. `null` if the user does not have access.
|
||||
- **RequestsAhead** (`int`): The number of requests ahead of the user in the queue. `0` if the user has access.
|
||||
- **HasAccess** (`bool`): Indicates whether the user currently has access (true if `ExpiresOn` is set and in the future).
|
||||
|
||||
## Running the Service
|
||||
|
||||
1. Build and run the project using .NET 8.0 or later:
|
||||
```powershell
|
||||
dotnet run --project AccessQueueService/AccessQueueService.csproj
|
||||
```
|
||||
2. By default, the API will be available at:
|
||||
- HTTP: http://localhost:5199
|
||||
- HTTPS: https://localhost:7291
|
||||
(See `AccessQueueService/Properties/launchSettings.json` for details.)
|
||||
|
||||
## Running the Tests
|
||||
|
||||
Unit tests for the service are located in the `AccessQueueServiceTests` project. To run all tests, use the following command from the root of the repository:
|
||||
|
||||
```powershell
|
||||
# Run all tests in the solution
|
||||
dotnet test
|
||||
```
|
||||
|
||||
Test results will be displayed in the terminal. You can also use Visual Studio's Test Explorer for a graphical interface.
|
||||
|
||||
## AccessQueuePlayground (Demo UI)
|
||||
|
||||
The `AccessQueuePlayground` project provides a simple web-based UI for interacting with the AccessQueueService API. This is useful for testing and demonstration purposes.
|
||||
|
||||
### Running
|
||||
|
||||
1. Build and run the playground project:
|
||||
```powershell
|
||||
dotnet run --project AccessQueuePlayground/AccessQueuePlayground.csproj
|
||||
```
|
||||
2. By default, the playground will be available at:
|
||||
- HTTP: http://localhost:5108
|
||||
- HTTPS: https://localhost:7211
|
||||
(See `AccessQueuePlayground/Properties/launchSettings.json` for details.)
|
||||
|
||||
### Using
|
||||
|
||||
- Open the provided URL in your browser.
|
||||
- Use the UI to request and revoke access for different user IDs.
|
||||
- The UI will display your access status, queue position, and expiration time.
|
||||
|
||||
This playground is intended only for local development and demonstration.
|
||||
|
||||
### Configuring
|
||||
|
||||
The `AccessQueuePlayground` project can be configured via its `appsettings.json` file. The main options are:
|
||||
|
||||
- **RefreshRateMilliseconds**: Determines how often (in milliseconds) the playground requests access for all active users and updates the UI. Lower values provide more real-time updates but may increase load on the service.
|
||||
- **ServiceUrl**: The URL of the AccessQueueService API to use. If this is set, all API requests from the playground will be sent to the specified service URL (e.g., `https://localhost:7291/`).
|
||||
- If `ServiceUrl` is **not provided**, the playground will use an internal instance of AccessQueueService, configured using the playground's own `appsettings.json` values under the `AccessQueue` section. This is useful for local development and testing without running the service separately.
|
||||
|
||||
Example configuration in `AccessQueuePlayground/appsettings.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"AccessQueuePlayground": {
|
||||
"RefreshRateMilliseconds": 200,
|
||||
"ServiceUrl": "https://localhost:7291/"
|
||||
},
|
||||
"AccessQueue": {
|
||||
"CapacityLimit": 3,
|
||||
"ActivitySeconds": 2,
|
||||
"ExpirationSeconds": 10,
|
||||
"RollingExpiration": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> **Tip:** Adjust `RefreshRateMilliseconds` for your use case. For most demos, 100–500ms works well. If you are connecting to a remote or production AccessQueueService, consider increasing the refresh interval (e.g., 1000ms or higher) to account for network latency and reduce unnecessary load.
|
||||
|
||||
## License
|
||||
See [LICENSE.txt](./LICENSE.txt) for license information.
|
||||
# AccessQueueService
|
Loading…
Reference in New Issue