| | | 1 | | using System.Globalization; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Heimdall.Command.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// The most live pending registrations (UC-47) one scope may hold at a time (#206). Read from |
| | | 7 | | /// <c>HEIMDALL_REGISTRATION_MAX_PENDING_PER_SCOPE</c>; 1000 by default. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <para> |
| | | 11 | | /// Each registration a caller sends stores a row and mails a link, and the per-IP rate limit is the |
| | | 12 | | /// only throttle on the caller: enough source addresses outpace the purge, and the table grows |
| | | 13 | | /// without bound. FR-SR-13 bounds what one address can be sent; this bounds the scope, whatever the |
| | | 14 | | /// addresses. A registration beyond it stores and mails nothing and is answered as any other. |
| | | 15 | | /// </para> |
| | | 16 | | /// <para> |
| | | 17 | | /// Any positive integer is accepted. Anything else — unparsable, zero or negative — falls back to the |
| | | 18 | | /// default rather than to no cap, and <see cref="RejectedMaxPendingPerScope" /> says so for start-up to |
| | | 19 | | /// warn, as an unusable retention batch size does. |
| | | 20 | | /// </para> |
| | | 21 | | /// </remarks> |
| | | 22 | | public class RegistrationOptions |
| | | 23 | | { |
| | | 24 | | /// <summary>Environment variable holding the cap.</summary> |
| | | 25 | | public const string MaxPendingPerScopeVariable = "HEIMDALL_REGISTRATION_MAX_PENDING_PER_SCOPE"; |
| | | 26 | | |
| | | 27 | | /// <summary>The cap when none is configured.</summary> |
| | | 28 | | public const int DefaultMaxPendingPerScope = 1000; |
| | | 29 | | |
| | | 30 | | /// <summary>The most live pending registrations one scope may hold.</summary> |
| | 2062 | 31 | | public int MaxPendingPerScope { get; init; } = DefaultMaxPendingPerScope; |
| | | 32 | | |
| | | 33 | | /// <summary>The configured text when it was present but unusable; <c>null</c> otherwise.</summary> |
| | 1964 | 34 | | public string? RejectedMaxPendingPerScope { get; init; } |
| | | 35 | | |
| | | 36 | | public static RegistrationOptions FromEnvironment() => |
| | 973 | 37 | | From(Environment.GetEnvironmentVariable(MaxPendingPerScopeVariable)); |
| | | 38 | | |
| | | 39 | | /// <summary>Reads a configured cap, falling back to the default when it is unusable.</summary> |
| | | 40 | | public static RegistrationOptions From(string? configured) |
| | | 41 | | { |
| | 982 | 42 | | var usable = int.TryParse(configured, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value) |
| | 982 | 43 | | && value > 0; |
| | | 44 | | |
| | 982 | 45 | | return new RegistrationOptions |
| | 982 | 46 | | { |
| | 982 | 47 | | MaxPendingPerScope = usable ? value : DefaultMaxPendingPerScope, |
| | 982 | 48 | | RejectedMaxPendingPerScope = usable || string.IsNullOrWhiteSpace(configured) ? null : configured |
| | 982 | 49 | | }; |
| | | 50 | | } |
| | | 51 | | } |