aboutsummaryrefslogtreecommitdiffstats
path: root/server/resty/session/storage/dshm.lua
blob: e6d887f114797d6487a8d75e6aa264ce693cb0ac (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
local dshm         = require "resty.dshm"

local setmetatable = setmetatable
local tonumber     = tonumber
local concat       = table.concat
local var          = ngx.var

local defaults = {
    region            = var.session_dshm_region                         or "sessions",
    connect_timeout   = tonumber(var.session_dshm_connect_timeout, 10),
    read_timeout      = tonumber(var.session_dshm_read_timeout,    10),
    send_timeout      = tonumber(var.session_dshm_send_timeout,    10),
    host              = var.session_dshm_host                           or "127.0.0.1",
    port              = tonumber(var.session_dshm_port,            10)  or 4321,
    pool              = {
        name          = var.session_dshm_pool_name,
        size          = tonumber(var.session_dshm_pool_size,       10)  or 100,
        timeout       = tonumber(var.session_dshm_pool_timeout,    10)  or 1000,
        backlog       = tonumber(var.session_dshm_pool_backlog,    10),
    },
}

local storage = {}

storage.__index = storage

function storage.new(session)
    local config = session.dshm or defaults
    local pool   = config.pool  or defaults.pool

    local connect_timeout = tonumber(config.connect_timeout, 10) or defaults.connect_timeout

    local store = dshm:new()
    if store.set_timeouts then
        local send_timeout = tonumber(config.send_timeout, 10) or defaults.send_timeout
        local read_timeout = tonumber(config.read_timeout, 10) or defaults.read_timeout

        if connect_timeout then
            if send_timeout and read_timeout then
                store:set_timeouts(connect_timeout, send_timeout, read_timeout)
            else
                store:set_timeout(connect_timeout)
            end
        end

    elseif store.set_timeout and connect_timeout then
        store:set_timeout(connect_timeout)
    end


    local self = {
        store        = store,
        encoder      = session.encoder,
        region       = config.region                      or defaults.region,
        host         = config.host                        or defaults.host,
        port         = tonumber(config.port,          10) or defaults.port,
        pool_timeout = tonumber(pool.timeout,         10) or defaults.pool.timeout,
        connect_opts = {
            pool             = pool.name                  or defaults.pool.name,
            pool_size        = tonumber(pool.size,    10) or defaults.pool.size,
            backlog          = tonumber(pool.backlog, 10) or defaults.pool.backlog,
        },
    }

    return setmetatable(self, storage)
end

function storage:connect()
    return self.store:connect(self.host, self.port, self.connect_opts)
end

function storage:set_keepalive()
    return self.store:set_keepalive(self.pool_timeout)
end

function storage:key(id)
    return concat({ self.region, id }, "::")
end

function storage:set(key, ttl, data)
    local ok, err = self:connect()
    if not ok then
        return nil, err
    end

    data, err = self.encoder.encode(data)

    if not data then
        self:set_keepalive()
        return nil, err
    end

    ok, err = self.store:set(key, data, ttl)

    self:set_keepalive()

    return ok, err
end

function storage:get(key)
    local ok, err = self:connect()
    if not ok then
        return nil, err
    end

    local data
    data, err = self.store:get(key)
    if data then
        data, err = self.encoder.decode(data)
    end

    self:set_keepalive()

    return data, err
end

function storage:delete(key)
    local ok, err = self:connect()
    if not ok then
        return nil, err
    end

    ok, err = self.store:delete(key)

    self:set_keepalive()

    return ok, err
end

function storage:touch(key, ttl)
    local ok, err = self:connect()
    if not ok then
        return nil, err
    end

    ok, err = self.store:touch(key, ttl)

    self:set_keepalive()

    return ok, err
end

function storage:open(id)
    local key = self:key(id)
    return self:get(key)
end

function storage:save(id, ttl, data)
    local key = self:key(id)
    return self:set(key, ttl, data)
end

function storage:destroy(id)
    local key = self:key(id)
    return self:delete(key)
end

function storage:ttl(id, ttl)
    local key = self:key(id)
    return self:touch(key, ttl)
end

return storage