Something went wrong. Try again.
declarative relay deployment on hetzner relay-eval.waow.tech
atproto relay
Something went wrong. Try again.
3.2 kB · 99 lines
Zig
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100const std = @import("std");const zat = @import("zat");
const log = std.log.scoped(.classifier);
pub const Classification = enum { active_missing, no_pds_endpoint, malformed_did, unsupported_did_method, did_resolution_failed, invalid_did_document, classification_not_attempted,
pub fn toString(self: Classification) []const u8 { return switch (self) { .active_missing => "active_missing", .no_pds_endpoint => "no_pds_endpoint", .malformed_did => "malformed_did", .unsupported_did_method => "unsupported_did_method", .did_resolution_failed => "did_resolution_failed", .invalid_did_document => "invalid_did_document", .classification_not_attempted => "classification_not_attempted", }; }};
pub const classification_version: u16 = 2;
pub const ClassifiedDid = struct { did: []const u8, classification: Classification, pds: ?[]const u8 = null,};
/// classify DIDs that appear in one relay but not another./// `max_count` is a real classification budget: callers should store/// classification_not_attempted for misses outside this returned set.pub fn classifyDids( allocator: std.mem.Allocator, dids: []const []const u8, max_count: usize,) ![]ClassifiedDid { var resolver = zat.DidResolver.init(std.Options.debug_io, allocator); defer resolver.deinit();
var results: std.ArrayList(ClassifiedDid) = .empty; errdefer results.deinit(allocator);
const limit = @min(dids.len, max_count); for (dids[0..limit], 0..) |did, i| { if (i > 0 and i % 50 == 0) { log.info("classified {d}/{d} DIDs", .{ i, limit }); } const classified = classifyOne(allocator, &resolver, did); try results.append(allocator, .{ .did = did, .classification = classified.classification, .pds = classified.pds, }); } log.info("classified {d}/{d} DIDs", .{ limit, limit });
return results.toOwnedSlice(allocator);}
pub fn freeClassified(allocator: std.mem.Allocator, classified: []ClassifiedDid) void { for (classified) |cd| { if (cd.pds) |p| allocator.free(p); } allocator.free(classified);}
const OneResult = struct { classification: Classification, pds: ?[]const u8 = null,};
fn classifyOne(allocator: std.mem.Allocator, resolver: *zat.DidResolver, did_str: []const u8) OneResult { const parsed = zat.Did.parse(did_str) orelse return .{ .classification = .malformed_did }; var doc = resolver.resolve(parsed) catch |err| { log.debug("resolve failed for {s}: {s}", .{ did_str, @errorName(err) }); return .{ .classification = switch (err) { error.UnsupportedDidMethod => .unsupported_did_method, error.InvalidDidDocument => .invalid_did_document, else => .did_resolution_failed, } }; }; defer doc.deinit();
if (doc.pdsEndpoint()) |pds| { return .{ .classification = .active_missing, .pds = allocator.dupe(u8, pds) catch null, }; }
return .{ .classification = .no_pds_endpoint };}