aboutsummaryrefslogtreecommitdiff
path: root/src/storage/session-manager/root.zig
blob: 8a59f2110ecd68905277b2faac01daee1e0aa0cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const std = @import("std");
const config = @import("../../config.zig");
const Storage = @import("../root.zig");

pub const Session = @import("session.zig");

const Self = @This();

pub const empty: Self = .{
    .cache = .empty,
};

cache: std.StringHashMapUnmanaged(Session),
rw_lock: std.Thread.RwLock = .{},

pub fn add(self: *Self, storage: *Storage, info: Storage.User.Info) !*Session {
    self.rw_lock.lock();
    defer self.rw_lock.unlock();

    const session = try Session.init(storage.allocator, info);
    errdefer session.deinit(storage.allocator);
    try self.cache.put(storage.allocator, session.fingerprint, session);
    return self.cache.getPtr(session.fingerprint) orelse unreachable;
}

pub fn renew(self: *Self, storage: *Storage, fingerprint: []const u8) !*Session {
    self.rw_lock.lockShared();
    if (self.cache.get(fingerprint)) |s| {
        self.rw_lock.unlockShared();
        self.rw_lock.lock();
        defer self.rw_lock.unlock();

        var session = s;
        try session.reset();

        try self.cache.put(storage.allocator, session.fingerprint, session);

        _ = self.cache.remove(fingerprint);

        return self.cache.getPtr(session.fingerprint) orelse unreachable;
    } else {
        self.rw_lock.unlockShared();
    }
    return error.SessionNotFound;
}

pub fn remove(self: *Self, storage: *Storage, fingerprint: []const u8) void {
    self.rw_lock.lockShared();
    if (self.cache.getPtr(fingerprint)) |session| {
        self.rw_lock.unlockShared();
        self.rw_lock.lock();
        defer self.rw_lock.unlock();

        session.deinit(storage.allocator);
        _ = self.cache.remove(fingerprint);
    } else {
        self.rw_lock.unlockShared();
    }
}

pub fn get(self: *Self, storage: *Storage, fingerprint: []const u8) ?*Session {
    self.rw_lock.lockShared();
    defer self.rw_lock.unlockShared();
    if (self.cache.getPtr(fingerprint)) |session| {
        const now = std.time.Instant.now() catch return null;
        const since = now.since(session.age);

        if (since > storage.config.session_expires_after) {
            session.deinit(storage.allocator);
            _ = self.cache.remove(fingerprint);
            return null;
        }

        return session;
    }

    return null;
}