update preallocation for new zig/enable environment variable to control

This commit is contained in:
Emil Lerch 2023-06-03 14:26:37 -07:00
parent a77e137da8
commit 8c86f3c7d4
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
3 changed files with 18 additions and 6 deletions

View File

@ -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

View File

@ -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");

View File

@ -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,