-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feature: add batch request plugin. #1388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bea7e49
feat: add api aggreate
ShiningRush 560dbf7
fix: move file and timeout code
ShiningRush c16b85b
feat: add global headers and query for pipeline
ShiningRush 813d291
fix: specify header and query should be high priority
ShiningRush 99b443b
refactor: using plugin to implement the features
ShiningRush 8cf0092
fix: keep init.lua format
ShiningRush 2ab4bcf
fix: change reason when for timeout
ShiningRush 0b82791
fix: add nil check for request body and append invaild body test case
ShiningRush 5964eff
refactor: use default schema check to ensure validation
ShiningRush 29c9b21
fix: lint warnings
ShiningRush 3549860
doc: append aggregate doc
ShiningRush 3acd1b5
fix: lint error
ShiningRush ef3b0e3
trigger travis
ShiningRush 6b8e07d
merger from remote and resolve conflict
ShiningRush b1ec138
fix: remove unnecessary line
ShiningRush a498091
fix: lint error
ShiningRush 95ffdd8
fix: code review
ShiningRush dbb7625
refactor: rename plugin to batch-requets
ShiningRush bc6be48
refactor: fix as reviews
ShiningRush 6c11f42
refactor: as reviews
ShiningRush 8e4882a
refactor: rename message field
ShiningRush File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,241 @@ | ||
| -- | ||
| -- 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 core = require("apisix.core") | ||
| local http = require("resty.http") | ||
| local ngx = ngx | ||
| local io_open = io.open | ||
| local ipairs = ipairs | ||
| local pairs = pairs | ||
|
|
||
| local plugin_name = "batch-requests" | ||
|
|
||
| local schema = { | ||
| type = "object", | ||
| additionalProperties = false, | ||
| } | ||
|
|
||
| local req_schema = { | ||
| type = "object", | ||
| properties = { | ||
| query = { | ||
| description = "pipeline query string", | ||
| type = "object" | ||
| }, | ||
| headers = { | ||
| description = "pipeline header", | ||
| type = "object" | ||
| }, | ||
| timeout = { | ||
| description = "pipeline timeout(ms)", | ||
| type = "integer", | ||
| default = 30000, | ||
| }, | ||
| pipeline = { | ||
| type = "array", | ||
| minItems = 1, | ||
| items = { | ||
| type = "object", | ||
| properties = { | ||
| version = { | ||
| description = "HTTP version", | ||
| type = "number", | ||
| enum = {1.0, 1.1}, | ||
| default = 1.1, | ||
| }, | ||
| method = { | ||
| description = "HTTP method", | ||
| type = "string", | ||
| enum = {"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", | ||
| "OPTIONS", "CONNECT", "TRACE"}, | ||
| default = "GET" | ||
| }, | ||
| path = { | ||
| type = "string", | ||
| minLength = 1, | ||
| }, | ||
| query = { | ||
| description = "request header", | ||
| type = "object", | ||
| }, | ||
| headers = { | ||
| description = "request query string", | ||
| type = "object", | ||
| }, | ||
| ssl_verify = { | ||
| type = "boolean", | ||
| default = false | ||
| }, | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| anyOf = { | ||
| {required = {"pipeline"}}, | ||
| }, | ||
| } | ||
|
|
||
| local _M = { | ||
| version = 0.1, | ||
| priority = 4010, | ||
| name = plugin_name, | ||
| schema = schema | ||
| } | ||
|
|
||
|
|
||
| function _M.check_schema(conf) | ||
| local ok, err = core.schema.check(schema, conf) | ||
| if not ok then | ||
| return false, err | ||
| end | ||
| return true | ||
| end | ||
|
|
||
|
|
||
| local function check_input(data) | ||
| local ok, err = core.schema.check(req_schema, data) | ||
| if not ok then | ||
| return 400, {error_msg = "bad request body: " .. err} | ||
| end | ||
| end | ||
|
|
||
|
|
||
| local function set_common_header(data) | ||
| if not data.headers then | ||
| return | ||
| end | ||
|
|
||
| for i,req in ipairs(data.pipeline) do | ||
| if not req.headers then | ||
| req.headers = data.headers | ||
| else | ||
| for k, v in pairs(data.headers) do | ||
| if not req.headers[k] then | ||
| req.headers[k] = v | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
| local function set_common_query(data) | ||
| if not data.query then | ||
| return | ||
| end | ||
|
|
||
| for i,req in ipairs(data.pipeline) do | ||
| if not req.query then | ||
| req.query = data.query | ||
| else | ||
| for k, v in pairs(data.query) do | ||
| if not req.query[k] then | ||
| req.query[k] = v | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
| local function get_file(file_name) | ||
| local f = io_open(file_name, 'r') | ||
| if f then | ||
| local req_body = f:read("*all") | ||
| f:close() | ||
| return req_body | ||
| end | ||
|
|
||
| return | ||
| end | ||
|
|
||
|
|
||
| local function batch_requests() | ||
| ngx.req.read_body() | ||
| local req_body = ngx.req.get_body_data() | ||
| if not req_body then | ||
| local file_name = ngx.req.get_body_file() | ||
| if file_name then | ||
| req_body = get_file(file_name) | ||
| end | ||
|
|
||
| if not req_body then | ||
| core.response.exit(400, { | ||
| error_msg = "no request body, you should give at least one pipeline setting" | ||
| }) | ||
| end | ||
| end | ||
|
|
||
| local data, err = core.json.decode(req_body) | ||
| if not data then | ||
| core.response.exit(400, { | ||
| error_msg = "invalid request body: " .. req_body .. ", err: " .. err | ||
| }) | ||
| end | ||
|
|
||
| local code, body = check_input(data) | ||
| if code then | ||
| core.response.exit(code, body) | ||
| end | ||
|
|
||
| local httpc = http.new() | ||
| httpc:set_timeout(data.timeout) | ||
| local ok, err = httpc:connect("127.0.0.1", ngx.var.server_port) | ||
| if not ok then | ||
| core.response.exit(500, {error_msg = "connect to apisix failed: " .. err}) | ||
| end | ||
|
|
||
| set_common_header(data) | ||
| set_common_query(data) | ||
| local responses, err = httpc:request_pipeline(data.pipeline) | ||
| if not responses then | ||
| core.response.exit(400, {error_msg = "request failed: " .. err}) | ||
| end | ||
|
|
||
| local aggregated_resp = {} | ||
| for _, resp in ipairs(responses) do | ||
| if not resp.status then | ||
| core.table.insert(aggregated_resp, { | ||
| status = 504, | ||
| reason = "upstream timeout" | ||
| }) | ||
| end | ||
| local sub_resp = { | ||
| status = resp.status, | ||
| reason = resp.reason, | ||
| headers = resp.headers, | ||
| } | ||
| if resp.has_body then | ||
| sub_resp.body = resp:read_body() | ||
| end | ||
| core.table.insert(aggregated_resp, sub_resp) | ||
| end | ||
| core.response.exit(200, aggregated_resp) | ||
| end | ||
|
|
||
|
|
||
| function _M.api() | ||
| return { | ||
| { | ||
| methods = {"POST"}, | ||
| uri = "/apisix/batch-requests", | ||
| handler = batch_requests, | ||
| } | ||
| } | ||
| end | ||
|
|
||
|
|
||
| return _M | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -145,5 +145,6 @@ plugins: # plugin list | |
| - proxy-mirror | ||
| - kafka-logger | ||
| - cors | ||
| - batch-requests | ||
| stream_plugins: | ||
| - mqtt-proxy | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.