[backend/masto-client] Make idempotency keys unique by user id

This commit is contained in:
Laura Hausmann 2024-03-22 19:53:54 +01:00
parent c6300d1c96
commit e3e34b4495
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -296,7 +296,7 @@ public class StatusController(
var idempotencyKey = idempotencyKeyHeader.FirstOrDefault(); var idempotencyKey = idempotencyKeyHeader.FirstOrDefault();
if (idempotencyKey != null) if (idempotencyKey != null)
{ {
var hit = await cache.FetchAsync($"idempotency:{idempotencyKey}", TimeSpan.FromHours(24), var hit = await cache.FetchAsync($"idempotency:{user.Id}:{idempotencyKey}", TimeSpan.FromHours(24),
() => $"_:{HttpContext.TraceIdentifier}"); () => $"_:{HttpContext.TraceIdentifier}");
if (hit != $"_:{HttpContext.TraceIdentifier}") if (hit != $"_:{HttpContext.TraceIdentifier}")
@ -305,7 +305,7 @@ public class StatusController(
{ {
if (!hit.StartsWith('_')) break; if (!hit.StartsWith('_')) break;
await Task.Delay(100); await Task.Delay(100);
hit = await cache.GetAsync<string>($"idempotency:{idempotencyKey}") ?? hit = await cache.GetAsync<string>($"idempotency:{user.Id}:{idempotencyKey}") ??
throw new Exception("Idempotency key status disappeared in for loop"); throw new Exception("Idempotency key status disappeared in for loop");
if (i >= 10) if (i >= 10)
throw GracefulException.RequestTimeout("Failed to resolve idempotency key note within 1000 ms"); throw GracefulException.RequestTimeout("Failed to resolve idempotency key note within 1000 ms");
@ -385,7 +385,7 @@ public class StatusController(
poll, request.LocalOnly); poll, request.LocalOnly);
if (idempotencyKey != null) if (idempotencyKey != null)
await cache.SetAsync($"idempotency:{idempotencyKey}", note.Id, TimeSpan.FromHours(24)); await cache.SetAsync($"idempotency:{user.Id}:{idempotencyKey}", note.Id, TimeSpan.FromHours(24));
var res = await noteRenderer.RenderAsync(note, user); var res = await noteRenderer.RenderAsync(note, user);