Skip to content

Commit

Permalink
fix(*): lazily initialize structures to avoid c-boundary errors on re…
Browse files Browse the repository at this point in the history
…quire

### Summary

Moves the code that was previously running on `module` level to `initialize` function
that are lazily called when needed. This is to avoid the `yield accross C-boundary`
errors during `require`.
  • Loading branch information
bungle committed Sep 19, 2023
1 parent 1dbf894 commit 5eecd21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/resty/aws/credentials/CredentialProviderChain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ CredentialProviderChain.__index = CredentialProviderChain
local aws_config = require("resty.aws.config")


CredentialProviderChain.defaultProviders = {} do
CredentialProviderChain.defaultProviders = {}


local function initialize()
-- while not everything is implemented this will load what we do have without
-- failing on what is missing. Will auto pick up newly added classes afterwards.
local function add_if_exists(name, opts)
Expand Down Expand Up @@ -41,6 +44,8 @@ CredentialProviderChain.defaultProviders = {} do
else
add_if_exists("EC2MetadataCredentials")
end

initialize = nil
end

--- Constructor, inherits from `Credentials`.
Expand All @@ -65,6 +70,11 @@ end
-- @param opt options table, additional fields to the `Credentials` class:
-- @param opt.providers array of `Credentials` objects or functions (functions must return a `Credentials` object)
function CredentialProviderChain:new(opts)
if initialize then
initialize()
initialize = nil
end

local self = Super:new(opts) -- override 'self' to be the new object/class
setmetatable(self, CredentialProviderChain)

Expand Down
13 changes: 10 additions & 3 deletions src/resty/aws/credentials/RemoteCredentials.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ local http = require "resty.luasocket.http"
local json = require "cjson"


local FullUri do
-- construct the URL
local FullUri


local function initialize()
-- construct the URL
local function makeset(t)
for i = 1, #t do
t[t[i]] = true
Expand Down Expand Up @@ -73,8 +75,9 @@ local FullUri do
FullUri.port = FullUri.port or
({ http = 80, https = 443 })[FullUri.scheme]
end
end

initialize = nil
end


-- Create class
Expand All @@ -96,6 +99,10 @@ end
-- updates credentials.
-- @return success, or nil+err
function RemoteCredentials:refresh()
if initialize then
initialize()
end

if not FullUri then
return nil, "No URI environment variables found for RemoteCredentials"
end
Expand Down

0 comments on commit 5eecd21

Please sign in to comment.