diff --git a/frameworks/genhttp-ioxide-563/.dockerignore b/frameworks/genhttp-ioxide-563/.dockerignore new file mode 100644 index 0000000000..2d4eae4526 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/.dockerignore @@ -0,0 +1,5 @@ +bin/ +obj/ +.idea/ +.vs/ +*.user diff --git a/frameworks/genhttp-ioxide-563/Dockerfile b/frameworks/genhttp-ioxide-563/Dockerfile new file mode 100644 index 0000000000..b674480acb --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Dockerfile @@ -0,0 +1,30 @@ +FROM mcr.microsoft.com/dotnet/sdk:11.0 AS build + +# GenHTTP from its ioxide-update branch (PR #953) instead of NuGet, fetched by commit so a later +# push to the branch can't change what this measures. +ARG GENHTTP_REPO=https://github.com/Kaliumhexacyanoferrat/GenHTTP.git +ARG GENHTTP_REF=update-ioxide-0.20.396 +ARG GENHTTP_COMMIT=a5d6314e2e1ee79f6657c98dd2081a1e2116b98f + +WORKDIR /src/GenHTTP +RUN echo "Building GenHTTP ${GENHTTP_REF} at ${GENHTTP_COMMIT}" && \ + git init -q && \ + git remote add origin "${GENHTTP_REPO}" && \ + git fetch -q --depth 1 origin "${GENHTTP_COMMIT}" && \ + git checkout -q FETCH_HEAD + +WORKDIR /src/app + +COPY genhttp.csproj ./ +RUN dotnet restore + +COPY . . +RUN dotnet publish genhttp.csproj -c Release --no-self-contained -o /app + +FROM mcr.microsoft.com/dotnet/runtime:11.0 +WORKDIR /app +COPY --from=build /app . + +# 8080 h1, 8081 h1+TLS, 8082 h2c, 8443 h2 over TCP and h3 over QUIC (UDP) +EXPOSE 8080 8081 8082 8443 8443/udp +ENTRYPOINT ["dotnet", "genhttp.dll"] diff --git a/frameworks/genhttp-ioxide-563/Infrastructure/Postgres.cs b/frameworks/genhttp-ioxide-563/Infrastructure/Postgres.cs new file mode 100644 index 0000000000..c42019c7e3 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Infrastructure/Postgres.cs @@ -0,0 +1,92 @@ +using System.Buffers.Text; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Text.Json; +using GenHTTP.Api.Ioxide; +using GenHTTP.Engine.Ioxide; + +using ioxide; +using ioxide.pg; + +namespace genhttp.Infrastructure; + +/// +/// Postgres wiring for the async-db / crud profiles. The pool is per-reactor and ring-native — +/// started in each reactor's OnStart (via the engine's onReactorStart hook) and resolved from a +/// handler with . Rows arrive as UTF-8 text fields and are mapped into the shared +/// model types, so GenHTTP's serializer renders the JSON rather than a hand-rolled writer. +/// +public static class Postgres +{ + public const string Columns = "id, name, category, price, quantity, active, tags, rating_score, rating_count"; + + public static PgOptions? Options { get; private set; } + + public static bool Enabled => Options is not null; + + public static PgPool Pool => IoxideReactor.Current.GetService(); + + public static void Configure(int reactors) + { + var url = Environment.GetEnvironmentVariable("DATABASE_URL"); + if (string.IsNullOrEmpty(url)) + { + Options = null; + return; + } + + var uri = new Uri(url); + var userInfo = uri.UserInfo.Split(':', 2); + var maxConn = int.TryParse(Environment.GetEnvironmentVariable("DATABASE_MAX_CONN"), out var mc) ? mc : 256; + + Options = new PgOptions + { + Host = ResolveIPv4(uri.Host), + Port = (ushort)(uri.Port > 0 ? uri.Port : 5432), + User = userInfo[0], + Password = userInfo.Length > 1 ? userInfo[1] : null, + Database = uri.AbsolutePath.TrimStart('/'), + PoolSize = Math.Clamp(maxConn / Math.Max(reactors, 1), 1, 8), + }; + } + + // onReactorStart: start the ring-native pool on this reactor. + public static void Start(Reactor reactor) => PgPool.Start(reactor, Options!); + + // Map one Postgres row (UTF-8 text fields) into a model item. + // 0 id, 1 name, 2 category, 3 price, 4 quantity, 5 active(t/f), 6 tags(jsonb), 7 rating_score, 8 rating_count + public static ProcessedItem MapItem(PgRow row) => new() + { + Id = ParseInt(row.Field(0)), + Name = Encoding.UTF8.GetString(row.Field(1)), + Category = Encoding.UTF8.GetString(row.Field(2)), + Price = ParseInt(row.Field(3)), + Quantity = ParseInt(row.Field(4)), + Active = row.Field(5).SequenceEqual("t"u8), + Tags = JsonSerializer.Deserialize>(row.Field(6)), + Rating = new RatingInfo { Score = ParseInt(row.Field(7)), Count = ParseInt(row.Field(8)) }, + }; + + public static int ParseInt(ReadOnlySpan s) => Utf8Parser.TryParse(s, out int v, out _) ? v : 0; + + public static long ParseLong(ReadOnlySpan s) => Utf8Parser.TryParse(s, out long v, out _) ? v : 0; + + private static string ResolveIPv4(string host) + { + if (IPAddress.TryParse(host, out _)) + { + return host; + } + + foreach (var addr in Dns.GetHostAddresses(host)) + { + if (addr.AddressFamily == AddressFamily.InterNetwork) + { + return addr.ToString(); + } + } + + return host; + } +} diff --git a/frameworks/genhttp-ioxide-563/Model.cs b/frameworks/genhttp-ioxide-563/Model.cs new file mode 100644 index 0000000000..a98572311d --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Model.cs @@ -0,0 +1,59 @@ +namespace genhttp; + +public sealed class DatasetItem +{ + public int Id { get; set; } + public string Name { get; set; } = ""; + public string Category { get; set; } = ""; + public int Price { get; set; } + public int Quantity { get; set; } + public bool Active { get; set; } + public List? Tags { get; set; } + public RatingInfo? Rating { get; set; } +} + +public sealed class ProcessedItem +{ + public int Id { get; set; } + public string Name { get; set; } = ""; + public string Category { get; set; } = ""; + public int Price { get; set; } + public int Quantity { get; set; } + public bool Active { get; set; } + public List? Tags { get; set; } + public RatingInfo? Rating { get; set; } + public long Total { get; set; } +} + +public sealed class RatingInfo +{ + public int Score { get; set; } + public int Count { get; set; } +} + +public sealed class ListWithCount(List items) +{ + + public List Items => items; + + public int Count => items.Count; + +} + + +public sealed class CrudListResponse +{ + public List Items { get; set; } = []; + public long Total { get; set; } + public int Page { get; set; } + public int Limit { get; set; } +} + +public sealed class CrudItem +{ + public int? Id { get; set; } + public string? Name { get; set; } + public string? Category { get; set; } + public int Price { get; set; } + public int Quantity { get; set; } +} diff --git a/frameworks/genhttp-ioxide-563/Program.cs b/frameworks/genhttp-ioxide-563/Program.cs new file mode 100644 index 0000000000..351580d358 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Program.cs @@ -0,0 +1,120 @@ +using System.Net; +using System.Net.Sockets; +using System.Security.Authentication; + +using genhttp; +using genhttp.Infrastructure; + +using GenHTTP.Api.Infrastructure; + +using GenHTTP.Engine.Ioxide; + +using ioxide; +using Microsoft.Extensions.Logging.Abstractions; + +// Reactor count follows the available CPUs (api-4 / api-16 control this via cpuset pinning); +// override with IOXIDE_REACTORS. +var reactors = int.TryParse(Environment.GetEnvironmentVariable("IOXIDE_REACTORS"), out var rc) ? rc : Environment.ProcessorCount; + +// Postgres (async-db / crud) is per-reactor: its pool is opened on each reactor's own thread. +Postgres.Configure(reactors); + +Action? onReactorStart = Postgres.Enabled ? Postgres.Start : null; + +// The engine buffers a whole response in one write slab; static assets can exceed the 16 KB default. +// Size the slab above the largest asset (plus GenHTTP's 64 KB file-copy buffer) - only when static is +// mounted, so the high-connection profiles keep the small per-connection buffer. +int? writeSlab = null; +var staticRoot = Environment.GetEnvironmentVariable("IOXIDE_STATIC") ?? "/data/static"; +if (Directory.Exists(staticRoot)) +{ + long largest = 0; + foreach (var file in Directory.EnumerateFiles(staticRoot, "*", SearchOption.AllDirectories)) + { + largest = Math.Max(largest, new FileInfo(file).Length); + } + writeSlab = (int)largest + 128 * 1024; +} + +// The harness mounts a certificate pair; without it only the plaintext listeners come up. +// kTLS derives its keys from one TLS 1.3 ciphersuite, so it pins the floor to 1.3 as well. +// ioxide refuses to start kTLS without the kernel's tls module, which a validation runner can lack. +var kernelTls = KernelTlsAvailable(); +var tlsFloor = kernelTls ? SslProtocols.Tls13 : SslProtocols.Tls12 | SslProtocols.Tls13; + +var certPath = Environment.GetEnvironmentVariable("TLS_CERT") ?? "/certs/server.crt"; +var keyPath = Environment.GetEnvironmentVariable("TLS_KEY") ?? "/certs/server.key"; +var hasCert = File.Exists(certPath) && File.Exists(keyPath); + +var options = new EngineOptions +{ + Reactor = new ReactorOptions + { + ReactorCount = reactors, + + // RecvSlots and RecvBufferSize are left at the engine's defaults on purpose. A parked + // request keeps its recv buffer - the engine parses headers zero-copy out of it - so the + // slot count is really a ceiling on requests in flight, not just a buffer count. This entry + // used to pin it at 256, carried over from the old BufferRingEntries setting, and the async + // profile paid for it: holding 32000 connections on a 10ms wait, the server flat-lined at + // slots x reactors in flight however many connections were offered, stretching the wait + // rather than serving more. Measured at 12000 connections, 16 reactors: + // + // 256 slots 385,584 rps wait stretched to 30.8ms + // 4096 slots 934,065 rps wait held at 10.1ms + }, + Tcp = new TcpTransportOptions + { + WriteSlabSize = writeSlab ?? new TcpTransportOptions().WriteSlabSize, + + // Kernel TLS on the way out, which is what this entry measured before the engine made it + // opt-in: ioxide.tls used to default it on, so json-tls has always been kTLS TX with + // OpenSSL still decrypting inbound. It needs the Linux tls module, so it is on only where + // KernelTlsAvailable finds it. + // + // cat /proc/sys/net/ipv4/tcp_available_ulp # wanted: tls + TxKernelTls = kernelTls, + }, +}; + +// Compression is attached per route in Project.cs, not to the host. +var host = Host.Create(onReactorStart: onReactorStart, options: options) + .Logging(NullLoggerFactory.Instance, false) + .Handler(Project.Create()); + +// :8080 HTTP/1.1 plaintext baseline, pipelined, limited-conn, async, json-comp, async-db, ws +// :8082 HTTP/1.1 + HTTP/2 cleartext baseline-h2c, json-h2c - the preface decides which +host.Bind(IPAddress.Any, 8080, HttpProtocols.Http1); +host.Bind(IPAddress.Any, 8082, HttpProtocols.Http1AndHttp2); + +if (hasCert) +{ + // The engine terminates TLS itself now, in OpenSSL, so the certificate is named as files - + // which is also the only form HTTP/3 takes, ngtcp2 loading PEM by path. + var certificate = new FileCertificateProvider(certPath, keyPath); + + // :8081 HTTP/1.1 over TLS json-tls, static-tls, 8gbit + // :8443 HTTP/1.1 + HTTP/2 over TCP, baseline-h2, static-h2 (ALPN picks h2) + // and HTTP/3 over QUIC on the baseline-h3, static-h3 + // same port number + host.Bind(IPAddress.Any, 8081, certificate, sslProtocols: tlsFloor, httpProtocols: HttpProtocols.Http1); + host.Bind(IPAddress.Any, 8443, certificate, sslProtocols: tlsFloor, httpProtocols: HttpProtocols.All); +} + +await host.RunAsync(); + +// ioxide's own probe: TCP_ULP "tls" on an unconnected socket refuses with ENOTCONN once the module is found. +static bool KernelTlsAvailable() +{ + using var probe = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + try + { + probe.SetRawSocketOption(6 /* SOL_TCP */, 31 /* TCP_ULP */, "tls"u8); + return true; + } + catch (SocketException e) + { + return e.SocketErrorCode == SocketError.NotConnected; + } +} diff --git a/frameworks/genhttp-ioxide-563/Project.cs b/frameworks/genhttp-ioxide-563/Project.cs new file mode 100644 index 0000000000..36e584d7b8 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Project.cs @@ -0,0 +1,69 @@ +using GenHTTP.Api.Content; + +using GenHTTP.Modules.IO; +using GenHTTP.Modules.Files; +using GenHTTP.Modules.Layouting; +using GenHTTP.Modules.Layouting.Provider; +using GenHTTP.Modules.Webservices; +using GenHTTP.Modules.Websockets; + +using genhttp.Infrastructure; +using GenHTTP.Modules.Compression; +using GenHTTP.Modules.Compression.Algorithms; +using genhttp.Tests; + +namespace genhttp; + +public static class Project +{ + public static IHandlerBuilder Create() + { + // Compression sits on /json, the one route whose clients ask for it (json-comp), rather + // than on the whole host. Host-wide, every request passes through the concern, /delay + // included, whose body is a number: one more async frame for each of the async profile's + // 32000 parked requests to suspend and resume, for nothing. Static assets do not need it: + // the asset handler serves the pre-compressed .br files itself. + var app = Layout.Create() + .Add("pipeline", Content.From(Resource.FromString("ok"))) + .AddService("baseline11") + .AddService("baseline2") + .AddService("echo") + .Add("json", ServiceResource.From().Add(CompressedContent.Default())) + .Add("delay", new DelayBuilder()); + + // async-db and crud require a configured Postgres (DATABASE_URL). + if (Postgres.Enabled) + { + var crud = Layout.Create() + .AddService("items"); + + app = app.AddService("async-db") + .Add("crud", crud); + } + + return app.AddStaticFiles() + .AddWebsocket(); + } + + private static LayoutBuilder AddStaticFiles(this LayoutBuilder app) + { + var staticDir = Environment.GetEnvironmentVariable("IOXIDE_STATIC") ?? "/data/static"; + + if (Directory.Exists(staticDir)) + { + app.Add("static", Assets.From(staticDir).AllowPrecompressed(new BrotliAlgorithm())); + } + + return app; + } + + private static LayoutBuilder AddWebsocket(this LayoutBuilder app) + { + var websocket = Websocket.Imperative() + .DoNotAllocateFrameData() + .Handler(new EchoHandler()); + + return app.Add("ws", websocket); + } + +} diff --git a/frameworks/genhttp-ioxide-563/README.md b/frameworks/genhttp-ioxide-563/README.md new file mode 100644 index 0000000000..0d3d4db682 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/README.md @@ -0,0 +1,41 @@ +# genhttp-ioxide + +[GenHTTP 11](https://github.com/Kaliumhexacyanoferrat/GenHTTP) running on a custom +**io_uring** server engine (the [ioxide](https://github.com/MDA2AV/ioxide) runtime) instead of GenHTTP's default socket engine. + +The engine runs GenHTTP's own HTTP/1.1 conversation directly on ioxide's per-connection +duplex pipe — thread-per-core, one io_uring reactor per core, with chunked transfer-encoding, +keep-alive, a per-second cached `Date` header and a per-reactor request pool. The ioxide engine +([PR #860](https://github.com/Kaliumhexacyanoferrat/GenHTTP/pull/860)) is now merged and published +to nuget.org: the app references the `GenHTTP.Engine.Ioxide` and `GenHTTP.Modules.IoxideFiles` +packages alongside the standard IO / Layouting / Webservices / Compression modules and the +`ioxide.pg` (Postgres) and `ioxide.tls` (TLS) packages — no source checkout needed. + +Postgres access and TLS termination ride generic per-reactor seams the engine exposes +(`IoxideReactor.Current`, an `onReactorStart` hook, and a connection-transport factory) — the +engine itself stays free of any `ioxide.pg` / `ioxide.tls` dependency. + +## Profiles + +Responses are produced by GenHTTP's own pipeline (routing + serialization), not hand-written: + +- `baseline` — mixed GET/POST with query parsing (`/baseline11` sum webservice) +- `pipelined` — 16× batched pipelining (`/pipeline`) +- `limited-conn` — short-lived connections that close after 10 requests +- `json` / `json-comp` — `/json/{count}?m=N` serialized items; json-comp adds Brotli (`Accept-Encoding`) +- `json-tls` — `json` over TLS on `:8081` (`ioxide.tls`, kTLS TX offload) +- `static` — `/static/...` files with encoding negotiation (Modules.IO / Files) +- `8gbit` — `POST /echo`, returns the request body back unchanged +- `async-db` — `/async-db?min=&max=&limit=`, Postgres via `ioxide.pg` (per-reactor pool) +- `crud` — list / get / create / update on `/crud/items`, cache-aside (`X-Cache`, in-process) + +`json-tls` serves on `:8081` when `TLS_CERT` / `TLS_KEY` (default `/certs`) exist; the DB profiles +need `DATABASE_URL`. Both are provided by the harness sidecars. + +## Build note + +This entry targets **.NET 11** (`net11.0`), matching the GenHTTP `11.0.0-preview.24` packages it +references - the same preview the `genhttp` entry builds against, and the first one that ships the +merged ioxide engine branch (PR #887) with TLS, HTTP/2 and HTTP/3 in the engine. Requires the .NET 11 +SDK with Roslyn 5.3+ (GenHTTP's `MemoryView` source generator references `Microsoft.CodeAnalysis 5.3`); +the `mcr.microsoft.com/dotnet/sdk:11.0.100-preview.7` image used by the Dockerfile provides both. diff --git a/frameworks/genhttp-ioxide-563/Tests/AsyncDatabase.cs b/frameworks/genhttp-ioxide-563/Tests/AsyncDatabase.cs new file mode 100644 index 0000000000..e631f2e7e8 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Tests/AsyncDatabase.cs @@ -0,0 +1,39 @@ +using System.Text; +using System.Text.Json; + +using genhttp.Infrastructure; + +using GenHTTP.Modules.Webservices; + +namespace genhttp.Tests; + +public class AsyncDatabase +{ + + // GET /async-db?min=&max=&limit= -> items with price in [min,max], fetched from Postgres via + // ioxide.pg's per-reactor pool and returned as objects GenHTTP serializes ({ items, count }). + [ResourceMethod] + public async Task> Compute(int min = 10, int max = 50, int limit = 50) + { + var items = new List(limit); + + // min/max/limit are int-typed (query-bound), so inline interpolation is injection-safe and + // keeps a stable statement shape — matches the reference async-db endpoint. + var sql = $"SELECT {Postgres.Columns} FROM items WHERE price BETWEEN {min} AND {max} LIMIT {limit}"; + + await Postgres.Pool.QueryAsync(sql, default, row => items.Add(new + { + id = Postgres.ParseInt(row.Field(0)), + name = Encoding.UTF8.GetString(row.Field(1)), + category = Encoding.UTF8.GetString(row.Field(2)), + price = Postgres.ParseInt(row.Field(3)), + quantity = Postgres.ParseInt(row.Field(4)), + active = row.Field(5).SequenceEqual("t"u8), + tags = JsonSerializer.Deserialize>(row.Field(6)), + rating = new { score = Postgres.ParseInt(row.Field(7)), count = Postgres.ParseInt(row.Field(8)) }, + })); + + return new ListWithCount(items); + } + +} diff --git a/frameworks/genhttp-ioxide-563/Tests/Baseline.cs b/frameworks/genhttp-ioxide-563/Tests/Baseline.cs new file mode 100644 index 0000000000..1e31cbf42e --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Tests/Baseline.cs @@ -0,0 +1,15 @@ +using GenHTTP.Modules.Reflection; +using GenHTTP.Modules.Webservices; + +namespace genhttp.Tests; + +public class Baseline +{ + + [ResourceMethod] + public int Sum(int a, int b) => a + b; + + [ResourceMethod(Method.Post)] + public int Sum(int a, int b, [FromBody] int c) => a + b + c; + +} diff --git a/frameworks/genhttp-ioxide-563/Tests/Crud.cs b/frameworks/genhttp-ioxide-563/Tests/Crud.cs new file mode 100644 index 0000000000..890070ddf5 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Tests/Crud.cs @@ -0,0 +1,141 @@ +using System.Text.Json; + +using GenHTTP.Api.Content; +using GenHTTP.Api.Protocol; + +using genhttp.Infrastructure; + +using GenHTTP.Modules.Reflection; +using GenHTTP.Modules.Webservices; + +using ioxide.pg; + +using Microsoft.Extensions.Caching.Memory; +using StringContent = GenHTTP.Modules.IO.Strings.StringContent; + +namespace genhttp.Tests; + +// Mounted at /crud/items (Crud sublayout): +// GET /crud/items?category=&page=&limit= -> paginated list (load-more semantics) +// GET /crud/items/{id} -> single item, cache-aside (X-Cache: MISS|HIT, 200ms TTL) +// POST /crud/items -> upsert (201) +// PUT /crud/items/{id} -> update (200), invalidates the cache entry +public class Crud +{ + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); + + private static readonly IMemoryCache ItemCache = new MemoryCache(new MemoryCacheOptions()); + + private static readonly MemoryCacheEntryOptions ItemCacheOptions = new() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(200) }; + + [ResourceMethod] + public async Task List(string category = "electronics", int page = 1, int limit = 10) + { + if (page < 1) page = 1; + if (limit is < 1 or > 50) limit = 10; + + var offset = (page - 1) * limit; + + var items = new List(); + + PgParam[] args = [PgParam.Text(category), PgParam.Int(limit), PgParam.Int(offset)]; + await Postgres.Pool.QueryAsync( + "SELECT " + Postgres.Columns + " FROM items WHERE category = $1 ORDER BY id LIMIT $2 OFFSET $3", + args, + row => items.Add(Postgres.MapItem(row))); + + return new CrudListResponse + { + Items = items, + Total = items.Count, + Page = page, + Limit = limit + }; + } + + [ResourceMethod(":id")] + public async ValueTask Get(int id, IRequest request) + { + if (ItemCache.TryGetValue(id, out string? cached) && cached is not null) + { + return request.Respond() + .Content(new StringContent(cached, ContentType.ApplicationJson)) + .Header("X-Cache", "HIT") + .Build(); + } + + var item = await FetchItemByIdAsync(id); + + if (item is null) + { + throw new ProviderException(ResponseStatus.NotFound, $"Item with ID {id} does not exist"); + } + + // Serialize once and cache the string, so a HIT skips both the DB round-trip and re-serialization. + var json = JsonSerializer.Serialize(item, JsonOptions); + + ItemCache.Set(id, json, ItemCacheOptions); + + return request.Respond() + .Content(new StringContent(json, ContentType.ApplicationJson)) + .Header("X-Cache", "MISS") + .Build(); + } + + [ResourceMethod(Method.Post)] + public async Task> Create(CrudItem item) + { + PgParam[] args = + [ + PgParam.Int(item.Id ?? 0), + PgParam.Text(item.Name ?? "New Product"), + PgParam.Text(item.Category ?? "test"), + PgParam.Int(item.Price), + PgParam.Int(item.Quantity) + ]; + + await Postgres.Pool.QueryAsync( + "INSERT INTO items (id, name, category, price, quantity, active, tags, rating_score, rating_count) " + + "VALUES ($1, $2, $3, $4, $5, true, '[\"bench\"]', 0, 0) " + + "ON CONFLICT (id) DO UPDATE SET name = $2, price = $4, quantity = $5", + args); + + ItemCache.Remove(item.Id ?? 0); + + return new Result(item).Status(ResponseStatus.Created); + } + + [ResourceMethod(Method.Put, ":id")] + public async Task Update(int id, CrudItem item) + { + PgParam[] args = + [ + PgParam.Text(item.Name ?? "Updated"), + PgParam.Int(item.Price), + PgParam.Int(item.Quantity), + PgParam.Int(id) + ]; + + await Postgres.Pool.QueryAsync( + "UPDATE items SET name = $1, price = $2, quantity = $3 WHERE id = $4", + args); + + ItemCache.Remove(id); + + return item; + } + + private static async Task FetchItemByIdAsync(int id) + { + ProcessedItem? item = null; + + PgParam[] args = [PgParam.Int(id)]; + await Postgres.Pool.QueryAsync( + "SELECT " + Postgres.Columns + " FROM items WHERE id = $1 LIMIT 1", + args, + row => item = Postgres.MapItem(row)); + + return item; + } + +} diff --git a/frameworks/genhttp-ioxide-563/Tests/Delay.cs b/frameworks/genhttp-ioxide-563/Tests/Delay.cs new file mode 100644 index 0000000000..e54010eb8b --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Tests/Delay.cs @@ -0,0 +1,116 @@ +using System.Buffers.Text; + +using GenHTTP.Api.Content; +using GenHTTP.Api.Infrastructure; +using GenHTTP.Api.Ioxide; +using GenHTTP.Api.Protocol; + +using GenHTTP.Engine.Ioxide; +using StringContent = GenHTTP.Modules.IO.Strings.StringContent; + +using ioxide.timer; + +namespace genhttp.Tests; + +/// +/// The async profile: hold the request for the requested number of milliseconds, then answer. +/// +/// +/// The wait is a ring timeout, not Task.Delay. The deadline travels in the io_uring +/// submission, so the kernel holds it and the completion arrives back on the reactor thread with +/// the connection's state still warm - whereas Task.Delay resumes on the thread pool, which costs +/// a hop per request and takes the whole exchange off its reactor for the rest of its life. +/// +/// One op is in flight per timer, so a timer cannot be shared by two waits that overlap. They are +/// pooled per reactor instead: rented for the wait, returned after it, and reused by the next +/// request that lands on the same thread. Measured over nine million requests, not one of them +/// resumed on a different thread than it started on, which is what makes the thread-static pool +/// safe as well as fast. +/// +/// This is a bare handler rather than a webservice because the profile holds 32000 requests at +/// once, so anything allocated per request is alive for the whole wait and gets promoted rather +/// than collected cheaply. Reaching the same route through the Webservices module measured 2230 +/// bytes a request against 1330 here - the module's binding and invocation being most of the +/// difference - and the remaining bytes are the engine's, not this handler's. +/// +public sealed class Delay : IHandler +{ + [ThreadStatic] + private static Stack? _timers; + + // The body echoes the delay back, which is how the profile checks the wait tracked the + // parameter rather than being a constant sleep. The values it asks for are small and repeat, + // so they are rendered once rather than per request. + private static readonly StringContent[] Bodies = BuildBodies(); + + public ValueTask PrepareAsync(IServer server) => default; + + public async ValueTask HandleAsync(IRequest request) + { + if (request.Header.Target.Current is not { } segment + || !Utf8Parser.TryParse(segment.Bytes.Span, out int ms, out var consumed) + || consumed != segment.Bytes.Length + || ms < 0) + { + return null; // not a delay request - let the rest of the layout answer it + } + + var timer = Rent(); + + try + { + await timer.DelayAsync(ms); + } + finally + { + Return(timer); + } + + return request.Respond() + .Content(ms < Bodies.Length ? Bodies[ms] : new StringContent(ms.ToString())) + .Build(); + } + + // One off this reactor's pool, or a new one bound to the reactor serving this request. + private static RingTimer Rent() + => _timers is { } pool && pool.TryPop(out var timer) ? timer : new RingTimer(IoxideReactor.Current); + + private static void Return(RingTimer timer) + { + var pool = _timers ??= new Stack(); + + // A connection per timer at most; the ceiling keeps a burst from pinning memory for good. + if (pool.Count < 16383) + { + pool.Push(timer); + } + } + + // The profile asks for a flat 10ms and validation draws from 10..90, so this covers every + // value either of them uses without a lookup miss. + private static StringContent[] BuildBodies() + { + var bodies = new StringContent[1024]; + + for (var i = 0; i < bodies.Length; i++) + { + bodies[i] = new StringContent(i.ToString()); + } + + return bodies; + } +} + +/// Builds the delay handler, so it can carry concerns like any other. +public sealed class DelayBuilder : IHandlerBuilder +{ + private readonly List _concerns = []; + + public DelayBuilder Add(IConcernBuilder concern) + { + _concerns.Add(concern); + return this; + } + + public IHandler Build() => Concerns.Chain(_concerns, new Delay()); +} diff --git a/frameworks/genhttp-ioxide-563/Tests/Echo.cs b/frameworks/genhttp-ioxide-563/Tests/Echo.cs new file mode 100644 index 0000000000..ba3fc97314 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Tests/Echo.cs @@ -0,0 +1,12 @@ +using GenHTTP.Modules.Reflection; +using GenHTTP.Modules.Webservices; + +namespace genhttp.Tests; + +public class Echo +{ + + [ResourceMethod(Method.Post)] + public Stream Return(Stream input) => input; + +} diff --git a/frameworks/genhttp-ioxide-563/Tests/EchoHandler.cs b/frameworks/genhttp-ioxide-563/Tests/EchoHandler.cs new file mode 100644 index 0000000000..04960feb58 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Tests/EchoHandler.cs @@ -0,0 +1,42 @@ +using GenHTTP.Modules.Websockets; +using GenHTTP.Modules.Websockets.Protocol; + +namespace genhttp.Tests; + + +public sealed class EchoHandler : IImperativeHandler +{ + + public async ValueTask HandleAsync(IImperativeConnection connection) + { + while (connection.Request.Server.Running) + { + var frame = await connection.ReadFrameAsync(); + + if (!await HandleAsync(frame, connection)) return; + + while (connection.TryReadFrame(out frame)) + { + if (!await HandleAsync(frame, connection)) return; + } + + await connection.FlushAsync(); + } + } + + private static async ValueTask HandleAsync(IWebsocketFrame frame, IImperativeConnection connection) + { + if (frame.Type == FrameType.Close) + { + return false; + } + + if (frame.Type is FrameType.Text or FrameType.Binary) + { + await connection.WriteAsync(frame.Data, frame.Type, flush: false); + } + + return true; + } + +} \ No newline at end of file diff --git a/frameworks/genhttp-ioxide-563/Tests/Json.cs b/frameworks/genhttp-ioxide-563/Tests/Json.cs new file mode 100644 index 0000000000..7ed96d0a23 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/Tests/Json.cs @@ -0,0 +1,60 @@ +using System.Text.Json; + +using GenHTTP.Api.Content; +using GenHTTP.Api.Protocol; +using GenHTTP.Modules.Webservices; + +namespace genhttp.Tests; + +public class Json +{ + private static readonly List? DatasetItems = LoadItems(); + + private static List? LoadItems() + { + var jsonOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + + var datasetPath = Environment.GetEnvironmentVariable("DATASET_PATH") ?? "/data/dataset.json"; + + if (File.Exists(datasetPath)) + { + return JsonSerializer.Deserialize>(File.ReadAllText(datasetPath), jsonOptions); + } + + return null; + } + + [ResourceMethod(":count")] + public ListWithCount Compute(int count, int m = 1) + { + if (DatasetItems == null) + { + throw new ProviderException(ResponseStatus.InternalServerError, "No dataset"); + } + + if (count > DatasetItems.Count) count = DatasetItems.Count; + if (count < 0) count = 0; + + var processed = new List(count); + + for (var i = 0; i < count; i++) + { + var d = DatasetItems[i]; + + processed.Add(new ProcessedItem + { + Id = d.Id, Name = d.Name, Category = d.Category, + Price = d.Price, Quantity = d.Quantity, Active = d.Active, + Tags = d.Tags, Rating = d.Rating, + Total = d.Price * d.Quantity * m + }); + } + + return new(processed); + } + +} diff --git a/frameworks/genhttp-ioxide-563/genhttp.csproj b/frameworks/genhttp-ioxide-563/genhttp.csproj new file mode 100644 index 0000000000..77d4a9365f --- /dev/null +++ b/frameworks/genhttp-ioxide-563/genhttp.csproj @@ -0,0 +1,41 @@ + + + + Exe + net11.0 + enable + enable + true + runtime-async=on + true + + 0 + + + + + $(MSBuildThisFileDirectory)../GenHTTP/ + + + + + + + + + + + + + + + + + + + + diff --git a/frameworks/genhttp-ioxide-563/meta.json b/frameworks/genhttp-ioxide-563/meta.json new file mode 100644 index 0000000000..2b44f89797 --- /dev/null +++ b/frameworks/genhttp-ioxide-563/meta.json @@ -0,0 +1,43 @@ +{ + "display_name": "genhttp-ioxide-563", + "language": "C#", + "type": "emerging", + "mode": "standard", + "completeness": { + "routing": true, + "middleware": true, + "request": true, + "response": true + }, + "engine": "ioxide", + "description": "GenHTTP 11 on .NET 11 on a custom io_uring server engine (the ioxide runtime) instead of the default socket engine. Built from GenHTTP's ioxide-update branch at a pinned commit, on ioxide 0.26.563, instead of the GenHTTP packages genhttp-ioxide uses; otherwise the same app. TLS is terminated in the engine itself, with HTTP/2 and HTTP/3: :8080 is HTTP/1.1, :8082 h2c, :8081 HTTP/1.1 over TLS, and :8443 serves HTTP/1.1 and HTTP/2 over TCP plus HTTP/3 over QUIC on the same port. Static is served by IoxideFiles, which reads bodies positionally off the io_uring ring through a per-reactor descriptor snapshot; responses go through GenHTTP's own routing + serialization; Postgres rides ioxide.pg (per-reactor ring-native pool).", + "repo": "https://github.com/Kaliumhexacyanoferrat/GenHTTP", + "website": "https://genhttp.org/documentation/server/engines/ioxide/", + "enabled": true, + "tests": [ + "baseline", + "async", + "latency-1m", + "latency-10k", + "latency-500k-8cpu", + "pipelined", + "limited-conn", + "json-comp", + "json-tls", + "static-tls", + "baseline-h2c", + "json-h2c", + "baseline-h2", + "static-h2", + "static-h3", + "baseline-h3", + "8gbit", + "async-db", + "echo-ws", + "echo-ws-pipeline", + "echo-ws-limited" + ], + "maintainers": [ + "MDA2AV" + ] +}