Reference

ICurrentPrincipalProvider

Package Trax.MediatorNamespace Trax.Mediator.Services.PrincipalSource ICurrentPrincipalProvider.cs

Gives the mediator the current caller as a stable id. Its only use in Trax is bucketing the per-principal concurrency limit on direct runs. It is not used for authorization: nothing here grants or checks access.

Signature

namespace Trax.Mediator.Services.Principal;
 
public interface ICurrentPrincipalProvider
{
    string? GetCurrentPrincipalId();
}

Returns

The current principal's stable id, or null when there is none (the scheduler, a remote worker, an anonymous request).

Behavior

  • A run with a null or empty id is not limited per principal; the per-train and global limits still apply. Ids are compared ordinally.
  • The concurrency limiter is a singleton and resolves the provider once, then calls GetCurrentPrincipalId on every direct run while the per-principal limit is set.

Registered services

Registered byImplementation
AddMediatorA default that always returns null, so PerPrincipalMaxConcurrentRun limits nothing on its own
AddTraxApi (called by AddTraxGraphQL)Replaces the default with one that reads the trax:principal-id claim of the current request's authenticated user. Called before AddTrax, it is replaced by the mediator's default in turn

Example

public sealed class JobContextPrincipalProvider : ICurrentPrincipalProvider
{
    public string? GetCurrentPrincipalId() => JobContext.Current?.TenantId;
}
services.AddTrax(trax => trax
    .AddEffects(effects => effects.UsePostgres(connectionString))
    .AddMediator(mediator => mediator
        .ScanAssemblies(assembly)
        .PerPrincipalMaxConcurrentRun(5)));
 
services.AddSingleton<ICurrentPrincipalProvider, JobContextPrincipalProvider>();

See also