|
| 1 | +namespace Craft.Configuration; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Native C# endpoints, hosted alongside the PowerShell ones. |
| 5 | +/// |
| 6 | +/// <para> |
| 7 | +/// Off by default. An application opts in by naming the assemblies its endpoints live in; those load |
| 8 | +/// at startup and each <c>[CraftEndpoint]</c> type is mapped at its literal route. Because a literal |
| 9 | +/// route outranks the PowerShell catch-all, endpoints migrate one at a time with the PowerShell |
| 10 | +/// function left in place as the rollback. |
| 11 | +/// </para> |
| 12 | +/// </summary> |
| 13 | +public class EndpointSettings |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Master switch. Default false, so a deployment that names no assemblies pays nothing — |
| 17 | + /// no assembly loading, no reflection, no route mapping. |
| 18 | + /// </summary> |
| 19 | + public bool Enabled { get; set; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Assemblies to scan, absolute or relative to the API base path (e.g. |
| 23 | + /// <c>bin/GeoIpDb.Endpoints.dll</c>). Loaded into the default load context and never unloaded — |
| 24 | + /// see <c>NativeEndpointRegistry</c> for why a collectible context would be a trap. |
| 25 | + /// </summary> |
| 26 | + public List<string> Assemblies { get; set; } = []; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// What to do when a native endpoint claims a route a PowerShell function already has. |
| 30 | + /// |
| 31 | + /// <list type="bullet"> |
| 32 | + /// <item><description><c>PreferNative</c> (default) — the native endpoint wins and the |
| 33 | + /// shadowing is logged. This is the mode that makes migration work: flip one endpoint, keep the |
| 34 | + /// PowerShell one loaded as the rollback.</description></item> |
| 35 | + /// <item><description><c>PreferPowerShell</c> — instant rollback with no rebuild.</description></item> |
| 36 | + /// <item><description><c>Fail</c> — refuse to start, naming every collision. The right setting |
| 37 | + /// for CI, where a route shadow should be caught before it ships.</description></item> |
| 38 | + /// </list> |
| 39 | + /// |
| 40 | + /// <c>Fail</c> is arguably the safer runtime default and is deliberately not the one chosen: on a |
| 41 | + /// config-driven runtime it turns an accidental shadow into a failed rolling deploy. Set it in CI |
| 42 | + /// and leave <c>PreferNative</c> in production. |
| 43 | + /// </summary> |
| 44 | + public string OnCollision { get; set; } = "PreferNative"; |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Refuse to start when endpoints declare Central dispatch and no <c>ICraftEndpointHandler</c> |
| 48 | + /// was found in the scanned assemblies. Default false: a handler-less application simply |
| 49 | + /// dispatches every endpoint directly, which is exactly the pre-handler behaviour. |
| 50 | + /// </summary> |
| 51 | + /// <remarks> |
| 52 | + /// Set it true — in CI at minimum — for applications whose authorization lives in the central |
| 53 | + /// handler. For them a missing handler does not mean "less middleware"; it means every Central |
| 54 | + /// endpoint is reachable with no auth check at all, and that should fail the deploy, not ship. |
| 55 | + /// The same reasoning as <see cref="OnCollision"/>=Fail, and the same split applies: hard-fail |
| 56 | + /// is right where a human sees the failure before traffic does. |
| 57 | + /// </remarks> |
| 58 | + public bool RequireHandler { get; set; } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Blanket in-flight limit for native endpoints that declare none. 0 (default) means unbounded. |
| 62 | + /// </summary> |
| 63 | + /// <remarks> |
| 64 | + /// <para> |
| 65 | + /// With <c>Worker:HttpPoolSize=0</c> there is no runspace pool, and the pool was what implicitly |
| 66 | + /// capped concurrent work — a request could only run if a worker was free. Native endpoints are |
| 67 | + /// async end to end and hold no thread while waiting on an upstream, so nothing stops the process |
| 68 | + /// accepting far more concurrent requests than it can finish. Memory is what gives out first: each |
| 69 | + /// in-flight request holds its request and response buffers. |
| 70 | + /// </para> |
| 71 | + /// <para> |
| 72 | + /// This is a concurrency limit, not a rate limit, and the two solve different problems. |
| 73 | + /// <c>App:RateLimit:*</c> stops one caller monopolising the service and is partitioned per client; |
| 74 | + /// this bounds total simultaneous work regardless of who asked. A service behind a trusted |
| 75 | + /// single caller needs this one and not the other. |
| 76 | + /// </para> |
| 77 | + /// </remarks> |
| 78 | + public int MaxConcurrency { get; set; } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Per-route in-flight limits, keyed by route (e.g. <c>"GeoDBDownload": 4</c>). Overrides both the |
| 82 | + /// endpoint's <c>[CraftEndpoint(MaxConcurrency = n)]</c> and <see cref="MaxConcurrency"/>. Set 0 to |
| 83 | + /// explicitly remove a limit the endpoint declared. |
| 84 | + /// </summary> |
| 85 | + /// <remarks> |
| 86 | + /// Resolution order is: this, then the attribute, then <see cref="MaxConcurrency"/>. The attribute |
| 87 | + /// outranks the blanket default deliberately — an endpoint that declares a limit is asserting |
| 88 | + /// something about itself that an operator setting a global default has no way to know, and |
| 89 | + /// silently widening it would turn a safety limit into a footgun. Overriding it still works; it |
| 90 | + /// just has to be said explicitly, by name. |
| 91 | + /// </remarks> |
| 92 | + public Dictionary<string, int> Concurrency { get; set; } = new(StringComparer.OrdinalIgnoreCase); |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// How long a request waits for a concurrency slot before being shed with 503. Default 0, meaning |
| 96 | + /// each endpoint's own <c>QueueTimeoutSeconds</c> applies. |
| 97 | + /// </summary> |
| 98 | + public int QueueTimeoutSeconds { get; set; } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Resolves the in-flight limit for a route. See <see cref="Concurrency"/> for why the attribute |
| 102 | + /// outranks the blanket default. |
| 103 | + /// </summary> |
| 104 | + public int ResolveConcurrency(string route, int declared) |
| 105 | + { |
| 106 | + if (Concurrency.TryGetValue(route, out var configured)) return Math.Max(0, configured); |
| 107 | + return declared > 0 ? declared : Math.Max(0, MaxConcurrency); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary>Resolves the queue timeout for a route, preferring the global override when set.</summary> |
| 111 | + public int ResolveQueueTimeout(int declared) => |
| 112 | + QueueTimeoutSeconds > 0 ? QueueTimeoutSeconds : declared; |
| 113 | +} |
0 commit comments