Much better cleanup, still fast B)

This commit is contained in:
2026-01-02 14:11:00 +00:00
parent aca265f095
commit 810e5f8211
2 changed files with 17 additions and 7 deletions

View File

@@ -233,12 +233,12 @@ fn publishMessage(server: *Server, io: std.Io, alloc: std.mem.Allocator, source_
std.debug.print("trying to publish to a client that no longer exists: {d}\n", .{subscription.client_id}); std.debug.print("trying to publish to a client that no longer exists: {d}\n", .{subscription.client_id});
continue; continue;
}; };
client.send(io, .{ .msg = .{ client.send(io, .{
.subject = try alloc.dupe(u8, msg.subject), .msg = try msg.toMsg(alloc, subscription.sid),
.sid = try alloc.dupe(u8, subscription.sid), }) catch |err| switch (err) {
.reply_to = if (msg.reply_to) |r| try alloc.dupe(u8, r) else null, error.Canceled => return err,
.payload = try alloc.dupe(u8, msg.payload), else => {},
} }) catch continue; };
} }
} }
if (source_client.connect) |c| { if (source_client.connect) |c| {

View File

@@ -137,6 +137,16 @@ pub const Message = union(MessageType) {
alloc.free(self.payload); alloc.free(self.payload);
if (self.reply_to) |r| alloc.free(r); if (self.reply_to) |r| alloc.free(r);
} }
pub fn toMsg(self: Pub, alloc: std.mem.Allocator, sid: []const u8) !Msg {
const res: Msg = .{
.subject = self.subject,
.sid = sid,
.reply_to = self.reply_to,
.payload = self.payload,
};
return res.dupe(alloc);
}
}; };
pub const Sub = struct { pub const Sub = struct {
/// The subject name to subscribe to. /// The subject name to subscribe to.
@@ -177,7 +187,7 @@ pub const Message = union(MessageType) {
errdefer alloc.free(res.subject); errdefer alloc.free(res.subject);
res.sid = try alloc.dupe(u8, self.sid); res.sid = try alloc.dupe(u8, self.sid);
errdefer alloc.free(res.sid); errdefer alloc.free(res.sid);
res.reply_to = if (self.reply_to) |r| alloc.dupe(u8, r) else null; res.reply_to = if (self.reply_to) |r| try alloc.dupe(u8, r) else null;
errdefer if (res.reply_to) |r| alloc.free(r); errdefer if (res.reply_to) |r| alloc.free(r);
res.payload = try alloc.dupe(u8, self.payload); res.payload = try alloc.dupe(u8, self.payload);
errdefer alloc.free(res.payload); errdefer alloc.free(res.payload);