~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
87 lines · 3.1 KB Raw History
const std = @import("std");

threadlocal var route: []const u8 = "background";

threadlocal var target_buf: [512]u8 = undefined;
threadlocal var target_len: usize = 0;
threadlocal var method: []const u8 = "";
threadlocal var last_route: []const u8 = "";

pub fn bind(value: []const u8) void {
    route = value;
}

/// The request line this thread is serving, kept for a crash report. The
/// query string is left out: it can carry tokens.
/// Kept after the handler returns: the HTTP library writes the response
/// after that, and a crash there should still name the request.
pub fn bindRequest(request_method: []const u8, target: []const u8) void {
    method = request_method;
    last_route = route;
    const path = target[0 .. std.mem.indexOfScalar(u8, target, '?') orelse target.len];
    target_len = @min(path.len, target_buf.len);
    @memcpy(target_buf[0..target_len], path[0..target_len]);
}

/// Writes the request this thread was serving, if any, for a panic report.
pub fn describeForPanic(w: *std.Io.Writer) void {
    if (target_len == 0) {
        w.writeAll("zds panic on a thread that has served no request\n") catch {};
        return;
    }
    const state = if (std.mem.eql(u8, route, "background")) "after serving" else "while serving";
    w.print("zds panic {s} {s} {s} route={s}\n", .{ state, method, target_buf[0..target_len], last_route }) catch {};
}

pub fn unbind() void {
    route = "background";
    app_len = 0;
    did_len = 0;
}

/// The account and app an authenticated request acted as, copied because
/// the handler's memory is gone by the time telemetry records the request.
threadlocal var did_buf: [128]u8 = undefined;
threadlocal var did_len: usize = 0;
threadlocal var app_buf: [128]u8 = undefined;
threadlocal var app_len: usize = 0;

pub fn noteCaller(did: []const u8, app: []const u8) void {
    if (did.len > did_buf.len) return;
    did_len = did.len;
    @memcpy(did_buf[0..did_len], did);
    app_len = @min(app.len, app_buf.len);
    @memcpy(app_buf[0..app_len], app[0..app_len]);
}

pub const Caller = struct { did: []const u8, app: []const u8 };

pub fn caller() ?Caller {
    if (did_len == 0) return null;
    return .{ .did = did_buf[0..did_len], .app = app_buf[0..app_len] };
}

pub fn currentRoute() []const u8 {
    return route;
}

test "request route context is scoped explicitly" {
    try std.testing.expectEqualStrings("background", currentRoute());
    bind("repo_create_record");
    try std.testing.expectEqualStrings("repo_create_record", currentRoute());
    noteCaller("did:plc:alice", "bsky.app");
    try std.testing.expectEqualStrings("bsky.app", caller().?.app);
    unbind();
    try std.testing.expectEqualStrings("background", currentRoute());
    try std.testing.expect(caller() == null);
}

test "a panic report names the last request a thread served" {
    bind("sync_get_repo");
    bindRequest("GET", "/xrpc/com.atproto.sync.getRepo?did=did:plc:alice&token=secret");
    unbind();
    var buf: [256]u8 = undefined;
    var w = std.Io.Writer.fixed(&buf);
    describeForPanic(&w);
    try std.testing.expectEqualStrings("zds panic after serving GET /xrpc/com.atproto.sync.getRepo route=sync_get_repo\n", w.buffered());
}