This is an automated email from the ASF dual-hosted git repository. membphis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push: new c030783 fix: support upstream_id & consumer with grpc (#3387) c030783 is described below commit c03078360761e51aceb25f8edd018724ac071abd Author: 罗泽轩 <spacewander...@gmail.com> AuthorDate: Thu Jan 21 09:38:19 2021 -0600 fix: support upstream_id & consumer with grpc (#3387) Fix #1646. --- apisix/init.lua | 74 ++++------------------------------------ t/node/grpc-proxy.t | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 68 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index 4f13603..a33248f 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -379,10 +379,6 @@ function _M.http_access_phase() core.log.info("matched route: ", core.json.delay_encode(api_ctx.matched_route, true)) - if route.value.service_protocol == "grpc" then - return ngx.exec("@grpc_pass") - end - local enable_websocket = route.value.enable_websocket if route.value.service_id then local service = service_fetch(route.value.service_id) @@ -523,6 +519,11 @@ function _M.http_access_phase() set_upstream_host(api_ctx) + if route.value.service_protocol == "grpc" then + ngx_var.ctx_ref = ctxdump.stash_ngx_ctx() + return ngx.exec("@grpc_pass") + end + if api_ctx.dubbo_proxy_enabled then ngx_var.ctx_ref = ctxdump.stash_ngx_ctx() return ngx.exec("@dubbo_pass") @@ -536,70 +537,7 @@ end function _M.grpc_access_phase() - local ngx_ctx = ngx.ctx - local api_ctx = ngx_ctx.api_ctx - - if not api_ctx then - api_ctx = core.tablepool.fetch("api_ctx", 0, 32) - ngx_ctx.api_ctx = api_ctx - end - - core.ctx.set_vars_meta(api_ctx) - - router.router_http.match(api_ctx) - - core.log.info("route: ", - core.json.delay_encode(api_ctx.matched_route, true)) - - local route = api_ctx.matched_route - if not route then - return core.response.exit(404) - end - - if route.value.service_id then - -- core.log.info("matched route: ", core.json.delay_encode(route.value)) - local service = service_fetch(route.value.service_id) - if not service then - core.log.error("failed to fetch service configuration by ", - "id: ", route.value.service_id) - return core.response.exit(404) - end - - local changed - route, changed = plugin.merge_service_route(service, route) - api_ctx.matched_route = route - - if changed then - api_ctx.conf_type = "route&service" - api_ctx.conf_version = route.modifiedIndex .. "&" - .. service.modifiedIndex - api_ctx.conf_id = route.value.id .. "&" - .. service.value.id - else - api_ctx.conf_type = "service" - api_ctx.conf_version = service.modifiedIndex - api_ctx.conf_id = service.value.id - end - - else - api_ctx.conf_type = "route" - api_ctx.conf_version = route.modifiedIndex - api_ctx.conf_id = route.value.id - end - - -- todo: support upstream id - - api_ctx.matched_upstream = (route.dns_value and - route.dns_value.upstream) - or route.value.upstream - - local plugins = core.tablepool.fetch("plugins", 32, 0) - api_ctx.plugins = plugin.filter(route, plugins) - - run_plugin("rewrite", plugins, api_ctx) - run_plugin("access", plugins, api_ctx) - - set_upstream(route, api_ctx) + ngx.ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref) end diff --git a/t/node/grpc-proxy.t b/t/node/grpc-proxy.t new file mode 100644 index 0000000..02ee658 --- /dev/null +++ b/t/node/grpc-proxy.t @@ -0,0 +1,97 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +# As the test framework doesn't support sending grpc request, this +# test file is only for grpc irrelative configuration check. +# To avoid confusion, we configure a closed port so if th configuration works, +# the result will be `connect refused`. +repeat_each(1); +log_level('info'); +no_root_location(); +no_shuffle(); + +add_block_preprocessor(sub { + my ($block) = @_; + + my $yaml_config = $block->yaml_config // <<_EOC_; +apisix: + node_listen: 1984 + config_center: yaml + enable_admin: false +_EOC_ + + $block->set_value("yaml_config", $yaml_config); + + if (!$block->request) { + $block->set_value("request", "POST /hello"); + } +}); + +run_tests(); + +__DATA__ + +=== TEST 1: with upstream_id +--- apisix_yaml +upstreams: + - id: 1 + type: roundrobin + nodes: + "127.0.0.1:9088": 1 +routes: + - id: 1 + methods: + - POST + service_protocol: grpc + uri: "/hello" + upstream_id: 1 +#END +--- error_code: 502 +--- error_log +proxy request to 127.0.0.1:9088 + + + +=== TEST 2: with consummer +--- apisix_yaml +consumers: + - username: jack + id: jack + plugins: + key-auth: + key: user-key +#END +routes: + - id: 1 + methods: + - POST + service_protocol: grpc + uri: "/hello" + plugins: + key-auth: + consumer-restriction: + whitelist: + - jack + upstream: + type: roundrobin + nodes: + "127.0.0.1:9088": 1 +#END +--- more_headers +apikey: user-key +--- error_code: 502