Locking whil SyntaxCheck

This commit is contained in:
Simon Martens
2025-09-30 21:00:44 +02:00
parent 4d26bee2ba
commit 5f545bafc9
4 changed files with 26 additions and 20 deletions

View File

@@ -36,6 +36,7 @@ public class APIController : Controller {
// Options
private static readonly FormOptions _defaultFormOptions = new FormOptions();
private static readonly SemaphoreSlim _syntaxCheckLock = new SemaphoreSlim(1, 1);
public APIController(IHaDocumentWrappper lib, IXMLInteractionService xmlService, IXMLFileProvider xmlProvider) {
@@ -78,13 +79,22 @@ public class APIController : Controller {
// [ValidateAntiForgeryToken]
[DisableFormValueModelBinding]
[FeatureGate(Features.SyntaxCheck, Features.AdminService)]
public ActionResult<Dictionary<string, SyntaxCheckModel>?> GetSyntaxCheck(string? id) {
public async Task<ActionResult<Dictionary<string, SyntaxCheckModel>?>> GetSyntaxCheck(string? id) {
var SCCache = _xmlService.GetSCCache();
if (_xmlProvider.HasChanged() || SCCache == null) {
await _syntaxCheckLock.WaitAsync();
try {
// Double-check after acquiring lock
SCCache = _xmlService.GetSCCache();
if (_xmlProvider.HasChanged() || SCCache == null) {
var commit = _xmlProvider.GetGitState();
SCCache = _xmlService.Test(_xmlService.GetState(), commit != null ? commit.Commit : string.Empty);
_xmlService.SetSCCache(SCCache);
}
} finally {
_syntaxCheckLock.Release();
}
}
return Ok(SCCache);
}
}

View File

@@ -81,13 +81,13 @@ public class WebSocketMiddleware : IMiddleware {
}
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
} catch (WebSocketException ex) {
_openSockets!.Remove(webSocket);
break;
}
}
try {
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
} catch (WebSocketException ex) {
_openSockets.Remove(webSocket);
// Socket already closed
}
_openSockets!.Remove(webSocket);
}
@@ -124,13 +124,17 @@ public class WebSocketMiddleware : IMiddleware {
private async Task _SendToAll<T>(T msg) {
if (_openSockets == null) return;
foreach (var socket in _openSockets) {
var socketsToRemove = new List<WebSocket>();
foreach (var socket in _openSockets.ToList()) {
try {
await socket.SendAsync(_SerializeToBytes(msg), WebSocketMessageType.Text, true, CancellationToken.None);
} catch (WebSocketException ex) {
_openSockets.Remove(socket);
socketsToRemove.Add(socket);
}
}
foreach (var socket in socketsToRemove) {
_openSockets.Remove(socket);
}
}
private ArraySegment<byte> _SerializeToBytes<T>(T o) {

View File

@@ -2,10 +2,10 @@ name: hamann-ausgabe-staging
services:
hamann-staging:
build: .
ports:
- "5000:5000"
volumes:
- hamann_staging_data:/app/data
networks:
- caddy
environment:
- ASPNETCORE_URLS=http://+:5000
- DOTNET_ENVIRONMENT=Staging
@@ -18,7 +18,3 @@ services:
volumes:
hamann_staging_data:
external: true
networks:
caddy:
external: true

View File

@@ -2,10 +2,10 @@ name: hamann-ausgabe
services:
hamann-service:
build: .
ports:
- "5000:5000"
volumes:
- hamann_data:/app/data
networks:
- caddy
environment:
- ASPNETCORE_URLS=http://+:5000
- DOTNET_ENVIRONMENT=Production
@@ -18,7 +18,3 @@ services:
volumes:
hamann_data:
external: true
networks:
caddy:
external: true