Skip to content

feat: rerun rewrite phase for newly added plugins in consumer#6502

Merged
spacewander merged 9 commits into
apache:masterfrom
tzssangglass:IssueNo6405
Mar 8, 2022
Merged

feat: rerun rewrite phase for newly added plugins in consumer#6502
spacewander merged 9 commits into
apache:masterfrom
tzssangglass:IssueNo6405

Conversation

@tzssangglass

Copy link
Copy Markdown
Member

Closes #6405

Signed-off-by: tzssangglass [email protected]

What this PR does / why we need it:

Pre-submission checklist:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

@tzssangglass

Copy link
Copy Markdown
Member Author

only call core.consumer.attach_consumer is considered to be an auth class plugin.

ref: #2898

Signed-off-by: tzssangglass <[email protected]>
Signed-off-by: tzssangglass <[email protected]>
Signed-off-by: tzssangglass <[email protected]>
@spacewander spacewander changed the title fix: rerun rewrite phase for newly added plugins in consumer feat: rerun rewrite phase for newly added plugins in consumer Mar 4, 2022
Comment thread apisix/init.lua Outdated
local runned_plugins = core.table.deepcopy(api_ctx.plugins)
core.table.clear(api_ctx.plugins)
api_ctx.plugins = plugin.filter(api_ctx, route, api_ctx.plugins)
local unrunn_plugins

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add a flag in

new_route_conf.value.plugins[name] = conf
so that we can save allocation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update

Signed-off-by: tzssangglass <[email protected]>
Signed-off-by: tzssangglass <[email protected]>
Comment thread apisix/init.lua Outdated
Comment thread apisix/plugin.lua
Comment thread apisix/plugin.lua
@@ -503,6 +503,11 @@ local function merge_consumer_route(route_conf, consumer_conf)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use if new_route_conf.value.plugins[name] == nil then conf._from_consumer = true?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried, Test 2 will fail. We need to merge the `new_route_conf.value.plugins["proxy-rewrite"], it will not be nil.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this change?

diff --git apisix/plugin.lua apisix/plugin.lua
index d66ec045..190b0833 100644
--- apisix/plugin.lua
+++ apisix/plugin.lua
@@ -502,12 +502,11 @@ local function merge_consumer_route(route_conf, consumer_conf)
             new_route_conf.value.plugins = {}
         end

-        new_route_conf.value.plugins[name] = conf
-
-        if (route_conf and route_conf.value and route_conf.value.plugins)
-                and not route_conf.value.plugins[name] then
-            new_route_conf.value.plugins[name]["_from_consumer"] = true
+        if new_route_conf.value.plugins[name] == nil then
+            conf._from_consumer = true
         end
+
+        new_route_conf.value.plugins[name] = conf
     end

     core.log.info("merged conf : ", core.json.delay_encode(new_route_conf))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, update

Comment thread apisix/plugin.lua Outdated
function _M.rerun_plugins_of_consumer(plugins, api_ctx)
for i = 1, #plugins, 2 do
-- no need to rerun the auth plugins
if plugins[i + 1]["_from_consumer"] and plugins[i].type ~= "auth" then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to merge it in

if phase_func then
?

@tzssangglass tzssangglass Mar 6, 2022

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had thought about it, but if plugins[i + 1]["_from_consumer"] and plugins[i].type ~= "auth" then is difficult to port. Unless we add a flag or something like that. This makes run_plugin look even more complicated.

Another way is as follows:

    for i = 1, #plugins, 2 do
        -- no need to rerun the non-auth plugins
        if plugins[i + 1]["_from_consumer"] and plugins[i].type ~= "auth" then
            local new_plugins = core.table.new(2, 0)
            core.table.insert(new_plugins, plugins[i])
            core.table.insert(new_plugins, plugins[i + 1])
            _M.run_plugin("rewrite", new_plugins, api_ctx)
        end
    end
    return api_ctx

this adds some table overhead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason is that the plugins here are used in the api_ctx in order to reduce the table overhead, so use the judgment condition.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add another fake phase (rewrite_in_consumer) in run_plugin so we don't need to duplicate the conditions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update

Signed-off-by: tzssangglass <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

request help: The plugins that act on the specified consumer do not take effect

5 participants