better logging for cache

This commit is contained in:
Muki Kiboigo
2026-03-30 10:52:20 -07:00
parent 201fba6362
commit d06dd1208f
2 changed files with 39 additions and 11 deletions

View File

@@ -39,6 +39,7 @@ pub const Scope = enum {
telemetry, telemetry,
unknown_prop, unknown_prop,
mcp, mcp,
cache,
}; };
const Opts = struct { const Opts = struct {

View File

@@ -17,6 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std"); const std = @import("std");
const log = @import("../../log.zig");
const Cache = @import("Cache.zig"); const Cache = @import("Cache.zig");
const Http = @import("../http.zig"); const Http = @import("../http.zig");
const CacheRequest = Cache.CacheRequest; const CacheRequest = Cache.CacheRequest;
@@ -93,8 +94,25 @@ pub fn get(self: *FsCache, arena: std.mem.Allocator, req: CacheRequest) ?Cache.C
lock.lock(); lock.lock();
defer lock.unlock(); defer lock.unlock();
const file = self.dir.openFile(&cache_p, .{ .mode = .read_only }) catch return null; const file = self.dir.openFile(&cache_p, .{ .mode = .read_only }) catch |e| {
errdefer file.close(); switch (e) {
std.fs.File.OpenError.FileNotFound => {
log.debug(.cache, "miss", .{ .url = req.url, .hash = &hashed_key });
},
else => |err| {
log.warn(.cache, "open file err", .{ .url = req.url, .err = err });
},
}
return null;
};
var cleanup = false;
defer if (cleanup) {
file.close();
self.dir.deleteFile(&cache_p) catch |e| {
log.err(.cache, "clean fail", .{ .url = req.url, .file = &cache_p, .err = e });
};
};
var file_buf: [1024]u8 = undefined; var file_buf: [1024]u8 = undefined;
var len_buf: [BODY_LEN_HEADER_LEN]u8 = undefined; var len_buf: [BODY_LEN_HEADER_LEN]u8 = undefined;
@@ -102,27 +120,35 @@ pub fn get(self: *FsCache, arena: std.mem.Allocator, req: CacheRequest) ?Cache.C
var file_reader = file.reader(&file_buf); var file_reader = file.reader(&file_buf);
const file_reader_iface = &file_reader.interface; const file_reader_iface = &file_reader.interface;
file_reader_iface.readSliceAll(&len_buf) catch return null; file_reader_iface.readSliceAll(&len_buf) catch |e| {
log.warn(.cache, "read header", .{ .url = req.url, .err = e });
cleanup = true;
return null;
};
const body_len = std.mem.readInt(u64, &len_buf, .little); const body_len = std.mem.readInt(u64, &len_buf, .little);
// Now we read metadata. // Now we read metadata.
file_reader.seekTo(body_len + BODY_LEN_HEADER_LEN) catch return null; file_reader.seekTo(body_len + BODY_LEN_HEADER_LEN) catch |e| {
log.warn(.cache, "seek metadata", .{ .url = req.url, .err = e });
cleanup = true;
return null;
};
var json_reader = std.json.Reader.init(arena, file_reader_iface); var json_reader = std.json.Reader.init(arena, file_reader_iface);
const cache_file: CacheMetadataJson = std.json.parseFromTokenSourceLeaky( const cache_file: CacheMetadataJson = std.json.parseFromTokenSourceLeaky(
CacheMetadataJson, CacheMetadataJson,
arena, arena,
&json_reader, &json_reader,
.{ .{ .allocate = .alloc_always },
.allocate = .alloc_always, ) catch |e| {
}, log.warn(.cache, "metadata parse", .{ .url = req.url, .err = e });
) catch { cleanup = true;
self.dir.deleteFile(&cache_p) catch {};
return null; return null;
}; };
if (cache_file.version != CACHE_VERSION) { if (cache_file.version != CACHE_VERSION) {
self.dir.deleteFile(&cache_p) catch {}; log.warn(.cache, "version", .{ .url = req.url, .expected = CACHE_VERSION, .got = cache_file.version });
cleanup = true;
return null; return null;
} }
@@ -131,7 +157,8 @@ pub fn get(self: *FsCache, arena: std.mem.Allocator, req: CacheRequest) ?Cache.C
const now = req.timestamp; const now = req.timestamp;
const age = (now - metadata.stored_at) + @as(i64, @intCast(metadata.age_at_store)); const age = (now - metadata.stored_at) + @as(i64, @intCast(metadata.age_at_store));
if (age < 0 or @as(u64, @intCast(age)) >= metadata.cache_control.max_age) { if (age < 0 or @as(u64, @intCast(age)) >= metadata.cache_control.max_age) {
self.dir.deleteFile(&cache_p) catch {}; log.debug(.cache, "expired", .{ .url = req.url });
cleanup = true;
return null; return null;
} }