Massive logging, improve connection closing/timeout handling

This commit is contained in:
2026-03-13 02:47:36 +01:00
parent 712e214f61
commit cbf0d6a9da
5 changed files with 140 additions and 28 deletions

View File

@@ -8,8 +8,24 @@ const UUID = web.UUID;
var running: std.atomic.Value(bool) = .init(true);
fn interruptionHandler(signal: i32) callconv(.c) void {
switch (signal) {
fn interruptionHandler(sig: i32) callconv(.c) void {
var buf: [32]u8 = undefined;
const signal_name = blk: inline for (@typeInfo(linux.SIG).@"struct".decls) |decl| {
if (comptime std.mem.eql(u8, decl.name, "BLOCK") or
std.mem.eql(u8, decl.name, "UNBLOCK") or
std.mem.eql(u8, decl.name, "SETMASK")) continue;
const decl_value = @field(linux.SIG, decl.name);
if (@TypeOf(decl_value) != comptime_int) continue;
if (decl_value == sig) break :blk "SIG" ++ decl.name;
} else {
break :blk std.fmt.bufPrint(&buf, "#{d}", .{sig}) catch unreachable;
};
std.log.debug("Interrupted with signal {s}.", .{signal_name});
switch (sig) {
linux.SIG.INT, linux.SIG.TERM => {
running.store(false, .release);
},
@@ -19,6 +35,12 @@ fn interruptionHandler(signal: i32) callconv(.c) void {
const Handler = struct {
fn handle(_: *anyopaque, request: *web.Request, response: *web.Response) !void {
std.log.info("{f} | {s} {s}", .{
response.connection.address,
@tagName(request.method),
request.pathname,
});
if (!std.mem.eql(u8, request.pathname, "/")) {
try response.body_writer.writeAll("Not Found\n");
@@ -96,14 +118,16 @@ pub fn main() !void {
.mask = linux.sigemptyset(),
.flags = linux.SA.RESETHAND,
};
const rc = linux.sigaction(linux.SIG.INT, &sigaction, null);
switch (errno(rc)) {
.SUCCESS => {},
else => |e| {
std.log.err("Error while estabilishing interruption handler: {s}", .{@tagName(e)});
return error.SystemError;
},
}
signal(linux.SIG.INT, &sigaction);
signal(linux.SIG.TERM, &sigaction);
try server.listen(&running);
}
fn signal(sig: u8, action: *const linux.Sigaction) void {
var old_action = std.mem.zeroes(linux.Sigaction);
_ = linux.sigaction(sig, null, &old_action);
if (old_action.handler.handler == linux.SIG.IGN) return;
_ = linux.sigaction(sig, action, null);
}