Fix ring buffer

This commit is contained in:
Dominic Grimm 2025-06-01 18:15:51 +02:00
parent e70a04ac16
commit c9c2b45b2a
Signed by: dergrimm
SSH key fingerprint: SHA256:0uoWpcqOtkyvQ+ZqBjNYiDqIZY+9s8VeZkkJ/4ryB4E
17 changed files with 330 additions and 135 deletions

View file

@ -0,0 +1,50 @@
const std = @import("std");
const msg_queue = @import("../msg_queue/root.zig");
const models = @import("../models/root.zig");
const Self = @This();
const log = std.log.scoped(.block_update_dispatcher);
pub const Config = struct {
capacity: usize,
};
allocator: std.mem.Allocator,
config: *const Config,
queue: *msg_queue.MsgQueueUnmanaged(models.BlockUpdateMsg),
stream: ?std.net.Stream = null,
pub fn init(
allocator: std.mem.Allocator,
config: *const Config,
queue: *msg_queue.MsgQueueUnmanaged(models.BlockUpdateMsg),
stream: std.net.Stream,
) Self {
return .{
.allocator = allocator,
.config = config,
.queue = queue,
.stream = stream,
};
}
fn send(self: *Self, update: *const models.BlockUpdateMsg) !void {
var buf: [models.BlockUpdateMsg.csv_max_len]u8 = undefined;
const stream = self.stream.?;
const csv = try update.toCsv(&buf);
try stream.writeAll(csv);
try stream.writeAll(&.{'\n'});
}
pub fn start(self: *Self) !void {
const buf = try self.allocator.alloc(models.BlockUpdateMsg, self.queue.capacity());
defer self.allocator.free(buf);
while (true) {
const updates = try self.queue.tryDequeueAll(buf);
for (updates) |*update| try self.send(update);
}
}

View file

@ -0,0 +1 @@
pub const BlockUpdate = @import("BlockUpdate.zig");