Skip to content

Commit

Permalink
cache hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Oct 28, 2024
1 parent 16aa4b6 commit 6090ebc
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions kong/pdk/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ end


local function new(self)
local _NODE = {}
local _NODE = {
hostname = nil,
}


---
Expand Down Expand Up @@ -247,20 +249,23 @@ local function new(self)
-- @usage
-- local hostname = kong.node.get_hostname()
function _NODE.get_hostname()
local SIZE = 253 -- max number of chars for a hostname
if not _NODE.hostname then
local SIZE = 253 -- max number of chars for a hostname

local buf = ffi_new("unsigned char[?]", SIZE)
local res = C.gethostname(buf, SIZE)
local buf = ffi_new("unsigned char[?]", SIZE)
local res = C.gethostname(buf, SIZE)

if res ~= 0 then
-- Return an empty string "" instead of nil and error message,
-- because strerror is not thread-safe and the behavior of strerror_r
-- is inconsistent across different systems.
return ""
if res ~= 0 then
-- Return an empty string "" instead of nil and error message,
-- because strerror is not thread-safe and the behavior of strerror_r
-- is inconsistent across different systems.
return ""
end

_NODE.hostname = gsub(ffi_str(buf, SIZE), "%z+$", "")
end

local hostname = ffi_str(buf, SIZE)
return gsub(hostname, "%z+$", "")
return _NODE.hostname
end


Expand Down

0 comments on commit 6090ebc

Please sign in to comment.