From 771d7d7b4cd0dcfb516034bd47bd757305cf7810 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Fri, 4 Apr 2025 12:09:21 -0700 Subject: [PATCH] add connection pool to hopefully eliminate unexpected connection failures --- src/main.zig | 8 +++++++- src/root.zig | 6 ++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main.zig b/src/main.zig index 5ff82c3..bcc7752 100644 --- a/src/main.zig +++ b/src/main.zig @@ -64,12 +64,18 @@ pub fn main() !u8 { try stdout.print("Monitoring Syncthing events at {s}\n", .{config.syncthing_url}); var last_id: ?i64 = null; + const connection_pool = std.http.Client.ConnectionPool{}; while (true) { var arena_alloc = std.heap.ArenaAllocator.init(allocator); defer arena_alloc.deinit(); const arena = arena_alloc.allocator(); - var poller = try EventPoller.init(arena, api_key, config); + var poller = try EventPoller.init( + arena, + api_key, + config, + connection_pool, + ); defer last_id = poller.last_id; poller.last_id = last_id; const events = poller.poll() catch |err| switch (err) { diff --git a/src/root.zig b/src/root.zig index a5fcfff..de4128b 100644 --- a/src/root.zig +++ b/src/root.zig @@ -75,18 +75,20 @@ pub const EventPoller = struct { config: Config, last_id: ?i64, api_key: []u8, + connection_pool: std.http.Client.ConnectionPool, - pub fn init(allocator: std.mem.Allocator, api_key: []u8, config: Config) !EventPoller { + pub fn init(allocator: std.mem.Allocator, api_key: []u8, config: Config, connection_pool: ?std.http.Client.ConnectionPool) !EventPoller { return .{ .allocator = allocator, .config = config, .last_id = null, .api_key = api_key, + .connection_pool = connection_pool orelse .{}, }; } pub fn poll(self: *EventPoller) ![]SyncthingEvent { - var client = std.http.Client{ .allocator = self.allocator }; + var client = std.http.Client{ .allocator = self.allocator, .connection_pool = self.connection_pool }; var arena = std.heap.ArenaAllocator.init(self.allocator); defer arena.deinit(); const aa = arena.allocator();