Skip to content

Commit

Permalink
fix(grpc) allow setting :authority on balancer phase
Browse files Browse the repository at this point in the history
  • Loading branch information
gszr committed Aug 18, 2021
1 parent 808202d commit 6f54813
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ resty.kong.grpc.set\_authority
------------------------------
**syntax:** *ok, err = resty.kong.grpc.set_authority(new_authority)*

**context:** *rewrite_by_lua*, access_by_lua*
**context:** *rewrite_by_lua*, access_by_lua*, balancer_by_lua**

**subsystems:** *http*

Expand All @@ -274,6 +274,9 @@ describing the error will be returned.
This function can be called multiple times in the same request. Later calls override
previous ones.

If called in the `balancer_by_lua` context, the request needs to be recreated
(see [balancer.recreate](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/balancer.md#recreate_request).

[Back to TOC](#table-of-contents)

resty.kong.tls.disable\_proxy\_ssl
Expand Down
1 change: 1 addition & 0 deletions lualib/resty/kong/grpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ do
local ALLOWED_PHASES = {
['rewrite'] = true,
['access'] = true,
['balancer'] = true,
}

function _M.set_authority(authority)
Expand Down
59 changes: 57 additions & 2 deletions t/003-grpc.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

repeat_each(2);

plan tests => repeat_each() * (blocks() * 5);

my $pwd = cwd();
Expand Down Expand Up @@ -225,3 +223,60 @@ falseincorrect argument, expects a string, got number
[error]
[crit]
[alert]
=== TEST 6: recreate upstream module requests with authority header change
--- http_config
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;";
server {
listen 127.0.0.1:12346 http2;
server_name example.com;
server_tokens off;
location / {
default_type 'text/plain';
more_clear_headers Date;
echo ':authority: $http_host';
}
}
upstream backend {
server 0.0.0.1;
balancer_by_lua_block {
print("here")
local b = require "ngx.balancer"
local grpc = require("resty.kong.grpc")
if ngx.ctx.balancer_run then
print"IF"
grpc.set_authority("try2")
assert(b.set_current_peer("127.0.0.1", 12346))
assert(b.recreate_request())
else
print"ELSE"
grpc.set_authority("try1")
ngx.ctx.balancer_run = true
assert(b.set_current_peer("127.0.0.3", 12345))
assert(b.set_more_tries(1))
end
}
}
--- config
location = /t {
grpc_pass grpc://backend;
}
--- request
GET /t
--- response_body
:authority: try2
--- error_log
connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1
--- no_error_log
[warn]
[crit]

0 comments on commit 6f54813

Please sign in to comment.