Przeglądaj źródła

fix startup and persistence leak

Danilo Fragoso 9 miesięcy temu
rodzic
commit
afe8ae6221
1 zmienionych plików z 16 dodań i 8 usunięć
  1. 16 8
      persistence.zig

+ 16 - 8
persistence.zig

@@ -6,6 +6,7 @@ var storage_file: ?std.fs.File = null;
 var thread_pool: std.Thread.Pool = undefined;
 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
 const allocator = arena.allocator();
+const c_allocator = std.heap.c_allocator;
 
 var mutex: std.Thread.Mutex = .{};
 
@@ -15,14 +16,14 @@ const OPCode = enum {
 };
 
 pub fn init() !void {
-    try std.Thread.Pool.init(&thread_pool, .{ .allocator = allocator, .n_jobs = 1024 });
+    try std.Thread.Pool.init(&thread_pool, .{ .allocator = allocator, .n_jobs = 4 });
 
     const cwd = std.fs.cwd();
     storage_file = cwd.openFile(".db", .{ .mode = .read_write }) catch |err| {
         if (err == std.fs.File.OpenError.FileNotFound) {
             std.debug.print("No persisted data found, starting fresh...\n", .{});
 
-            storage_file = cwd.createFile(".db", .{}) catch |ierr| {
+            storage_file = cwd.createFile(".db", .{ .read = true }) catch |ierr| {
                 std.debug.print("Failed to create storage file: {any}\n", .{ierr});
                 return;
             };
@@ -32,11 +33,11 @@ pub fn init() !void {
         return;
     };
 
-    const storage_data = storage_file.?.readToEndAlloc(allocator, MAX_PERSISTENCE_SIZE) catch |err| {
+    const storage_data = storage_file.?.readToEndAlloc(c_allocator, MAX_PERSISTENCE_SIZE) catch |err| {
         std.debug.print("Failed to read storage file: {any}\n", .{err});
         return;
     };
-    defer allocator.free(storage_data);
+    defer c_allocator.free(storage_data);
     var records = std.mem.splitScalar(u8, storage_data, '\r');
     var record_count: usize = 0;
     while (records.next()) |record| {
@@ -61,17 +62,22 @@ pub fn init() !void {
         }
     }
     std.debug.print("Restored {d} records from persistence", .{record_count});
+
+    storage_file.?.close();
+    storage_file = cwd.openFile(".db", .{ .mode = .write_only }) catch |err| {
+        std.debug.print("Failed to reopen storage file in append mode: {any}\n", .{err});
+        return;
+    };
+    try storage_file.?.seekFromEnd(0);
+
     return;
 }
 
 pub fn persist(opcode: u8, key: []const u8, value: []const u8) void {
-    mutex.lock();
-    const record = std.fmt.allocPrint(allocator, "{c}|{s}|{s}\r", .{ opcode, key, value }) catch {
+    const record = std.fmt.allocPrint(c_allocator, "{c}|{s}|{s}\r", .{ opcode, key, value }) catch {
         std.debug.print("Failed to format record for persistence\n", .{});
-        mutex.unlock();
         return;
     };
-    mutex.unlock();
 
     thread_pool.spawn(persistRecord, .{record}) catch |err| {
         std.debug.print("Failed to spawn persistence job: {any}\n", .{err});
@@ -80,6 +86,8 @@ pub fn persist(opcode: u8, key: []const u8, value: []const u8) void {
 }
 
 fn persistRecord(record: []const u8) void {
+    defer c_allocator.free(record);
+
     mutex.lock();
     defer mutex.unlock();