Unique indexes, one username space, and entity maps warmed before use
Infrastructure/Data/Indexes runs at every start, after the migrations: unique on Post/DmPost ObjectURI, ForeignAvatar ActorURI, the Follower triple, RootToAvatar, RootUser UserName and ReservedName; plain indexes on the lookups the services actually make (PublicKeyId, author and group post listings, ParticipantsKey, the delivery queue). Migration _001 runs first and removes the duplicates the races could already have left (keeping the newest actor row, the oldest post, the accepted follower), then fills ReservedName from every avatar and group. ReservedName is one username space for personas, groups and the instance: a name is reserved by an insert the unique index arbitrates, before the avatar or group is saved, so two simultaneous sign-ups cannot both get it. A short list of names (admin, support, abuse, postmaster, ...) is never available. EntityMaps.Warm touches every entity's collection one at a time before anything else runs. MongoDB.Entities maps the Entity base class on first touch, and two types mapped at once throw "An item with the same key has already been added" and stay broken for the life of the process; the parallel test run hit it, and the delivery worker racing a request could have too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012CzABvBkbcFqoHdmi8b9WB
This commit is contained in:
1 parent
0f85030744
commit
d2f0c7a14e
10 files changed
+263
-3
No files matched your search
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Models;
|
||||
@@ -52,6 +53,7 @@ namespace PrivaPub.Federation.Actors
|
||||
Task<LocalActor> FindByUri(string actorUri, CancellationToken token);
|
||||
Task<LocalActor> GetInstanceActor(CancellationToken token);
|
||||
Task<bool> IsUserNameTaken(string userName, CancellationToken token);
|
||||
Task<bool> TryReserveUserName(string userName, LocalActorKind kind, string ownerId, CancellationToken token);
|
||||
LocalActor FromAvatar(Avatar avatar);
|
||||
LocalActor FromGroup(GroupEntity group);
|
||||
}
|
||||
@@ -60,6 +62,12 @@ namespace PrivaPub.Federation.Actors
|
||||
{
|
||||
public const string InstanceUserName = "privapub";
|
||||
|
||||
static readonly HashSet<string> ReservedByInstance = new(StringComparer.Ordinal)
|
||||
{
|
||||
InstanceUserName, "admin", "administrator", "root", "system", "support", "help", "moderator", "mod",
|
||||
"abuse", "postmaster", "webmaster", "hostmaster", "security", "noreply", "no_reply", "null", "undefined"
|
||||
};
|
||||
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IOptionsMonitor<AppConfiguration> _appConfiguration;
|
||||
InstanceActor _instanceActor;
|
||||
@@ -149,11 +157,25 @@ namespace PrivaPub.Federation.Actors
|
||||
public async Task<bool> IsUserNameTaken(string userName, CancellationToken token)
|
||||
{
|
||||
userName = userName?.ToLowerInvariant();
|
||||
if (string.IsNullOrEmpty(userName) || userName == InstanceUserName)
|
||||
if (string.IsNullOrEmpty(userName) || ReservedByInstance.Contains(userName))
|
||||
return true;
|
||||
if (await _dbEntities.Avatars.Match(a => a.UserName == userName).ExecuteAnyAsync(token))
|
||||
return await DB.Default.Find<ReservedName>().Match(r => r.Name == userName).ExecuteAnyAsync(token);
|
||||
}
|
||||
|
||||
public async Task<bool> TryReserveUserName(string userName, LocalActorKind kind, string ownerId, CancellationToken token)
|
||||
{
|
||||
userName = userName?.ToLowerInvariant();
|
||||
if (string.IsNullOrEmpty(userName) || ReservedByInstance.Contains(userName))
|
||||
return false;
|
||||
try
|
||||
{
|
||||
await DB.Default.SaveAsync(new ReservedName { Name = userName, OwnerKind = kind, OwnerId = ownerId }, token);
|
||||
return true;
|
||||
return await _dbEntities.Groups.Match(g => g.UserName == userName).ExecuteAnyAsync(token);
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public LocalActor FromAvatar(Avatar avatar) => new()
|
||||
|
||||
Reference in new issue
Block a user