-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: control the proxy_upstream in lua side
- Loading branch information
Showing
3 changed files
with
154 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
-- Copyright 2019-2022 Kong Inc. | ||
|
||
-- Licensed 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 _M = {} | ||
|
||
local ffi = require("ffi") | ||
local base = require("resty.core.base") | ||
base.allows_subsystem("http") | ||
|
||
ffi.cdef([[ | ||
int | ||
ngx_http_lua_ffi_set_upstream_next(ngx_http_request_t *r, uint next_upstream, char **err); | ||
]]) | ||
|
||
local type = type | ||
local error = error | ||
local tostring = tostring | ||
local C = ffi.C | ||
local get_request = base.get_request | ||
local ffi_str = ffi.string | ||
|
||
local NGX_OK = ngx.OK | ||
|
||
local next_upstream_table = { | ||
error = 0x00000002, | ||
timeout = 0x00000004, | ||
invalid_header = 0x00000008, | ||
http_500 = 0x00000010, | ||
http_502 = 0x00000020, | ||
http_503 = 0x00000040, | ||
http_504 = 0x00000080, | ||
http_403 = 0x00000100, | ||
http_404 = 0x00000200, | ||
http_429 = 0x00000400, | ||
updating = 0x00000800, | ||
off = 0x00001000, | ||
} | ||
|
||
function _M.set_upstream_next(...) | ||
local nargs = select("#", ...) | ||
if nargs == 0 then | ||
error("no argument") | ||
end | ||
|
||
local r = get_request() | ||
if not r then | ||
error("no request found") | ||
end | ||
|
||
local next_upstream_table = { ... } | ||
local next_upstream = 0 | ||
for i = 1, nargs do | ||
local v = next_upstream_table[i] | ||
if type(v) ~= "number" then | ||
error("argument #" .. i .. " is not a valid argument") | ||
end | ||
|
||
next_upstream = bit.bor(next_upstream, v) | ||
end | ||
|
||
local err = ffi.new("char *[1]") | ||
local rc = C.ngx_http_lua_ffi_set_upstream_next(r, next_upstream, err) | ||
|
||
if rc ~= NGX_OK then | ||
error("failed to set upstream next: " .. tostring(ffi_str(err[0]))) | ||
end | ||
|
||
return true | ||
end | ||
|
||
return _M |
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