Repository navigation
Expand file tree
/
Copy pathglobal.lua
More file actions
255 lines (190 loc) · 5.74 KB
/
Copy pathglobal.lua
File metadata and controls
255 lines (190 loc) · 5.74 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
-- TODO: get rid of 'kong.meta'; this module is king
local meta = require "kong.meta"
local PDK = require "kong.pdk"
local phase_checker = require "kong.pdk.private.phases"
local kong_cache = require "kong.cache"
local kong_cluster_events = require "kong.cluster_events"
local kong_constants = require "kong.constants"
local type = type
local setmetatable = setmetatable
local KONG_VERSION = tostring(meta._VERSION)
local KONG_VERSION_NUM = tonumber(string.format("%d%.2d%.2d",
meta._VERSION_TABLE.major * 100,
meta._VERSION_TABLE.minor * 10,
meta._VERSION_TABLE.patch))
local LOCK_OPTS = {
exptime = 10,
timeout = 5,
}
-- Runloop interface
local _GLOBAL = {
phases = phase_checker.phases,
}
function _GLOBAL.new()
return {
version = KONG_VERSION,
version_num = KONG_VERSION_NUM,
pdk_major_version = nil,
pdk_version = nil,
configuration = nil,
}
end
function _GLOBAL.set_named_ctx(self, name, key)
if not self then
error("arg #1 cannot be nil", 2)
end
if type(name) ~= "string" then
error("name must be a string", 2)
end
if #name == 0 then
error("name cannot be an empty string", 2)
end
if key == nil then
error("key cannot be nil", 2)
end
if not self.ctx then
error("ctx PDK module not initialized", 2)
end
self.ctx.__set_namespace(name, key)
end
function _GLOBAL.del_named_ctx(self, name)
if not self then
error("arg #1 cannot be nil", 2)
end
if type(name) ~= "string" then
error("name must be a string", 2)
end
if #name == 0 then
error("name cannot be an empty string", 2)
end
if not self.ctx then
error("ctx PDK module not initialized", 2)
end
self.ctx.__del_namespace(name)
end
function _GLOBAL.set_phase(self, phase)
if not self then
error("arg #1 cannot be nil", 2)
end
local kctx = self.ctx
if not kctx then
error("ctx PDK module not initialized", 2)
end
kctx.core.phase = phase
end
function _GLOBAL.get_phase(self)
if not self then
error("arg #1 cannot be nil", 2)
end
local kctx = self.ctx
if not kctx then
error("ctx SDK module not initialized", 2)
end
return kctx.core.phase
end
do
local log_facilities = setmetatable({}, { __index = "k" })
function _GLOBAL.set_namespaced_log(self, namespace)
if not self then
error("arg #1 cannot be nil", 2)
end
if type(namespace) ~= "string" then
error("namespace (arg #2) must be a string", 2)
end
if not self.ctx then
error("ctx PDK module not initialized", 2)
end
local log = log_facilities[namespace]
if not log then
log = self.core_log.new(namespace) -- use default namespaced format
log_facilities[namespace] = log
end
self.ctx.core.log = log
end
function _GLOBAL.reset_log(self)
if not self then
error("arg #1 cannot be nil", 2)
end
if not self.ctx then
error("ctx PDK module not initialized", 2)
end
self.ctx.core.log = self.core_log
end
end
function _GLOBAL.init_pdk(self, kong_config, pdk_major_version)
if not self then
error("arg #1 cannot be nil", 2)
end
PDK.new(kong_config, pdk_major_version, self)
end
function _GLOBAL.init_worker_events()
-- Note: worker_events will not work correctly if required at the top of the file.
-- It must be required right here, inside the init function
local worker_events = require "resty.worker.events"
local ok, err = worker_events.configure {
shm = "kong_process_events", -- defined by "lua_shared_dict"
timeout = 5, -- life time of event data in shm
interval = 1, -- poll interval (seconds)
wait_interval = 0.010, -- wait before retry fetching event data
wait_max = 0.5, -- max wait time before discarding event
}
if not ok then
return nil, err
end
return worker_events
end
function _GLOBAL.init_cluster_events(kong_config, db)
return kong_cluster_events.new({
db = db,
poll_interval = kong_config.db_update_frequency,
poll_offset = kong_config.db_update_propagation,
poll_delay = kong_config.db_update_propagation,
})
end
function _GLOBAL.init_cache(kong_config, cluster_events, worker_events)
local db_cache_ttl = kong_config.db_cache_ttl
local db_cache_neg_ttl = kong_config.db_cache_neg_ttl
local page = 1
local cache_pages = 1
if kong_config.database == "off" then
db_cache_ttl = 0
db_cache_neg_ttl = 0
cache_pages = 2
page = ngx.shared.kong:get(kong_constants.DECLARATIVE_PAGE_KEY) or page
end
return kong_cache.new {
shm_name = "kong_db_cache",
cluster_events = cluster_events,
worker_events = worker_events,
ttl = db_cache_ttl,
neg_ttl = db_cache_neg_ttl or db_cache_ttl,
resurrect_ttl = kong_config.resurrect_ttl,
page = page,
cache_pages = cache_pages,
resty_lock_opts = LOCK_OPTS,
}
end
function _GLOBAL.init_core_cache(kong_config, cluster_events, worker_events)
local db_cache_ttl = kong_config.db_cache_ttl
local db_cache_neg_ttl = kong_config.db_cache_neg_ttl
local page = 1
local cache_pages = 1
if kong_config.database == "off" then
db_cache_ttl = 0
db_cache_neg_ttl = 0
cache_pages = 2
page = ngx.shared.kong:get(kong_constants.DECLARATIVE_PAGE_KEY) or page
end
return kong_cache.new {
shm_name = "kong_core_db_cache",
cluster_events = cluster_events,
worker_events = worker_events,
ttl = db_cache_ttl,
neg_ttl = db_cache_neg_ttl or db_cache_ttl,
resurrect_ttl = kong_config.resurrect_ttl,
page = page,
cache_pages = cache_pages,
resty_lock_opts = LOCK_OPTS,
}
end
return _GLOBAL