Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@ on:
jobs:
build:
strategy:
fail-fast: false
matrix:
op_version:
- "1.25.3.1"
- "1.27.1.2"
runtime:
- name: apisix-runtime
script_url: "https://raw.githubusercontent.com/api7/apisix-build-tools/master/build-apisix-runtime.sh"
script_name: "build-apisix-runtime.sh"
exclude_tests: ""
- name: api7ee-runtime
script_url: "https://raw.githubusercontent.com/api7/apisix-build-tools/release/api7ee-runtime/build-api7ee-runtime.sh"
script_name: "build-api7ee-runtime.sh"
# Tests requiring nginx features not available in OR 1.21 (nginx 1.21)
exclude_tests: "t/pipe.t t/upstream_pass_trailers.t"

runs-on: "ubuntu-latest"
name: build (${{ matrix.runtime.name }})

env:
OPENRESTY_VERSION: ${{ matrix.op_version }}
OPENRESTY_PREFIX: "/usr/local/openresty"

steps:
- name: Check out code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up build environment
run: |
Expand All @@ -43,13 +51,20 @@ jobs:

- name: Install
run: |
wget https://raw.githubusercontent.com/api7/apisix-build-tools/master/build-apisix-base.sh
chmod +x build-apisix-base.sh
OR_PREFIX=$OPENRESTY_PREFIX CC="gcc -fsanitize=address -fdiagnostics-color=always -Wno-unused-but-set-variable -Wno-unused-parameter" \
cc_opt="-Werror" ./build-apisix-base.sh latest

wget ${{ matrix.runtime.script_url }}
chmod +x ${{ matrix.runtime.script_name }}
# Fix permission issue: EE script does rm without sudo after sudo make install
sed -i 's|rm -rf "$OPENSSL_PREFIX"/share|sudo rm -rf "$OPENSSL_PREFIX"/share|' ${{ matrix.runtime.script_name }}
OR_PREFIX=$OPENRESTY_PREFIX CC="gcc -fsanitize=address -fdiagnostics-color=always -Werror -Wno-unused-but-set-variable -Wno-unused-parameter" \
./${{ matrix.runtime.script_name }} latest
Comment thread
jarvis9443 marked this conversation as resolved.
Comment thread
jarvis9443 marked this conversation as resolved.

- name: Script
run: |
export PATH=$OPENRESTY_PREFIX/nginx/sbin:$PATH
prove -I. -Itest-nginx/lib -r t/
TEST_FILES=$(find t/ -name '*.t' -type f | sort)
if [ -n "${{ matrix.runtime.exclude_tests }}" ]; then
for f in ${{ matrix.runtime.exclude_tests }}; do
TEST_FILES=$(echo "$TEST_FILES" | grep -v "^${f}$")
done
fi
echo "$TEST_FILES" | xargs prove -I. -Itest-nginx/lib
57 changes: 57 additions & 0 deletions lib/resty/apisix/upstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ ngx_int_t ngx_http_apisix_upstream_set_ssl_trusted_store(ngx_http_request_t *r,
int ngx_http_apisix_upstream_set_ssl_verify(ngx_http_request_t *r, int verify);

ngx_int_t ngx_http_apisix_set_upstream_pass_trailers(ngx_http_request_t *r, int on);

ngx_int_t ngx_http_apisix_push_upstream_state(ngx_http_request_t *r,
const unsigned char *addr, size_t addr_len, ngx_int_t status,
ngx_int_t connect_time_ms, ngx_int_t header_time_ms);
ngx_int_t ngx_http_apisix_update_upstream_state(ngx_http_request_t *r,
ngx_int_t response_time_ms, intptr_t response_length);
Comment thread
jarvis9443 marked this conversation as resolved.
]])
local _M = {}

Expand Down Expand Up @@ -124,4 +130,55 @@ function _M.set_pass_trailers(on)
end


function _M.push_upstream_state(opts)
if type(opts) ~= "table" then
return nil, "opts must be a table"
end

local r = get_request()
if not r then
return nil, "no request found"
end

local addr = opts.addr
if addr ~= nil and type(addr) ~= "string" then
return nil, "addr must be a string"
end
local addr_len = addr and #addr or 0
local ret = C.ngx_http_apisix_push_upstream_state(
r,
addr, addr_len,
opts.status or 0,
opts.connect_time or -1,
opts.header_time or -1)
Comment thread
jarvis9443 marked this conversation as resolved.
Comment thread
jarvis9443 marked this conversation as resolved.
Comment thread
jarvis9443 marked this conversation as resolved.
Comment thread
jarvis9443 marked this conversation as resolved.
if ret == NGX_ERROR then
return nil, "error while pushing upstream state"
end

return true
end


function _M.update_upstream_state(opts)
if type(opts) ~= "table" then
return nil, "opts must be a table"
end

local r = get_request()
if not r then
return nil, "no request found"
end

local ret = C.ngx_http_apisix_update_upstream_state(
r,
opts.response_time or -1,
opts.response_length or -1)
Comment thread
jarvis9443 marked this conversation as resolved.
if ret == NGX_ERROR then
Comment thread
jarvis9443 marked this conversation as resolved.
return nil, "error while updating upstream state"
end

return true
end


return _M
85 changes: 85 additions & 0 deletions src/ngx_http_apisix_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,3 +1080,88 @@ ngx_http_apisix_is_upstream_pass_trailers(ngx_http_request_t *r)

return 1;
}


ngx_int_t
ngx_http_apisix_push_upstream_state(ngx_http_request_t *r,
const u_char *addr, size_t addr_len, ngx_int_t status,
ngx_msec_int_t connect_time_ms, ngx_msec_int_t header_time_ms)
{
ngx_http_upstream_state_t *state;
ngx_str_t *peer;
u_char *p;

if (r->upstream_states == NULL) {
r->upstream_states = ngx_array_create(r->pool, 1,
sizeof(ngx_http_upstream_state_t));
if (r->upstream_states == NULL) {
return NGX_ERROR;
}
}

state = ngx_array_push(r->upstream_states);
if (state == NULL) {
return NGX_ERROR;
}

ngx_memzero(state, sizeof(ngx_http_upstream_state_t));

/* unset timings render as "-" in logs, not "0.000" */
state->response_time = (ngx_msec_t) -1;
state->connect_time = (ngx_msec_t) -1;
state->header_time = (ngx_msec_t) -1;

if (addr != NULL && addr_len > 0) {
peer = ngx_palloc(r->pool, sizeof(ngx_str_t));
if (peer == NULL) {
return NGX_ERROR;
}

p = ngx_pnalloc(r->pool, addr_len);
if (p == NULL) {
return NGX_ERROR;
}

ngx_memcpy(p, addr, addr_len);
peer->data = p;
peer->len = addr_len;
state->peer = peer;
}

state->status = status;

if (connect_time_ms >= 0) {
state->connect_time = (ngx_msec_t) connect_time_ms;
}

if (header_time_ms >= 0) {
state->header_time = (ngx_msec_t) header_time_ms;
}

return NGX_OK;
}


ngx_int_t
ngx_http_apisix_update_upstream_state(ngx_http_request_t *r,
ngx_msec_int_t response_time_ms, off_t response_length)
{
ngx_http_upstream_state_t *state;

if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
return NGX_ERROR;
}

state = (ngx_http_upstream_state_t *) r->upstream_states->elts
+ (r->upstream_states->nelts - 1);

if (response_time_ms >= 0) {
state->response_time = (ngx_msec_t) response_time_ms;
}

if (response_length >= 0) {
state->response_length = response_length;
}

return NGX_OK;
}
6 changes: 6 additions & 0 deletions src/ngx_http_apisix_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ char * ngx_http_apisix_error_log_request_id(ngx_conf_t *cf, ngx_command_t *cmd,

ngx_int_t ngx_http_apisix_set_upstream_pass_trailers(ngx_http_request_t *r, int on);
ngx_int_t ngx_http_apisix_is_upstream_pass_trailers(ngx_http_request_t *r);

ngx_int_t ngx_http_apisix_push_upstream_state(ngx_http_request_t *r,
const u_char *addr, size_t addr_len, ngx_int_t status,
ngx_msec_int_t connect_time_ms, ngx_msec_int_t header_time_ms);
ngx_int_t ngx_http_apisix_update_upstream_state(ngx_http_request_t *r,
ngx_msec_int_t response_time_ms, off_t response_length);
#endif /* _NGX_HTTP_APISIX_H_INCLUDED_ */
Loading
Loading