diff --git a/gateway/src/apicast/policy/grpc/grpc.lua b/gateway/src/apicast/policy/grpc/grpc.lua index 22df2938d..cf4397122 100644 --- a/gateway/src/apicast/policy/grpc/grpc.lua +++ b/gateway/src/apicast/policy/grpc/grpc.lua @@ -3,65 +3,21 @@ local policy = require('apicast.policy') local _M = policy.new('grpc', "builtin") local resty_url = require('resty.url') -local round_robin = require 'resty.balancer.round_robin' +local apicast_balancer = require('apicast.balancer') local new = _M.new -local balancer = round_robin.new() - function _M.new(config) local self = new(config) return self end function _M:rewrite(context) - if ngx.var.server_protocol == "HTTP/2.0" then -- upstream defined in gateway/conf.d/http2.conf context.upstream_location_name = "@grpc_upstream" - end -end - -function _M:content(context) - -- This is needed within the combination of the routing policy, if not the - -- upstream got overwritten and balancer phase is called before. - if not context.upstream_location_name then - return - end - - if ngx.var.server_protocol ~= "HTTP/2.0" then - ngx.var.host = context.upstream_location_name - end - + ngx.var.proxy_host = "upstream" end -function _M:balancer(context) - if not context.upstream_location_name then - return - end - - -- balancer need to be used due to grpc_pass does not support variables and - -- upstream block need to be in place. - local upstream = context:get_upstream() - if not upstream then - ngx.log(ngx.WARN, "Upstream is not present in the balancer") - return - end - - local peers = balancer:peers(upstream.servers) - local peer, err = balancer:select_peer(peers) - if err then - ngx.log(ngx.WARN, "Cannot get a peer for the given upstream: ", err) - return - end - - local ip = peer[1] - local port = peer[2] or upstream.uri.port or resty_url.default_port(upstream.uri.scheme) - local _, err = balancer:set_current_peer(ip, port) - - if err then - ngx.log(ngx.WARN, "Cannot set balancer IP and port '", ip, ":", port, "'") - return - end -end +_M.balancer = apicast_balancer.call return _M diff --git a/gateway/src/apicast/upstream.lua b/gateway/src/apicast/upstream.lua index 74221974e..b4c246dcb 100644 --- a/gateway/src/apicast/upstream.lua +++ b/gateway/src/apicast/upstream.lua @@ -221,7 +221,9 @@ function _M:call(context) end if not self.servers then self:resolve() end - + if context.upstream_location_name then + self.location_name = context.upstream_location_name + end context[self.upstream_name] = self return exec(self)