-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from SakuraIsayeki/develop
v0.17.4.1 - Add user atomic lock
- Loading branch information
Showing
9 changed files
with
106 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
WowsKarma.Api/Infrastructure/Attributes/UserAtomicLockAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace WowsKarma.Api.Infrastructure.Attributes; | ||
|
||
/// <summary> | ||
/// Provides an attribute to lock concurrency on a given action to a given user. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | ||
public class UserAtomicLockAttribute : Attribute | ||
{ | ||
public UserAtomicLockAttribute() { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Collections.Concurrent; | ||
using WowsKarma.Api.Infrastructure.Attributes; | ||
using WowsKarma.Common; | ||
|
||
namespace WowsKarma.Api.Middlewares; | ||
|
||
/// <summary> | ||
/// Provides a middleware to lock a given action to a given user. | ||
/// </summary> | ||
public class UserAtomicLockMiddleware : IMiddleware | ||
{ | ||
private static readonly ConcurrentDictionary<Tuple<string, uint>, object> Locks = new(); | ||
private static object ConcurrencyLock = new(); | ||
|
||
/// <inheritdoc /> | ||
public async Task InvokeAsync(HttpContext ctx, RequestDelegate next) | ||
{ | ||
// Get the current user and endpoint | ||
uint? uid = ctx.User.ToAccountListing()?.Id; | ||
PathString path = ctx.Request.Path; | ||
|
||
if (uid is null) | ||
{ | ||
// Pass through. | ||
await next(ctx); | ||
return; | ||
} | ||
|
||
if (ctx.GetEndpoint()?.Metadata.GetMetadata<UserAtomicLockAttribute>() is null) | ||
{ | ||
// Pass through. | ||
await next(ctx); | ||
return; | ||
} | ||
|
||
bool lockExists; | ||
// lock (ConcurrencyLock) | ||
// { | ||
lockExists = Locks.TryGetValue(new(path, uid.Value), out _) || !Locks.TryAdd(new(path, uid.Value), new()); | ||
// } | ||
|
||
// Get or try to add the lock object. | ||
if (lockExists) | ||
{ | ||
// Lock is already taken. | ||
ctx.Response.StatusCode = StatusCodes.Status429TooManyRequests; | ||
return; | ||
} | ||
|
||
// Hold the lock for the request's duration. | ||
try | ||
{ | ||
await next(ctx); | ||
} | ||
finally | ||
{ | ||
// lock (ConcurrencyLock) | ||
// { | ||
// Release the lock. | ||
Locks.TryRemove(new(path, uid.Value), out _); | ||
// } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 8 additions & 10 deletions
18
wowskarma.app/src/app/shared/layout/navbar/nav-auth/nav-auth.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
<ul class="navbar-nav flex-grow-0 me-xl-3"> | ||
<ng-container *ngIf="authService.userInfo$ | async as user else unauthorized"> | ||
<li class="navbar-text text-light">Welcome, {{user.username}}. </li> | ||
@if (authService.userInfo$ | async; as user) { | ||
<li class="navbar-text text-white me-3">Welcome, {{user.username}}.</li> | ||
|
||
<li class="nav-item"> | ||
<a class="nav-link text-white" [routerLink]="['/player', user.id +','+ user.username]">Profile</a> | ||
<a class="nav-link" [routerLink]="['/player', user.id +','+ user.username]" routerLinkActive="active">Profile</a> | ||
</li> | ||
|
||
<li class="nav-item"><a class="nav-link text-white" routerLink="/settings">Settings</a></li> | ||
<li class="nav-item"><a class="nav-link text-white" routerLink="/logout">Logout</a></li> | ||
</ng-container> | ||
|
||
<ng-template #unauthorized> | ||
<li class="nav-item"><a class="nav-link" routerLink="/settings" routerLinkActive="active">Settings</a></li> | ||
<li class="nav-item"><a class="nav-link" routerLink="/logout" routerLinkActive="active">Logout</a></li> | ||
} @else { | ||
<li class="nav-item"> | ||
<a class="nav-link text-white" routerLink="/login" [queryParams]="{redirectUri: (currentRelativePath$ | async)}">Login</a> | ||
<a class="nav-link text-white" routerLink="/login" [queryParams]="{redirectUri: (currentRelativePath$ | async)}" routerLinkActive="active">Login</a> | ||
</li> | ||
</ng-template> | ||
} | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters