summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2026-01-01 17:21:17 +0000
committerRobby Zambito <contact@robbyzambito.me>2026-01-01 18:50:52 +0000
commit86558986efff134680ac14aae605bbd80eaba4d5 (patch)
tree383548542af93f6947975d676bc5de87d7fc4835
parentca95c9a06c3cfcdd759be30d764b9446308a9add (diff)
Slower, but probably more correct
-rw-r--r--src/server/main.zig22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/server/main.zig b/src/server/main.zig
index fb4e698..e622304 100644
--- a/src/server/main.zig
+++ b/src/server/main.zig
@@ -97,14 +97,16 @@ fn addClient(server: *Server, allocator: std.mem.Allocator, id: usize, client: *
fn removeClient(server: *Server, io: std.Io, allocator: std.mem.Allocator, id: usize) void {
server.subs_lock.lockUncancelable(io);
defer server.subs_lock.unlock(io);
- _ = server.clients.remove(id);
- const len = server.subscriptions.items.len;
- for (0..len) |i| {
- const sub = server.subscriptions.items[len - i - 1];
- if (sub.client_id == id) {
- allocator.free(sub.sid);
- allocator.free(sub.subject);
- _ = server.subscriptions.swapRemove(i);
+ if (server.clients.remove(id)) {
+ const len = server.subscriptions.items.len;
+ for (0..len) |from_end| {
+ const i = len - from_end - 1;
+ const sub = server.subscriptions.items[i];
+ if (sub.client_id == id) {
+ allocator.free(sub.sid);
+ allocator.free(sub.subject);
+ _ = server.subscriptions.swapRemove(i);
+ }
}
}
}
@@ -138,7 +140,7 @@ fn handleConnection(
try server.addClient(server_allocator, id, &client);
defer server.removeClient(io, server_allocator, id);
- var qbuf: [1024]Message = undefined;
+ var qbuf: [16]Message = undefined;
var queue: std.Io.Queue(Message) = .init(&qbuf);
var client_task = try io.concurrent(Client.start, .{ &client, io, server_allocator, &queue });
@@ -207,7 +209,7 @@ fn publishMessage(server: *Server, io: std.Io, alloc: std.mem.Allocator, source_
for (server.subscriptions.items) |subscription| {
if (subjectMatches(subscription.subject, msg.subject)) {
const client = server.clients.get(subscription.client_id) orelse {
- std.debug.print("trying to publish to a client that no longer exists: {d}", .{subscription.client_id});
+ std.debug.print("trying to publish to a client that no longer exists: {d}\n", .{subscription.client_id});
continue;
};
client.send(io, .{ .msg = .{