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: TODO:
We assume Linux. 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 To achieve the lowest latency possible and eliminate the proliferation, The architecture of this server is setup
Security Security

View File

@ -64,12 +64,16 @@ fn handleRequest(allocator: std.mem.Allocator, request: interface.ZigRequest, re
// real work // real work
for (request.headers) |h| { for (request.headers) |h| {
const header = interface.toZigHeader(h); const header = interface.toZigHeader(h);
if (!std.ascii.eqlIgnoreCase(header.name, "host")) continue; // std.debug.print("\n{s}: {s}\n", .{ header.name, header.value });
if (std.mem.startsWith(u8, header.value, "iam")) { if (std.ascii.eqlIgnoreCase(header.name, "host") and std.mem.startsWith(u8, header.value, "iam")) {
try response_writer.print("iam response", .{}); try response_writer.print("iam response", .{});
return; 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_writer.print(" {d}", .{request.headers.len});
try response.headers.put("X-custom-foo", "bar"); try response.headers.put("X-custom-foo", "bar");

View File

@ -59,7 +59,8 @@ const Executor = struct {
const SERVE_FN_NAME = "handle_request"; const SERVE_FN_NAME = "handle_request";
const PORT = 8069; // TODO: Update based on environment variable 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 executors: []Executor = undefined;
var parsed_config: config.ParsedConfig = undefined; var parsed_config: config.ParsedConfig = undefined;
@ -369,6 +370,10 @@ pub fn main() !void {
log.info("pid: {d}", .{std.os.linux.getpid()}); log.info("pid: {d}", .{std.os.linux.getpid()});
try installSignalHandler(); 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| var server_thread_count = if (std.os.getenv("SERVER_THREAD_COUNT")) |count|
try std.fmt.parseInt(usize, count, 10) try std.fmt.parseInt(usize, count, 10)
else switch (builtin.mode) { 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); var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit(); defer arena.deinit();
var aa = arena.allocator(); var aa = arena.allocator();
const bytes_preallocated = try preWarmArena(aa, &arena, 1); const bytes_preallocated = try preWarmArena(aa, &arena, response_preallocation_kb);
while (true) { while (true) {
defer { defer {
if (!arena.reset(.{ .retain_capacity = {} })) { if (!arena.reset(.{ .retain_capacity = {} })) {
@ -573,7 +578,7 @@ fn testRequest(request_bytes: []const u8) !void {
var aa = arena.allocator(); var aa = arena.allocator();
var bytes_allocated: usize = 0; var bytes_allocated: usize = 0;
// pre-warm // 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( const server_thread = try std.Thread.spawn(
.{}, .{},
processRequest, processRequest,