From dab10464fee9c61f9688521400825093901c45e4 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Sun, 15 Oct 2023 14:47:22 -0500 Subject: [PATCH] fixes --- src/main.zig | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/main.zig b/src/main.zig index 1d19b7a..9758cfd 100644 --- a/src/main.zig +++ b/src/main.zig @@ -67,9 +67,11 @@ fn readConfig(allocator: std.mem.Allocator, path: []const u8) !ConfigWrapper(Con .arena = try allocator.create(std.heap.ArenaAllocator), .value = undefined, }; - config.arena.child_allocator = allocator; - const data = try std.fs.cwd().readFileAlloc(config.arena.child_allocator, path, 1024); - config.value = try std.json.parseFromSliceLeaky(Config, config.arena.child_allocator, data, .{}); + errdefer allocator.destroy(config.arena); + config.arena.* = std.heap.ArenaAllocator.init(allocator); + errdefer config.arena.deinit(); + const data = try std.fs.cwd().readFileAlloc(config.arena.allocator(), path, 1024); + config.value = try std.json.parseFromSliceLeaky(Config, config.arena.allocator(), data, .{}); return config; } @@ -102,7 +104,10 @@ pub fn main() !void { var password_buffer: [128]u8 = undefined; var password: []const u8 = undefined; if (config.value.loki.password_file) |password_file_path| { - password = try std.fs.cwd().readFile(password_file_path, &password_buffer); + password = std.fs.cwd().readFile(password_file_path, &password_buffer) catch |err| { + iperf3_log.err("error trying to read password file {s}: {}", .{ password_file_path, err }); + return; + }; password = std.mem.trimRight(u8, password, "\r\n"); } else if (config.value.loki.password) |password_data| { @memcpy(&password_buffer, password_data); @@ -133,9 +138,6 @@ pub fn main() !void { const uri = try std.Uri.parse(url); - var client = std.http.Client{ .allocator = allocator }; - defer client.deinit(); - var port_buf: [16]u8 = undefined; var port: []u8 = undefined; if (config.value.iperf3.port) |p| { @@ -169,15 +171,15 @@ pub fn main() !void { "--json", "--one-off", }, - arena.child_allocator, + arena.allocator(), ); c.stdin_behavior = .Ignore; c.stdout_behavior = .Pipe; c.stderr_behavior = .Pipe; try c.spawn(); - var stdout = std.ArrayList(u8).init(arena.child_allocator); - var stderr = std.ArrayList(u8).init(arena.child_allocator); + var stdout = std.ArrayList(u8).init(arena.allocator()); + var stderr = std.ArrayList(u8).init(arena.allocator()); iperf3_log.info("waiting for data", .{}); @@ -197,7 +199,7 @@ pub fn main() !void { var obj_or_err = std.json.parseFromSliceLeaky( iperf3.IPerfReturn, - arena.child_allocator, + arena.allocator(), stdout.items, .{}, ); @@ -243,7 +245,7 @@ pub fn main() !void { }, }; - var data = std.ArrayList(u8).init(allocator); + var data = std.ArrayList(u8).init(arena.allocator()); try std.json.stringify( streams, .{ @@ -260,13 +262,22 @@ pub fn main() !void { .{data.items.len}, ); - var headers = std.http.Headers{ .allocator = allocator }; + var client = std.http.Client{ .allocator = arena.allocator() }; + defer client.deinit(); + + var headers = std.http.Headers{ .allocator = arena.allocator() }; try headers.append("Authorization", auth_header); try headers.append("Accept", "application/json"); try headers.append("Content-Type", "application/json"); try headers.append("Content-Length", content_length); + defer headers.deinit(); - var req = try client.request(.POST, uri, headers, .{}); + var req = try client.request( + .POST, + uri, + headers, + .{}, + ); defer req.deinit(); iperf3_log.info("sending stream to loki", .{});