This repository was archived by the owner on Dec 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
fix: required changes for 3.1 compatibility #36
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
14256e9
temp: ngx.decode_base64url
omegabytes 2f294c1
replace lua resty.openssl.digest with go crypto/sha256
omegabytes 4ac298b
rename pkg crypto to sha256
omegabytes b6e369b
replace lua resty.string.to_hex with go encoding/hex
omegabytes 788f46c
replace table.nkeys with local fn
omegabytes 86df807
fmt and lint
omegabytes ff8aeb1
remove log_level from constants
omegabytes ab748dd
replace table.new with local fn
omegabytes 393daa9
remove/replace resty.* imports
omegabytes 739a308
rework goto statements
omegabytes 001809f
remove ffi imports from node.lua
omegabytes 884c12f
pdk/private/node.lua remove pl imports
omegabytes 23c7b76
remove ngx imports from node and reports
omegabytes 0345f15
override nkeys for router/utils
omegabytes ea9bcf7
remove ngx.worker.id ref
omegabytes b47d2e6
just get rid of atc already
omegabytes 33e4b3c
refactor: name go.hex and go.sha256 pkg
omegabytes 9798f25
empty atc and node files
omegabytes 8bd2cdc
remove commented code
omegabytes a7b66b9
remove debug traceback
omegabytes 74a909b
move new_tab and nkeys to kong.tools.utils
omegabytes fc044a8
remove node, add newlines
omegabytes cddf098
revert reports changes
omegabytes 821f5f7
revert router.utils changes
omegabytes 4c25d00
update patchfile
omegabytes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,283 +1 @@ | ||
| --- Node-level utilities. | ||
| -- | ||
| -- @module kong.node | ||
|
|
||
| local utils = require "kong.tools.utils" | ||
| local ffi = require "ffi" | ||
| local private_node = require "kong.pdk.private.node" | ||
|
|
||
|
|
||
| local floor = math.floor | ||
| local lower = string.lower | ||
| local match = string.match | ||
| local gsub = string.gsub | ||
| local sort = table.sort | ||
| local insert = table.insert | ||
| local ngx = ngx | ||
| local shared = ngx.shared | ||
| local C = ffi.C | ||
| local ffi_new = ffi.new | ||
| local ffi_str = ffi.string | ||
|
|
||
| local NODE_ID_KEY = "kong:node_id" | ||
|
|
||
|
|
||
| local node_id | ||
| local shms = {} | ||
| local n_workers = ngx.worker.count() | ||
|
|
||
|
|
||
| for shm_name, shm in pairs(shared) do | ||
| insert(shms, { | ||
| zone = shm, | ||
| name = shm_name, | ||
| capacity = shm:capacity(), | ||
| }) | ||
| end | ||
|
|
||
|
|
||
| local function convert_bytes(bytes, unit, scale) | ||
| if not unit or lower(unit) == "b" then | ||
| return floor(bytes) | ||
| end | ||
|
|
||
| return utils.bytes_to_str(bytes, unit, scale) | ||
| end | ||
|
|
||
|
|
||
| local function sort_pid_asc(a, b) | ||
| return a.pid < b.pid | ||
| end | ||
|
|
||
|
|
||
| local function new(self) | ||
| local _NODE = {} | ||
|
|
||
|
|
||
| --- | ||
| -- Returns the ID used by this node to describe itself. | ||
| -- | ||
| -- @function kong.node.get_id | ||
| -- @treturn string The v4 UUID used by this node as its ID. | ||
| -- @usage | ||
| -- local id = kong.node.get_id() | ||
| function _NODE.get_id() | ||
| if node_id then | ||
| return node_id | ||
| end | ||
|
|
||
| local shm = ngx.shared.kong | ||
|
|
||
| local ok, err = shm:safe_add(NODE_ID_KEY, utils.uuid()) | ||
| if not ok and err ~= "exists" then | ||
| error("failed to set 'node_id' in shm: " .. err) | ||
| end | ||
|
|
||
| node_id, err = shm:get(NODE_ID_KEY) | ||
| if err then | ||
| error("failed to get 'node_id' in shm: " .. err) | ||
| end | ||
|
|
||
| if not node_id then | ||
| error("no 'node_id' set in shm") | ||
| end | ||
|
|
||
| return node_id | ||
| end | ||
|
|
||
|
|
||
| --- | ||
| -- Returns memory usage statistics about this node. | ||
| -- | ||
| -- @function kong.node.get_memory_stats | ||
| -- @tparam[opt] string unit The unit that memory is reported in. Can be | ||
| -- any of `b/B`, `k/K`, `m/M`, or `g/G` for bytes, kibibytes, mebibytes, | ||
| -- or gibibytes, respectively. Defaults to `b` (bytes). | ||
| -- @tparam[opt] number scale The number of digits to the right of the decimal | ||
| -- point. Defaults to 2. | ||
| -- @treturn table A table containing memory usage statistics for this node. | ||
| -- If `unit` is `b/B` (the default), reported values are Lua numbers. | ||
| -- Otherwise, reported values are strings with the unit as a suffix. | ||
| -- @usage | ||
| -- local res = kong.node.get_memory_stats() | ||
| -- -- res will have the following structure: | ||
| -- { | ||
| -- lua_shared_dicts = { | ||
| -- kong = { | ||
| -- allocated_slabs = 12288, | ||
| -- capacity = 24576 | ||
| -- }, | ||
| -- kong_db_cache = { | ||
| -- allocated_slabs = 12288, | ||
| -- capacity = 12288 | ||
| -- } | ||
| -- }, | ||
| -- workers_lua_vms = { | ||
| -- { | ||
| -- http_allocated_gc = 1102, | ||
| -- pid = 18004 | ||
| -- }, | ||
| -- { | ||
| -- http_allocated_gc = 1102, | ||
| -- pid = 18005 | ||
| -- } | ||
| -- } | ||
| -- } | ||
| -- | ||
| -- local res = kong.node.get_memory_stats("k", 1) | ||
| -- -- res will have the following structure: | ||
| -- { | ||
| -- lua_shared_dicts = { | ||
| -- kong = { | ||
| -- allocated_slabs = "12.0 KiB", | ||
| -- capacity = "24.0 KiB", | ||
| -- }, | ||
| -- kong_db_cache = { | ||
| -- allocated_slabs = "12.0 KiB", | ||
| -- capacity = "12.0 KiB", | ||
| -- } | ||
| -- }, | ||
| -- workers_lua_vms = { | ||
| -- { | ||
| -- http_allocated_gc = "1.1 KiB", | ||
| -- pid = 18004 | ||
| -- }, | ||
| -- { | ||
| -- http_allocated_gc = "1.1 KiB", | ||
| -- pid = 18005 | ||
| -- } | ||
| -- } | ||
| -- } | ||
| function _NODE.get_memory_stats(unit, scale) | ||
| -- validate arguments | ||
|
|
||
| do | ||
| unit = unit or "b" | ||
| scale = scale or 2 | ||
|
|
||
| local pok, perr = pcall(utils.bytes_to_str, 0, unit, scale) | ||
| if not pok then | ||
| error(perr, 2) | ||
| end | ||
| end | ||
|
|
||
| local res = { | ||
| workers_lua_vms = self.table.new(n_workers, 0), | ||
| lua_shared_dicts = self.table.new(0, #shms), | ||
| } | ||
|
|
||
| -- get workers Lua VM allocated memory | ||
|
|
||
| do | ||
| if not shared.kong then | ||
| goto lua_shared_dicts | ||
| end | ||
|
|
||
| local keys, err = shared.kong:get_keys() | ||
| if not keys then | ||
| res.workers_lua_vms.err = "could not get kong shm keys: " .. err | ||
| goto lua_shared_dicts | ||
| end | ||
|
|
||
| if #keys == 1024 then | ||
| -- Preventive warning log for future Kong developers, in case 'kong' | ||
| -- shm becomes mis-used or over-used. | ||
| ngx.log(ngx.WARN, "ngx.shared.kong:get_keys() returned 1024 keys, ", | ||
| "but it may have more") | ||
| end | ||
|
|
||
| for i = 1, #keys do | ||
| local pid = match(keys[i], "kong:mem:(%d+)") | ||
| if not pid then | ||
| goto continue | ||
| end | ||
|
|
||
| local w = self.table.new(0, 2) | ||
| w.pid = tonumber(pid) | ||
|
|
||
| local count, err = shared.kong:get("kong:mem:" .. pid) | ||
| if err then | ||
| w.err = "could not get worker's HTTP Lua VM memory (pid: " .. | ||
| pid .. "): " .. err | ||
|
|
||
| elseif type(count) ~= "number" then | ||
| w.err = "could not get worker's HTTP Lua VM memory (pid: " .. | ||
| pid .. "): reported value is corrupted" | ||
|
|
||
| else | ||
| count = count * 1024 -- reported value is in kb | ||
| w.http_allocated_gc = convert_bytes(count, unit, scale) | ||
| end | ||
|
|
||
| insert(res.workers_lua_vms, w) | ||
|
|
||
| ::continue:: | ||
| end | ||
|
|
||
| sort(res.workers_lua_vms, sort_pid_asc) | ||
| end | ||
|
|
||
| -- get lua_shared_dicts allocated slabs | ||
| ::lua_shared_dicts:: | ||
|
|
||
| for _, shm in ipairs(shms) do | ||
| local allocated = shm.capacity - shm.zone:free_space() | ||
|
|
||
| res.lua_shared_dicts[shm.name] = { | ||
| capacity = convert_bytes(shm.capacity, unit, scale), | ||
| allocated_slabs = convert_bytes(allocated, unit, scale), | ||
| } | ||
| end | ||
|
|
||
| return res | ||
| end | ||
|
|
||
|
|
||
| --- | ||
| -- Returns the name used by the local machine. | ||
| -- | ||
| -- @function kong.node.get_hostname | ||
| -- @treturn string The local machine hostname. | ||
| -- @usage | ||
| -- local hostname = kong.node.get_hostname() | ||
| function _NODE.get_hostname() | ||
| local SIZE = 253 -- max number of chars for a hostname | ||
|
|
||
| local buf = ffi_new("unsigned char[?]", SIZE) | ||
| local res = C.gethostname(buf, SIZE) | ||
|
|
||
| if res == 0 then | ||
| local hostname = ffi_str(buf, SIZE) | ||
| return gsub(hostname, "%z+$", "") | ||
| end | ||
|
|
||
| local f = io.popen("/bin/hostname") | ||
| local hostname = f:read("*a") or "" | ||
| f:close() | ||
| return gsub(hostname, "\n$", "") | ||
| end | ||
|
|
||
|
|
||
| local prefix = self and self.configuration and self.configuration.prefix | ||
| if prefix and self.configuration.role == "data_plane" then | ||
| local id, err = private_node.load_node_id(prefix) | ||
| if id then | ||
| node_id = id | ||
| ngx.log(ngx.DEBUG, "restored node_id from the filesystem: ", node_id) | ||
|
|
||
| else | ||
| id = _NODE.get_id() | ||
| if err then | ||
| ngx.log(ngx.WARN, "failed to restore node_id from the filesystem: ", | ||
| err, ", so a new one was generated: ", id) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return _NODE | ||
| end | ||
|
|
||
|
|
||
| return { | ||
| new = new, | ||
| } | ||
| -- not used in koko at this time |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.