-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate grpc-client-nginx-module in APISIX
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
- Loading branch information
1 parent
2a44ba6
commit 745f5f2
Showing
6 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-- | ||
-- 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. | ||
-- | ||
local pcall = pcall | ||
|
||
local ok, mod = pcall(require, "resty.grpc") | ||
if not ok then | ||
-- vanilla OpenResty doesn't have grpc-client-nginx-module | ||
return nil | ||
end | ||
|
||
-- Reimport the `resty.grpc` as `core.grpc`. For the doc of the gRPC API, | ||
-- see https://github.com/api7/grpc-client-nginx-module | ||
return mod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
# | ||
# 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; | ||
|
||
my $nginx_binary = $ENV{'TEST_NGINX_BINARY'} || 'nginx'; | ||
my $version = eval { `$nginx_binary -V 2>&1` }; | ||
|
||
if ($version !~ m/\/apisix-nginx-module/) { | ||
plan(skip_all => "apisix-nginx-module not installed"); | ||
} else { | ||
plan('no_plan'); | ||
} | ||
|
||
add_block_preprocessor(sub { | ||
my ($block) = @_; | ||
|
||
if (!$block->request) { | ||
$block->set_value("request", "GET /t"); | ||
} | ||
|
||
if ((!defined $block->error_log) && (!defined $block->no_error_log)) { | ||
$block->set_value("no_error_log", "[error]"); | ||
} | ||
}); | ||
|
||
run_tests; | ||
|
||
__DATA__ | ||
=== TEST 1: unary | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local core = require "apisix.core" | ||
local gcli = core.grpc | ||
assert(gcli.load("t/grpc_server_example/proto/helloworld.proto")) | ||
local conn = assert(gcli.connect("127.0.0.1:50051")) | ||
local res, err = conn:call("helloworld.Greeter", "SayHello", { | ||
name = "apisix" }) | ||
conn:close() | ||
if not res then | ||
ngx.status = 503 | ||
ngx.say(err) | ||
return | ||
end | ||
ngx.say(res.message) | ||
} | ||
} | ||
--- response_body | ||
Hello apisix | ||
=== TEST 2: server stream | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local core = require "apisix.core" | ||
local gcli = core.grpc | ||
assert(gcli.load("t/grpc_server_example/proto/helloworld.proto")) | ||
local conn = assert(gcli.connect("127.0.0.1:50051")) | ||
local st, err = conn:new_server_stream("helloworld.Greeter", | ||
"SayHelloServerStream", { name = "apisix" }) | ||
if not st then | ||
ngx.status = 503 | ||
ngx.say(err) | ||
return | ||
end | ||
for i = 1, 5 do | ||
local res, err = st:recv() | ||
if not res then | ||
ngx.status = 503 | ||
ngx.say(err) | ||
return | ||
end | ||
ngx.say(res.message) | ||
end | ||
} | ||
} | ||
--- response_body eval | ||
"Hello apisix\n" x 5 | ||
=== TEST 3: client stream | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local core = require "apisix.core" | ||
local gcli = core.grpc | ||
assert(gcli.load("t/grpc_server_example/proto/helloworld.proto")) | ||
local conn = assert(gcli.connect("127.0.0.1:50051")) | ||
local st, err = conn:new_client_stream("helloworld.Greeter", | ||
"SayHelloClientStream", { name = "apisix" }) | ||
if not st then | ||
ngx.status = 503 | ||
ngx.say(err) | ||
return | ||
end | ||
for i = 1, 3 do | ||
local ok, err = st:send({ name = "apisix" }) | ||
if not ok then | ||
ngx.status = 503 | ||
ngx.say(err) | ||
return | ||
end | ||
end | ||
local res, err = st:recv_close() | ||
if not res then | ||
ngx.status = 503 | ||
ngx.say(err) | ||
return | ||
end | ||
ngx.say(res.message) | ||
} | ||
} | ||
--- response_body | ||
Hello apisix!Hello apisix!Hello apisix!Hello apisix! | ||
=== TEST 4: bidirectional stream | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local core = require "apisix.core" | ||
local gcli = core.grpc | ||
assert(gcli.load("t/grpc_server_example/proto/helloworld.proto")) | ||
local conn = assert(gcli.connect("127.0.0.1:50051")) | ||
local st, err = conn:new_bidirectional_stream("helloworld.Greeter", | ||
"SayHelloBidirectionalStream", { name = "apisix" }) | ||
if not st then | ||
ngx.status = 503 | ||
ngx.say(err) | ||
return | ||
end | ||
for i = 1, 3 do | ||
local ok, err = st:send({ name = "apisix" }) | ||
if not ok then | ||
ngx.status = 503 | ||
ngx.say(err) | ||
return | ||
end | ||
end | ||
assert(st:close_send()) | ||
for i = 1, 5 do | ||
local res, err = st:recv() | ||
if not res then | ||
ngx.status = 503 | ||
ngx.say(err) | ||
return | ||
end | ||
ngx.say(res.message) | ||
end | ||
} | ||
} | ||
--- response_body eval | ||
"Hello apisix\n" x 4 . "stream ended\n" |