Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions frameworks/genhttp-ioxide-563/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin/
obj/
.idea/
.vs/
*.user
30 changes: 30 additions & 0 deletions frameworks/genhttp-ioxide-563/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
92 changes: 92 additions & 0 deletions frameworks/genhttp-ioxide-563/Infrastructure/Postgres.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// 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 <see cref="Pool" />. 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.
/// </summary>
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<PgPool>();

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<List<string>>(row.Field(6)),
Rating = new RatingInfo { Score = ParseInt(row.Field(7)), Count = ParseInt(row.Field(8)) },
};

public static int ParseInt(ReadOnlySpan<byte> s) => Utf8Parser.TryParse(s, out int v, out _) ? v : 0;

public static long ParseLong(ReadOnlySpan<byte> 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;
}
}
59 changes: 59 additions & 0 deletions frameworks/genhttp-ioxide-563/Model.cs
Original file line number Diff line number Diff line change
@@ -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<string>? 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<string>? 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<T>(List<T> items)
{

public List<T> Items => items;

public int Count => items.Count;

}


public sealed class CrudListResponse
{
public List<ProcessedItem> 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; }
}
120 changes: 120 additions & 0 deletions frameworks/genhttp-ioxide-563/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Reactor>? 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;
}
}
69 changes: 69 additions & 0 deletions frameworks/genhttp-ioxide-563/Project.cs
Original file line number Diff line number Diff line change
@@ -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<Baseline>("baseline11")
.AddService<Baseline>("baseline2")
.AddService<Echo>("echo")
.Add("json", ServiceResource.From<Json>().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<Crud>("items");

app = app.AddService<AsyncDatabase>("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);
}

}
Loading
Loading