diff --git a/README.md b/README.md index bb6824d..090dbd5 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,9 @@ Architecture TODO: We assume Linux. + +Built with zig 0.11.0-dev.3312+ab37ab33c (provide permalink). Explain lock to 0.11 when released. + To achieve the lowest latency possible and eliminate the proliferation, The architecture of this server is setup Security diff --git a/src/main-lib.zig b/src/main-lib.zig index 356d321..23e2b45 100644 --- a/src/main-lib.zig +++ b/src/main-lib.zig @@ -64,12 +64,16 @@ fn handleRequest(allocator: std.mem.Allocator, request: interface.ZigRequest, re // real work for (request.headers) |h| { const header = interface.toZigHeader(h); - if (!std.ascii.eqlIgnoreCase(header.name, "host")) continue; - if (std.mem.startsWith(u8, header.value, "iam")) { + // std.debug.print("\n{s}: {s}\n", .{ header.name, header.value }); + if (std.ascii.eqlIgnoreCase(header.name, "host") and std.mem.startsWith(u8, header.value, "iam")) { try response_writer.print("iam response", .{}); return; } - break; + if (std.ascii.eqlIgnoreCase(header.name, "x-slow")) { + std.time.sleep(std.time.ns_per_ms * (std.fmt.parseInt(usize, header.value, 10) catch 1000)); + try response_writer.print("i am slow\n\n", .{}); + return; + } } try response_writer.print(" {d}", .{request.headers.len}); try response.headers.put("X-custom-foo", "bar"); diff --git a/src/main.zig b/src/main.zig index 197f0df..3afb0a1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -59,7 +59,8 @@ const Executor = struct { const SERVE_FN_NAME = "handle_request"; const PORT = 8069; // TODO: Update based on environment variable -var initial_request_buffer: usize = 1; // TODO: Update based on environment variable +var response_preallocation_kb: usize = 8; // We used to need 1kb, but the changes between zig 465272921 and fd6200eda +// ends up allocating about 4kb. Bumping this to 8kb gives plugins some room var executors: []Executor = undefined; var parsed_config: config.ParsedConfig = undefined; @@ -369,6 +370,10 @@ pub fn main() !void { log.info("pid: {d}", .{std.os.linux.getpid()}); try installSignalHandler(); + response_preallocation_kb = if (std.os.getenv("RESPONSE_PREALLOCATION_KB")) |kb| + try std.fmt.parseInt(usize, kb, 10) + else + response_preallocation_kb; var server_thread_count = if (std.os.getenv("SERVER_THREAD_COUNT")) |count| try std.fmt.parseInt(usize, count, 10) else switch (builtin.mode) { @@ -404,7 +409,7 @@ fn threadMain(allocator: std.mem.Allocator, server: *std.http.Server, thread_num var arena = std.heap.ArenaAllocator.init(allocator); defer arena.deinit(); var aa = arena.allocator(); - const bytes_preallocated = try preWarmArena(aa, &arena, 1); + const bytes_preallocated = try preWarmArena(aa, &arena, response_preallocation_kb); while (true) { defer { if (!arena.reset(.{ .retain_capacity = {} })) { @@ -573,7 +578,7 @@ fn testRequest(request_bytes: []const u8) !void { var aa = arena.allocator(); var bytes_allocated: usize = 0; // pre-warm - bytes_allocated = try preWarmArena(aa, &arena, initial_request_buffer); + bytes_allocated = try preWarmArena(aa, &arena, response_preallocation_kb); const server_thread = try std.Thread.spawn( .{}, processRequest,