From 4ecf3652d18a3d304dfff2b759d7c897af199d33 Mon Sep 17 00:00:00 2001 From: Drew Nutter Date: Tue, 26 Mar 2024 09:51:43 -0400 Subject: [PATCH] refactor(gossip): dump service in own thread make the gossip dump service work like the other gossip components, where a thread is spawned, and the entire thing exits if it dies. --- src/gossip/dump_service.zig | 4 ++-- src/gossip/service.zig | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/gossip/dump_service.zig b/src/gossip/dump_service.zig index 1ad80d69a..b74364912 100644 --- a/src/gossip/dump_service.zig +++ b/src/gossip/dump_service.zig @@ -17,7 +17,7 @@ pub const GossipDumpService = struct { const Self = @This(); - pub fn run(self: *Self) !void { + pub fn run(self: Self) !void { const start_time = std.time.timestamp(); const dir = try std.fmt.allocPrint(self.allocator, "gossip-dumps/{}", .{start_time}); defer self.allocator.free(dir); @@ -29,7 +29,7 @@ pub const GossipDumpService = struct { } } - fn dumpGossip(self: *Self, dir: []const u8, start_time: i64) !void { + fn dumpGossip(self: *const Self, dir: []const u8, start_time: i64) !void { const data = blk: { var gossip_table_lock = self.gossip_table_rw.read(); defer gossip_table_lock.unlock(); diff --git a/src/gossip/service.zig b/src/gossip/service.zig index bb539acb0..d4e7708ac 100644 --- a/src/gossip/service.zig +++ b/src/gossip/service.zig @@ -364,15 +364,13 @@ pub const GossipService = struct { }); defer self.joinAndExit(&responder_handle); - if (dump) { - var dump_svc = GossipDumpService{ - .allocator = self.allocator, - .logger = self.logger, - .gossip_table_rw = &self.gossip_table_rw, - .exit = self.exit, - }; - try dump_svc.run(); - } + var dump_handle = if (dump) try Thread.spawn(.{}, GossipDumpService.run, .{.{ + .allocator = self.allocator, + .logger = self.logger, + .gossip_table_rw = &self.gossip_table_rw, + .exit = self.exit, + }}) else null; + defer if (dump_handle) |*h| self.joinAndExit(h); } fn sortSlices(slices: anytype) void {