Getting started
Add the dependency
Section titled “Add the dependency”In a new project, run:
mkdir zigrupt-democd zigrupt-demozig initzig fetch --save=zigrupt git+https://github.com/sarthakvk/zigrupt.gitIn build.zig, add these lines after the exe declaration so main.zig can
import zigrupt:
const zigrupt = b.dependency("zigrupt", .{ .target = target, .optimize = optimize,});exe.root_module.addImport("zigrupt", zigrupt.module("zigrupt"));Try it
Section titled “Try it”Replace src/main.zig with:
const std = @import("std");const zigrupt = @import("zigrupt");
const Event = struct { value: usize };
fn handle(events: []const Event) usize { for (events) |event| std.debug.print("{d}\n", .{event.value}); return events.len;}
const BusySpin = zigrupt.waiting_strategy.BusySpin;const Bus = zigrupt.EventBus(Event, false, &.{handle}, BusySpin, BusySpin);
pub fn main() !void { var bus = try Bus.init(std.heap.page_allocator, 64, 8); defer bus.deinit(); try bus.start(); errdefer bus.stop() catch {};
try bus.produce(.{ .value = 42 }); try bus.stop();}zig build runThis prints 42.
Choose capacity and batch size
Section titled “Choose capacity and batch size”In Bus.init(allocator, 64, 8), 64 is the ring capacity and 8 is the
maximum number of events offered to a handler in one callback. Capacity must
be a nonzero power of two, and the batch limit must be between one and capacity
inclusive.
A larger ring absorbs longer bursts but uses more storage; it does not remove slow-handler backpressure. The batch limit controls how much work a handler receives at once, not how long the bus waits to collect it. See batching for acknowledgement and retry rules.
The event type, producer mode, handlers, and waiting strategies are fixed when
defining Bus; capacity and batch size are chosen when initializing each
instance. The example uses one producer and one handler. See
producer modes before publishing concurrently.
Continue with concepts and event ownership before passing pointers or slices. API docs.
